2 * drivers/power/ricoh619-battery.c
4 * Charger driver for RICOH RC5T619 power management chip.
6 * Copyright (C) 2012-2013 RICOH COMPANY,LTD
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
26 #include <linux/mutex.h>
27 #include <linux/string.h>
28 #include <linux/power_supply.h>
29 #include <linux/mfd/ricoh619.h>
30 #include <linux/power/ricoh619_battery.h>
31 #include <linux/power/ricoh61x_battery_init.h>
32 #include <linux/delay.h>
33 #include <linux/workqueue.h>
34 #include <linux/delay.h>
35 #include <mach/board.h>
38 #include <linux/interrupt.h>
39 #include <linux/irq.h>
42 /* define for function */
43 #define ENABLE_FUEL_GAUGE_FUNCTION
44 #define ENABLE_LOW_BATTERY_DETECTION
45 #define ENABLE_FACTORY_MODE
46 #define DISABLE_CHARGER_TIMER
47 /* #define ENABLE_FG_KEEP_ON_MODE */
52 #define RICOH619_REL1_SEL_VALUE 64
53 #define RICOH619_REL2_SEL_VALUE 0
63 //for debug #ifdef ENABLE_FUEL_GAUGE_FUNCTION
64 /* define for FG delayed time */
65 #define RICOH619_MONITOR_START_TIME 15
66 #define RICOH619_FG_RESET_TIME 6
67 #define RICOH619_FG_STABLE_TIME 120
68 #define RICOH619_DISPLAY_UPDATE_TIME 15
69 #define RICOH619_LOW_VOL_DOWN_TIME 10
70 #define RICOH619_CHARGE_MONITOR_TIME 20
71 #define RICOH619_CHARGE_RESUME_TIME 1
72 #define RICOH619_CHARGE_CALC_TIME 1
73 #define RICOH619_JEITA_UPDATE_TIME 60
74 /* define for FG parameter */
75 #define RICOH619_MAX_RESET_SOC_DIFF 5
76 #define RICOH619_GET_CHARGE_NUM 10
77 #define RICOH619_UPDATE_COUNT_DISP 4
78 #define RICOH619_UPDATE_COUNT_FULL 4
79 #define RICOH619_CHARGE_UPDATE_TIME 3
80 #define RE_CAP_GO_DOWN 10 /* 40 */
81 #define RICOH619_ENTER_LOW_VOL 70
82 #define RICOH619_TAH_SEL2 5
83 #define RICOH619_TAL_SEL2 6
84 /* define for FG status */
87 RICOH619_SOCA_UNSTABLE,
88 RICOH619_SOCA_FG_RESET,
93 RICOH619_SOCA_LOW_VOL,
97 #ifdef ENABLE_LOW_BATTERY_DETECTION
98 #define LOW_BATTERY_DETECTION_TIME 10
101 struct ricoh619_soca_info {
105 int soc; /* Latest FG SOC value */
108 int status; /* SOCA status 0: Not initial; 5: Finished */
110 int chg_status; /* chg_status */
111 int soc_delta; /* soc delta for status3(DISP) */
121 int Vbat[RICOH619_GET_CHARGE_NUM];
122 int Vsys[RICOH619_GET_CHARGE_NUM];
123 int Ibat[RICOH619_GET_CHARGE_NUM];
129 /* for LOW VOL state */
140 struct ricoh619_battery_info {
142 struct power_supply battery;
143 struct delayed_work monitor_work;
144 struct delayed_work displayed_work;
145 struct delayed_work charge_stable_work;
146 struct delayed_work changed_work;
147 #ifdef ENABLE_LOW_BATTERY_DETECTION
148 struct delayed_work low_battery_work;
150 struct delayed_work charge_monitor_work;
151 struct delayed_work get_charge_work;
152 struct delayed_work jeita_work;
154 struct work_struct irq_work; /* for Charging & VUSB/VADP */
155 struct work_struct usb_irq_work; /* for ADC_VUSB */
157 struct workqueue_struct *monitor_wqueue;
158 struct workqueue_struct *workqueue; /* for Charging & VUSB/VADP */
159 struct workqueue_struct *usb_workqueue; /* for ADC_VUSB */
161 #ifdef ENABLE_FACTORY_MODE
162 struct delayed_work factory_mode_work;
163 struct workqueue_struct *factory_mode_wqueue;
167 unsigned long monitor_time;
183 struct ricoh619_soca_info *soca;
185 bool entry_factory_mode;
208 /* this value is for mfd fucntion */
211 extern int dwc_vbus_status(void);
212 /*This is for full state*/
214 static int BatteryTableFlageDef=0;
215 static int BatteryTypeDef=0;
216 static int Battery_Type(void)
218 return BatteryTypeDef;
221 static int Battery_Table(void)
223 return BatteryTableFlageDef;
226 static void ricoh619_battery_work(struct work_struct *work)
228 struct ricoh619_battery_info *info = container_of(work,
229 struct ricoh619_battery_info, monitor_work.work);
231 RICOH_FG_DBG("PMU: %s\n", __func__);
232 power_supply_changed(&info->battery);
233 queue_delayed_work(info->monitor_wqueue, &info->monitor_work,
237 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
238 static int measure_vbatt_FG(struct ricoh619_battery_info *info, int *data);
239 static int measure_Ibatt_FG(struct ricoh619_battery_info *info, int *data);
240 static int calc_capacity(struct ricoh619_battery_info *info);
241 static int get_OCV_init_Data(struct ricoh619_battery_info *info, int index);
242 static int get_OCV_voltage(struct ricoh619_battery_info *info, int index);
243 static int get_check_fuel_gauge_reg(struct ricoh619_battery_info *info,
244 int Reg_h, int Reg_l, int enable_bit);
245 static int calc_capacity_in_period(struct ricoh619_battery_info *info,
246 int *cc_cap, bool *is_charging);
247 static int get_charge_priority(struct ricoh619_battery_info *info, bool *data);
248 static int set_charge_priority(struct ricoh619_battery_info *info, bool *data);
249 static int get_power_supply_status(struct ricoh619_battery_info *info);
250 static int measure_vsys_ADC(struct ricoh619_battery_info *info, int *data);
251 static int Calc_Linear_Interpolation(int x0, int y0, int x1, int y1, int y);
252 static int get_battery_temp(struct ricoh619_battery_info *info);
253 static int check_jeita_status(struct ricoh619_battery_info *info, bool *is_jeita_updated);
255 static int calc_ocv(struct ricoh619_battery_info *info)
262 ret = measure_vbatt_FG(info, &Vbat);
263 ret = measure_Ibatt_FG(info, &Ibat);
265 ocv = Vbat - Ibat * info->soca->Rbat;
270 static int check_charge_status_2(struct ricoh619_battery_info *info, int displayed_soc_temp)
272 if (displayed_soc_temp < 0)
273 displayed_soc_temp = 0;
275 get_power_supply_status(info);
277 if (POWER_SUPPLY_STATUS_FULL == info->soca->chg_status) {
279 info->soca->displayed_soc = 100*100;
281 if (info->soca->Ibat_ave >= 0) { /* for issue 3 */
282 if (g_full_flag == 1) {
283 info->soca->displayed_soc = 100*100;
285 if (info->soca->displayed_soc/100 < 99) {
286 info->soca->displayed_soc = displayed_soc_temp;
288 info->soca->displayed_soc = 99 * 100;
292 if (info->soca->Ibat_ave < 0) {
293 if (g_full_flag == 1) {
294 if ((calc_ocv(info) < (get_OCV_voltage(info, 10) - info->soca->f_chg_margin) )
295 && (info->soca->soc/100 <= 98) ) {
297 info->soca->displayed_soc = 100*100;
299 info->soca->displayed_soc = 100*100;
303 info->soca->displayed_soc = displayed_soc_temp;
307 return info->soca->displayed_soc;
311 * Calculate Capacity in a period
312 * - read CC_SUM & FA_CAP from Coulom Counter
313 * - and calculate Capacity.
314 * @cc_cap: capacity in a period, unit 0.01%
315 * @is_charging: Flag of charging current direction
316 * TRUE : charging (plus)
317 * FALSE: discharging (minus)
319 static int calc_capacity_in_period(struct ricoh619_battery_info *info,
320 int *cc_cap, bool *is_charging)
323 uint8_t cc_sum_reg[4];
324 uint8_t cc_clr[4] = {0, 0, 0, 0};
325 uint8_t fa_cap_reg[2];
330 uint8_t charge_state;
333 *is_charging = true; /* currrent state initialize -> charging */
335 if (info->entry_factory_mode)
338 //check need charge stop or not
339 /* get power supply status */
340 err = ricoh619_read(info->dev->parent, CHGSTATE_REG, &status);
343 charge_state = (status & 0x1F);
344 Ocv = calc_ocv(info);
345 if (charge_state == CHG_STATE_CHG_COMPLETE) {
346 /* Check CHG status is complete or not */
348 } else if (calc_capacity(info) == 100) {
349 /* Check HW soc is 100 or not */
351 } else if (Ocv/1000 < get_OCV_voltage(info, 9)) {
352 /* Check VBAT is high level or not */
358 if (cc_stop_flag == 1)
360 /* Disable Charging/Completion Interrupt */
361 err = ricoh619_set_bits(info->dev->parent,
362 RICOH619_INT_MSK_CHGSTS1, 0x01);
366 /* disable charging */
367 err = ricoh619_clr_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x03);
373 err = ricoh619_write(info->dev->parent, CC_CTRL_REG, 0x01);
378 err = ricoh619_bulk_reads(info->dev->parent,
379 CC_SUMREG3_REG, 4, cc_sum_reg);
384 err = ricoh619_bulk_writes(info->dev->parent,
385 CC_SUMREG3_REG, 4, cc_clr);
390 err = ricoh619_write(info->dev->parent, CC_CTRL_REG, 0);
393 if (cc_stop_flag == 1)
396 /* Enable charging */
397 err = ricoh619_set_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x03);
403 /* Clear Charging Interrupt status */
404 err = ricoh619_clr_bits(info->dev->parent,
405 RICOH619_INT_IR_CHGSTS1, 0x01);
409 /* ricoh619_read(info->dev->parent, RICOH619_INT_IR_CHGSTS1, &val);
410 RICOH_FG_DBG("INT_IR_CHGSTS1 = 0x%x\n",val); */
412 /* Enable Charging Interrupt */
413 err = ricoh619_clr_bits(info->dev->parent,
414 RICOH619_INT_MSK_CHGSTS1, 0x01);
419 err = ricoh619_bulk_reads(info->dev->parent,
420 FA_CAP_H_REG, 2, fa_cap_reg);
424 /* fa_cap = *(uint16_t*)fa_cap_reg & 0x7fff; */
425 fa_cap = (fa_cap_reg[0] << 8 | fa_cap_reg[1]) & 0x7fff;
427 /* cc_sum = *(uint32_t*)cc_sum_reg; */
428 cc_sum = cc_sum_reg[0] << 24 | cc_sum_reg[1] << 16 |
429 cc_sum_reg[2] << 8 | cc_sum_reg[3];
431 /* calculation two's complement of CC_SUM */
432 if (cc_sum & 0x80000000) {
433 cc_sum = (cc_sum^0xffffffff)+0x01;
434 *is_charging = false; /* discharge */
437 *cc_cap = cc_sum*25/9/fa_cap; /* CC_SUM/3600/FA_CAP */
441 dev_err(info->dev, "Error !!-----\n");
445 * Calculate target using capacity
447 static int get_target_use_cap(struct ricoh619_battery_info *info)
452 int Target_Cutoff_Vol;
462 /* get const value */
463 Ibat_min = -1 * info->soca->target_ibat;
464 if (info->soca->Ibat_ave > Ibat_min) /* I bat is minus */
468 Ibat_now = info->soca->Ibat_ave;
470 fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
472 use_cap = fa_cap - info->soca->re_cap_old;
474 Ocv_ZeroPer_now = info->soca->target_vsys * 1000 - Ibat_now * info->soca->Rsys;
476 RICOH_FG_DBG("PMU: ------- Rsys= %d: cutoff_ocv= %d: Ocv_ZeroPer_now= %d =======\n",
477 info->soca->Rsys, info->soca->cutoff_ocv, Ocv_ZeroPer_now);
481 for (i = 0; i <= 10; i = i+1) {
482 temp = (battery_init_para[info->num][i*2]<<8)
483 | (battery_init_para[info->num][i*2+1]);
484 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
485 temp = ((temp * 50000 * 10 / 4095) + 5) / 10;
488 for (i = 1; i < 11; i++) {
489 if (ocv_table[i] >= Ocv_ZeroPer_now / 100) {
491 start_per = Calc_Linear_Interpolation(
492 (i-1)*1000, ocv_table[i-1], i*1000,
493 ocv_table[i], (Ocv_ZeroPer_now / 100));
498 FA_CAP_now = fa_cap * ((10000 - start_per) / 100 ) / 100;
500 RICOH_FG_DBG("PMU: ------- Target_Cutoff_Vol= %d: Ocv_ZeroPer_now= %d: start_per= %d =======\n",
501 Target_Cutoff_Vol, Ocv_ZeroPer_now, start_per);
504 RE_CAP_now = FA_CAP_now - use_cap;
506 if (RE_CAP_now < RE_CAP_GO_DOWN || info->soca->Vsys_ave < info->soca->target_vsys*1000) {
507 info->soca->hurry_up_flg = 1;
509 info->soca->hurry_up_flg = 0;
510 /* get CC_OnePer_step */
511 if (info->soca->displayed_soc > 0) { /* avoid divide-by-0 */
512 CC_OnePer_step = RE_CAP_now / (info->soca->displayed_soc / 100);
516 /* get info->soca->target_use_cap */
517 info->soca->target_use_cap = use_cap + CC_OnePer_step;
520 RICOH_FG_DBG("PMU: ------- FA_CAP_now= %d: RE_CAP_now= %d: target_use_cap= %d: hurry_up_flg= %d -------\n",
521 FA_CAP_now, RE_CAP_now, info->soca->target_use_cap, info->soca->hurry_up_flg);
526 static void ricoh619_displayed_work(struct work_struct *work)
534 int displayed_soc_temp;
536 bool is_charging = true;
538 int re_cap,fa_cap,use_cap;
539 bool is_jeita_updated;
541 struct ricoh619_battery_info *info = container_of(work,
542 struct ricoh619_battery_info, displayed_work.work);
544 if (info->entry_factory_mode) {
545 info->soca->status = RICOH619_SOCA_STABLE;
546 info->soca->displayed_soc = -EINVAL;
547 info->soca->ready_fg = 0;
551 mutex_lock(&info->lock);
553 is_jeita_updated = false;
555 if ((RICOH619_SOCA_START == info->soca->status)
556 || (RICOH619_SOCA_STABLE == info->soca->status))
557 info->soca->ready_fg = 1;
559 /* judege Full state or Moni Vsys state */
560 if ((RICOH619_SOCA_DISP == info->soca->status)
561 || (RICOH619_SOCA_STABLE == info->soca->status))
563 /* for issue 1 solution start*/
564 if(g_full_flag == 1){
565 info->soca->status = RICOH619_SOCA_FULL;
566 info->soca->update_count = 0;
568 /* for issue1 solution end */
569 /* check Full state or not*/
570 if (info->soca->Ibat_ave >= 0) {
571 if ((calc_ocv(info) > (get_OCV_voltage(info, 10) - info->soca->f_chg_margin))
572 || (info->soca->displayed_soc/100 >= 99))
575 info->soca->status = RICOH619_SOCA_FULL;
576 info->soca->update_count = 0;
578 } else { /* dis-charging */
579 if (info->soca->displayed_soc/100 < RICOH619_ENTER_LOW_VOL) {
580 info->soca->status = RICOH619_SOCA_LOW_VOL;
585 if (RICOH619_SOCA_STABLE == info->soca->status) {
586 info->soca->soc = calc_capacity(info) * 100;
587 soc_round = info->soca->soc / 100;
588 last_soc_round = info->soca->last_soc / 100;
590 info->soca->soc_delta = soc_round - last_soc_round;
593 if (info->soca->chg_status == POWER_SUPPLY_STATUS_CHARGING) {
594 if (soc_round >= 90) {
596 if (info->soca->reset_flg_90 == 0) {
597 err = ricoh619_write(info->dev->parent,
600 dev_err(info->dev, "Error in writing the control register\n");
602 info->soca->ready_fg = 0;
604 for (i = 0; i < 3; i = i+1) {
605 info->soca->reset_soc[i] = 0;
607 info->soca->stable_count = 0;
609 info->soca->status = RICOH619_SOCA_FG_RESET;
610 /* Delay for addition Reset Time (6s) */
611 info->soca->stable_count = 1;
612 queue_delayed_work(info->monitor_wqueue,
613 &info->charge_stable_work,
614 RICOH619_FG_RESET_TIME*HZ);
616 info->soca->reset_flg_90 = 1;
619 } else if (soc_round < 100) {
620 if (info->soca->reset_flg_95 == 0) {
621 err = ricoh619_write(info->dev->parent,
624 dev_err(info->dev, "Error in writing the control register\n");
626 info->soca->ready_fg = 0;
628 for (i = 0; i < 3; i = i+1) {
629 info->soca->reset_soc[i] = 0;
632 info->soca->stable_count = 0;
633 info->soca->status = RICOH619_SOCA_FG_RESET;
634 info->soca->stable_count = 1;
635 queue_delayed_work(info->monitor_wqueue,
636 &info->charge_stable_work,
637 RICOH619_FG_RESET_TIME*HZ);
639 info->soca->reset_flg_95 = 1;
644 info->soca->reset_flg_90 = 0;
645 info->soca->reset_flg_95 = 0;
649 if (info->soca->soc_delta >= -1 && info->soca->soc_delta <= 1) {
650 info->soca->displayed_soc = info->soca->soc;
652 info->soca->status = RICOH619_SOCA_DISP;
654 info->soca->last_soc = info->soca->soc;
655 info->soca->soc_delta = 0;
656 info->soca->update_count = 0;
657 } else if (RICOH619_SOCA_FULL == info->soca->status) {
658 err = check_jeita_status(info, &is_jeita_updated);
660 dev_err(info->dev, "Error in updating JEITA %d\n", err);
663 info->soca->soc = calc_capacity(info) * 100;
664 if (POWER_SUPPLY_STATUS_FULL == info->soca->chg_status) {
665 if (0 == info->soca->jt_limit) {
667 info->soca->displayed_soc = 100*100;
668 info->soca->update_count = 0;
670 info->soca->update_count = 0;
673 if (info->soca->Ibat_ave >= 0) { /* for issue 3 */
674 if (0 == info->soca->jt_limit) {
675 if (g_full_flag == 1) {
676 info->soca->displayed_soc = 100*100;
677 info->soca->update_count = 0;
679 if (info->soca->displayed_soc/100 < 99) {
680 info->soca->update_count++;
681 if (info->soca->update_count
682 >= RICOH619_UPDATE_COUNT_FULL) {
683 info->soca->displayed_soc = info->soca->displayed_soc + 100;
684 info->soca->update_count = 0;
687 info->soca->displayed_soc = 99 * 100;
688 info->soca->update_count = 0;
692 info->soca->update_count = 0;
695 if (info->soca->Ibat_ave < 0) { /* for issue 3 */
696 info->soca->update_count = 0;
697 if (g_full_flag == 1) {
698 if ((calc_ocv(info) < (get_OCV_voltage(info, 10) - info->soca->f_chg_margin))
699 && (info->soca->soc/100 <= 98)) { /* for issue 2 */
701 info->soca->displayed_soc = 100*100;
702 info->soca->status = RICOH619_SOCA_DISP;
703 info->soca->last_soc = info->soca->soc;
704 info->soca->soc_delta = 0;
706 info->soca->displayed_soc = 100*100;
710 info->soca->status = RICOH619_SOCA_DISP;
711 info->soca->last_soc = info->soca->soc;
712 info->soca->soc_delta = 0;
715 } else if (RICOH619_SOCA_LOW_VOL == info->soca->status) {
716 if(info->soca->Ibat_ave >= 0) {
717 info->soca->soc = calc_capacity(info) * 100;
718 info->soca->status = RICOH619_SOCA_DISP;
719 info->soca->last_soc = info->soca->soc;
720 info->soca->soc_delta = 0;
721 info->soca->update_count = 0;
723 re_cap = get_check_fuel_gauge_reg(info, RE_CAP_H_REG, RE_CAP_L_REG,
725 fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
727 use_cap = fa_cap - re_cap;
729 if ((info->soca->target_use_cap == 0)
730 && (info->soca->hurry_up_flg == 0)) {
731 info->soca->re_cap_old = re_cap;
732 get_target_use_cap(info);
735 if((use_cap >= info->soca->target_use_cap)
736 || (info->soca->hurry_up_flg == 1)) {
737 info->soca->displayed_soc = info->soca->displayed_soc - 100;
738 info->soca->displayed_soc = max(0, info->soca->displayed_soc);
739 info->soca->re_cap_old = re_cap;
741 get_target_use_cap(info);
742 info->soca->soc = calc_capacity(info) * 100;
745 if (RICOH619_SOCA_DISP == info->soca->status) {
747 info->soca->soc = calc_capacity(info) * 100;
749 soc_round = info->soca->soc / 100;
750 last_soc_round = info->soca->last_soc / 100;
751 last_disp_round = (info->soca->displayed_soc + 50) / 100;
753 info->soca->soc_delta =
754 info->soca->soc_delta + (soc_round - last_soc_round);
756 info->soca->last_soc = info->soca->soc;
758 if (last_disp_round == soc_round) {
759 /* if SOC == DISPLAY move to stable */
760 info->soca->displayed_soc = info->soca->soc ;
761 info->soca->status = RICOH619_SOCA_STABLE;
762 } else if (info->soca->Ibat_ave > 0) {
763 if ((0 == info->soca->jt_limit) ||
764 (POWER_SUPPLY_STATUS_FULL != info->soca->chg_status)) {
766 if (last_disp_round < soc_round) {
767 /* Case 1 : Charge, Display < SOC */
768 info->soca->update_count++;
769 if ((info->soca->update_count
770 >= RICOH619_UPDATE_COUNT_DISP)
771 || (info->soca->soc_delta == 1)) {
772 info->soca->displayed_soc
773 = (last_disp_round + 1)*100;
774 info->soca->update_count = 0;
775 info->soca->soc_delta = 0;
777 if (info->soca->displayed_soc/100
779 info->soca->displayed_soc
782 = RICOH619_SOCA_STABLE;
784 } else if (last_disp_round > soc_round) {
785 /* Case 2 : Charge, Display > SOC */
786 info->soca->update_count = 0;
787 if (info->soca->soc_delta >= 3) {
788 info->soca->displayed_soc =
789 (last_disp_round + 1)*100;
790 info->soca->soc_delta -= 3;
792 if (info->soca->displayed_soc/100
794 info->soca->displayed_soc
797 = RICOH619_SOCA_STABLE;
801 info->soca->update_count = 0;
802 info->soca->soc_delta = 0;
806 if (last_disp_round > soc_round) {
807 /* Case 3 : Dis-Charge, Display > SOC */
808 info->soca->update_count++;
809 if ((info->soca->update_count
810 >= RICOH619_UPDATE_COUNT_DISP)
811 || (info->soca->soc_delta == -1)) {
812 info->soca->displayed_soc
813 = (last_disp_round - 1)*100;
814 info->soca->update_count = 0;
815 info->soca->soc_delta = 0;
817 if (info->soca->displayed_soc/100
819 info->soca->displayed_soc
822 = RICOH619_SOCA_STABLE;
824 } else if (last_disp_round < soc_round) {
825 /* Case 4 : Dis-Charge, Display < SOC */
826 info->soca->update_count = 0;
827 if (info->soca->soc_delta <= -3) {
828 info->soca->displayed_soc
829 = (last_disp_round - 1)*100;
830 info->soca->soc_delta += 3;
832 if (info->soca->displayed_soc/100
834 info->soca->displayed_soc
837 = RICOH619_SOCA_STABLE;
841 } else if (RICOH619_SOCA_UNSTABLE == info->soca->status) {
842 if (0 == info->soca->jt_limit) {
843 check_charge_status_2(info, info->soca->displayed_soc);
845 } else if (RICOH619_SOCA_FG_RESET == info->soca->status) {
847 } else if (RICOH619_SOCA_START == info->soca->status) {
848 err = check_jeita_status(info, &is_jeita_updated);
849 is_jeita_updated = false;
851 dev_err(info->dev, "Error in updating JEITA %d\n", err);
853 err = ricoh619_read(info->dev->parent, PSWR_REG, &val);
855 if (info->first_pwon) {
856 info->soca->soc = calc_capacity(info) * 100;
857 if ((info->soca->soc == 0) && (calc_ocv(info)
858 < get_OCV_voltage(info, 0))) {
859 info->soca->displayed_soc = 0;
860 info->soca->status = RICOH619_SOCA_ZERO;
862 if (0 == info->soca->jt_limit) {
863 check_charge_status_2(info, info->soca->soc);
865 info->soca->displayed_soc = info->soca->soc;
867 info->soca->status = RICOH619_SOCA_UNSTABLE;
869 } else if (g_fg_on_mode && (val == 0x7f)) {
870 info->soca->soc = calc_capacity(info) * 100;
871 if ((info->soca->soc == 0) && (calc_ocv(info)
872 < get_OCV_voltage(info, 0))) {
873 info->soca->displayed_soc = 0;
874 info->soca->status = RICOH619_SOCA_ZERO;
876 if (0 == info->soca->jt_limit) {
877 check_charge_status_2(info, info->soca->soc);
879 info->soca->displayed_soc = info->soca->soc;
881 info->soca->status = RICOH619_SOCA_STABLE;
884 info->soca->soc = val * 100;
887 "Error in reading PSWR_REG %d\n", err);
889 = calc_capacity(info) * 100;
892 err = calc_capacity_in_period(info, &cc_cap,
895 dev_err(info->dev, "Read cc_sum Error !!-----\n");
898 = (is_charging == true) ? cc_cap : -cc_cap;
899 if (calc_ocv(info) < get_OCV_voltage(info, 0)) {
900 info->soca->displayed_soc = 0;
901 info->soca->status = RICOH619_SOCA_ZERO;
904 = info->soca->soc + info->soca->cc_delta;
905 if (displayed_soc_temp < 0)
906 displayed_soc_temp = 0;
908 = min(10000, displayed_soc_temp);
909 displayed_soc_temp = max(0, displayed_soc_temp);
910 if (0 == info->soca->jt_limit) {
911 check_charge_status_2(info, displayed_soc_temp);
913 info->soca->displayed_soc = displayed_soc_temp;
915 info->soca->status = RICOH619_SOCA_UNSTABLE;
918 } else if (RICOH619_SOCA_ZERO == info->soca->status) {
919 if (calc_ocv(info) > get_OCV_voltage(info, 0)) {
920 err = ricoh619_write(info->dev->parent,
923 dev_err(info->dev, "Error in writing the control register\n");
924 info->soca->status = RICOH619_SOCA_STABLE;
925 info->soca->ready_fg = 0;
927 info->soca->displayed_soc = 0;
931 && (info->soca->status == RICOH619_SOCA_STABLE)) {
932 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
934 dev_err(info->dev, "Error in writing PSWR_REG\n");
937 if (info->soca->displayed_soc < 0) {
940 val = (info->soca->displayed_soc + 50)/100;
943 err = ricoh619_write(info->dev->parent, PSWR_REG, val);
945 dev_err(info->dev, "Error in writing PSWR_REG\n");
949 err = calc_capacity_in_period(info, &cc_cap, &is_charging);
951 dev_err(info->dev, "Read cc_sum Error !!-----\n");
954 RICOH_FG_DBG("PMU: ------- STATUS= %d: IBAT= %d: VSYS= %d: VBAT= %d: DSOC= %d: RSOC= %d: -------\n",
955 info->soca->status, info->soca->Ibat_ave, info->soca->Vsys_ave, info->soca->Vbat_ave,
956 info->soca->displayed_soc, info->soca->soc);
958 #ifdef DISABLE_CHARGER_TIMER
959 /* clear charger timer */
960 if ( info->soca->chg_status == POWER_SUPPLY_STATUS_CHARGING ) {
961 err = ricoh619_read(info->dev->parent, TIMSET_REG, &val);
964 "Error in read TIMSET_REG%d\n", err);
965 /* to check bit 0-1 */
969 /* set rapid timer 240 -> 300 */
970 err = ricoh619_set_bits(info->dev->parent, TIMSET_REG, 0x03);
972 dev_err(info->dev, "Error in writing the control register\n");
975 /* set rapid timer 300 -> 240 */
976 err = ricoh619_clr_bits(info->dev->parent, TIMSET_REG, 0x01);
977 err = ricoh619_set_bits(info->dev->parent, TIMSET_REG, 0x02);
979 dev_err(info->dev, "Error in writing the control register\n");
985 if (0 == info->soca->ready_fg)
986 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
987 RICOH619_FG_RESET_TIME * HZ);
988 else if (RICOH619_SOCA_DISP == info->soca->status)
989 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
990 RICOH619_DISPLAY_UPDATE_TIME * HZ);
991 else if (info->soca->hurry_up_flg == 1)
992 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
993 RICOH619_LOW_VOL_DOWN_TIME * HZ);
995 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
996 RICOH619_DISPLAY_UPDATE_TIME * HZ);
998 mutex_unlock(&info->lock);
1000 if(true == is_jeita_updated)
1001 power_supply_changed(&info->battery);
1006 static void ricoh619_stable_charge_countdown_work(struct work_struct *work)
1012 struct ricoh619_battery_info *info = container_of(work,
1013 struct ricoh619_battery_info, charge_stable_work.work);
1015 if (info->entry_factory_mode)
1018 mutex_lock(&info->lock);
1019 if (RICOH619_SOCA_FG_RESET == info->soca->status)
1020 info->soca->ready_fg = 1;
1022 if (2 <= info->soca->stable_count) {
1023 if (3 == info->soca->stable_count
1024 && RICOH619_SOCA_FG_RESET == info->soca->status) {
1025 ret = ricoh619_write(info->dev->parent,
1028 dev_err(info->dev, "Error in writing the control register\n");
1029 info->soca->ready_fg = 0;
1031 info->soca->stable_count = info->soca->stable_count - 1;
1032 queue_delayed_work(info->monitor_wqueue,
1033 &info->charge_stable_work,
1034 RICOH619_FG_STABLE_TIME * HZ / 10);
1035 } else if (0 >= info->soca->stable_count) {
1036 /* Finished queue, ignore */
1037 } else if (1 == info->soca->stable_count) {
1038 if (RICOH619_SOCA_UNSTABLE == info->soca->status) {
1039 /* Judge if FG need reset or Not */
1040 info->soca->soc = calc_capacity(info) * 100;
1041 if (info->chg_ctr != 0) {
1042 queue_delayed_work(info->monitor_wqueue,
1043 &info->charge_stable_work,
1044 RICOH619_FG_STABLE_TIME * HZ / 10);
1045 mutex_unlock(&info->lock);
1048 /* Do reset setting */
1049 ret = ricoh619_write(info->dev->parent,
1052 dev_err(info->dev, "Error in writing the control register\n");
1054 info->soca->ready_fg = 0;
1055 info->soca->status = RICOH619_SOCA_FG_RESET;
1057 /* Delay for addition Reset Time (6s) */
1058 queue_delayed_work(info->monitor_wqueue,
1059 &info->charge_stable_work,
1060 RICOH619_FG_RESET_TIME*HZ);
1061 } else if (RICOH619_SOCA_FG_RESET == info->soca->status) {
1062 info->soca->reset_soc[2] = info->soca->reset_soc[1];
1063 info->soca->reset_soc[1] = info->soca->reset_soc[0];
1064 info->soca->reset_soc[0] = calc_capacity(info) * 100;
1065 info->soca->reset_count++;
1067 if (info->soca->reset_count > 10) {
1068 /* Reset finished; */
1069 info->soca->soc = info->soca->reset_soc[0];
1070 info->soca->stable_count = 0;
1074 for (i = 0; i < 3; i++) {
1075 if (max < info->soca->reset_soc[i]/100)
1076 max = info->soca->reset_soc[i]/100;
1077 if (min > info->soca->reset_soc[i]/100)
1078 min = info->soca->reset_soc[i]/100;
1081 if ((info->soca->reset_count > 3) && ((max - min)
1082 < RICOH619_MAX_RESET_SOC_DIFF)) {
1083 /* Reset finished; */
1084 info->soca->soc = info->soca->reset_soc[0];
1085 info->soca->stable_count = 0;
1088 /* Do reset setting */
1089 ret = ricoh619_write(info->dev->parent,
1092 dev_err(info->dev, "Error in writing the control register\n");
1094 info->soca->ready_fg = 0;
1096 /* Delay for addition Reset Time (6s) */
1097 queue_delayed_work(info->monitor_wqueue,
1098 &info->charge_stable_work,
1099 RICOH619_FG_RESET_TIME*HZ);
1101 /* Finished queue From now, select FG as result; */
1102 } else if (RICOH619_SOCA_START == info->soca->status) {
1103 /* Normal condition */
1104 } else { /* other state ZERO/DISP/STABLE */
1105 info->soca->stable_count = 0;
1108 mutex_unlock(&info->lock);
1112 info->soca->last_soc = info->soca->soc;
1113 info->soca->status = RICOH619_SOCA_DISP;
1114 info->soca->soc_delta = 0;
1115 info->soca->update_count = 0;
1118 mutex_unlock(&info->lock);
1122 static void ricoh619_charge_monitor_work(struct work_struct *work)
1124 struct ricoh619_battery_info *info = container_of(work,
1125 struct ricoh619_battery_info, charge_monitor_work.work);
1127 get_power_supply_status(info);
1129 if (POWER_SUPPLY_STATUS_DISCHARGING == info->soca->chg_status
1130 || POWER_SUPPLY_STATUS_NOT_CHARGING == info->soca->chg_status) {
1131 switch (info->soca->dischg_state) {
1133 info->soca->dischg_state = 1;
1136 info->soca->dischg_state = 2;
1144 info->soca->dischg_state = 0;
1147 queue_delayed_work(info->monitor_wqueue, &info->charge_monitor_work,
1148 RICOH619_CHARGE_MONITOR_TIME * HZ);
1153 static void ricoh619_get_charge_work(struct work_struct *work)
1155 struct ricoh619_battery_info *info = container_of(work,
1156 struct ricoh619_battery_info, get_charge_work.work);
1158 int Vbat_temp, Vsys_temp, Ibat_temp;
1159 int Vbat_sort[RICOH619_GET_CHARGE_NUM];
1160 int Vsys_sort[RICOH619_GET_CHARGE_NUM];
1161 int Ibat_sort[RICOH619_GET_CHARGE_NUM];
1165 mutex_lock(&info->lock);
1167 for (i = RICOH619_GET_CHARGE_NUM-1; i > 0; i--) {
1168 if (0 == info->soca->chg_count) {
1169 info->soca->Vbat[i] = 0;
1170 info->soca->Vsys[i] = 0;
1171 info->soca->Ibat[i] = 0;
1173 info->soca->Vbat[i] = info->soca->Vbat[i-1];
1174 info->soca->Vsys[i] = info->soca->Vsys[i-1];
1175 info->soca->Ibat[i] = info->soca->Ibat[i-1];
1179 ret = measure_vbatt_FG(info, &info->soca->Vbat[0]);
1180 ret = measure_vsys_ADC(info, &info->soca->Vsys[0]);
1181 ret = measure_Ibatt_FG(info, &info->soca->Ibat[0]);
1183 info->soca->chg_count++;
1185 if (RICOH619_GET_CHARGE_NUM != info->soca->chg_count) {
1186 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1187 RICOH619_CHARGE_CALC_TIME * HZ);
1188 mutex_unlock(&info->lock);
1192 for (i = 0; i < RICOH619_GET_CHARGE_NUM; i++) {
1193 Vbat_sort[i] = info->soca->Vbat[i];
1194 Vsys_sort[i] = info->soca->Vsys[i];
1195 Ibat_sort[i] = info->soca->Ibat[i];
1201 for (i = 0; i < RICOH619_GET_CHARGE_NUM - 1; i++) {
1202 for (j = RICOH619_GET_CHARGE_NUM - 1; j > i; j--) {
1203 if (Vbat_sort[j - 1] > Vbat_sort[j]) {
1204 Vbat_temp = Vbat_sort[j];
1205 Vbat_sort[j] = Vbat_sort[j - 1];
1206 Vbat_sort[j - 1] = Vbat_temp;
1208 if (Vsys_sort[j - 1] > Vsys_sort[j]) {
1209 Vsys_temp = Vsys_sort[j];
1210 Vsys_sort[j] = Vsys_sort[j - 1];
1211 Vsys_sort[j - 1] = Vsys_temp;
1213 if (Ibat_sort[j - 1] > Ibat_sort[j]) {
1214 Ibat_temp = Ibat_sort[j];
1215 Ibat_sort[j] = Ibat_sort[j - 1];
1216 Ibat_sort[j - 1] = Ibat_temp;
1224 for (i = 3; i < RICOH619_GET_CHARGE_NUM-3; i++) {
1225 Vbat_temp = Vbat_temp + Vbat_sort[i];
1226 Vsys_temp = Vsys_temp + Vsys_sort[i];
1227 Ibat_temp = Ibat_temp + Ibat_sort[i];
1229 Vbat_temp = Vbat_temp / (RICOH619_GET_CHARGE_NUM - 6);
1230 Vsys_temp = Vsys_temp / (RICOH619_GET_CHARGE_NUM - 6);
1231 Ibat_temp = Ibat_temp / (RICOH619_GET_CHARGE_NUM - 6);
1233 if (0 == info->soca->chg_count) {
1234 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1235 RICOH619_CHARGE_UPDATE_TIME * HZ);
1236 mutex_unlock(&info->lock);
1239 info->soca->Vbat_ave = Vbat_temp;
1240 info->soca->Vsys_ave = Vsys_temp;
1241 info->soca->Ibat_ave = Ibat_temp;
1244 info->soca->chg_count = 0;
1245 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1246 RICOH619_CHARGE_UPDATE_TIME * HZ);
1247 mutex_unlock(&info->lock);
1251 /* Initial setting of FuelGauge SOCA function */
1252 static int ricoh619_init_fgsoca(struct ricoh619_battery_info *info)
1258 for (i = 0; i <= 10; i = i+1) {
1259 info->soca->ocv_table[i] = get_OCV_voltage(info, i);
1260 RICOH_FG_DBG("PMU: %s : * %d0%% voltage = %d uV\n",
1261 __func__, i, info->soca->ocv_table[i]);
1264 for (i = 0; i < 3; i = i+1)
1265 info->soca->reset_soc[i] = 0;
1266 info->soca->reset_count = 0;
1268 if (info->first_pwon) {
1270 err = ricoh619_read(info->dev->parent, CHGISET_REG, &val);
1273 "Error in read CHGISET_REG%d\n", err);
1275 err = ricoh619_write(info->dev->parent, CHGISET_REG, 0);
1278 "Error in writing CHGISET_REG%d\n", err);
1281 if (!info->entry_factory_mode) {
1282 err = ricoh619_write(info->dev->parent,
1285 dev_err(info->dev, "Error in writing the control register\n");
1290 err = ricoh619_write(info->dev->parent, CHGISET_REG, val);
1293 "Error in writing CHGISET_REG%d\n", err);
1296 /* Rbat : Transfer */
1297 info->soca->Rbat = get_OCV_init_Data(info, 12) * 1000 / 512
1299 info->soca->n_cap = get_OCV_init_Data(info, 11);
1301 info->soca->f_chg_margin = (get_OCV_voltage(info, 10) -
1302 get_OCV_voltage(info, 9)) / 10 * 3;
1304 info->soca->displayed_soc = 0;
1305 info->soca->suspend_soc = 0;
1306 info->soca->ready_fg = 0;
1307 info->soca->soc_delta = 0;
1308 info->soca->update_count = 0;
1309 info->soca->status = RICOH619_SOCA_START;
1310 /* stable count down 11->2, 1: reset; 0: Finished; */
1311 info->soca->stable_count = 11;
1312 info->soca->dischg_state = 0;
1313 info->soca->Vbat_ave = 0;
1314 info->soca->Vsys_ave = 0;
1315 info->soca->Ibat_ave = 0;
1316 info->soca->chg_count = 0;
1317 info->soca->target_use_cap = 0;
1318 info->soca->hurry_up_flg = 0;
1319 info->soca->re_cap_old = 0;
1320 info->soca->jt_limit = 0;
1322 for (i = 0; i < RICOH619_GET_CHARGE_NUM; i++) {
1323 info->soca->Vbat[i] = 0;
1324 info->soca->Vsys[i] = 0;
1325 info->soca->Ibat[i] = 0;
1328 #ifdef ENABLE_FG_KEEP_ON_MODE
1334 /* Start first Display job */
1335 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1336 RICOH619_FG_RESET_TIME*HZ);
1338 /* Start first Waiting stable job */
1339 queue_delayed_work(info->monitor_wqueue, &info->charge_stable_work,
1340 RICOH619_FG_STABLE_TIME*HZ/10);
1342 queue_delayed_work(info->monitor_wqueue, &info->charge_monitor_work,
1343 RICOH619_CHARGE_MONITOR_TIME * HZ);
1345 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1346 RICOH619_CHARGE_MONITOR_TIME * HZ);
1348 if (info->jt_hw_sw) {
1349 /* Enable JEITA function supported by H/W */
1350 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x04);
1352 dev_err(info->dev, "Error in writing the control register\n");
1354 /* Disable JEITA function supported by H/W */
1355 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x04);
1357 dev_err(info->dev, "Error in writing the control register\n");
1358 queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
1359 RICOH619_FG_RESET_TIME * HZ);
1362 /* Disable JEITA function supported by H/W */
1363 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x04);
1365 dev_err(info->dev, "Error in writing the control register\n");
1368 RICOH_FG_DBG("PMU: %s : * Rbat = %d mOhm n_cap = %d mAH\n",
1369 __func__, info->soca->Rbat, info->soca->n_cap);
1374 static void ricoh619_changed_work(struct work_struct *work)
1376 struct ricoh619_battery_info *info = container_of(work,
1377 struct ricoh619_battery_info, changed_work.work);
1379 RICOH_FG_DBG("PMU: %s\n", __func__);
1380 power_supply_changed(&info->battery);
1385 static int check_jeita_status(struct ricoh619_battery_info *info, bool *is_jeita_updated)
1386 /* JEITA Parameter settings
1390 * jt_vfchg_h~+~~~~~~~~~~~~~~~~~~~+
1392 * jt_vfchg_l-| - - - - - - - - - +~~~~~~~~~~+
1394 * -------0--+-------------------+----------+--- Temp
1398 * jt_ichg_h-+ - -+~~~~~~~~~~~~~~+~~~~~~~~~~+
1400 * jt_ichg_l-+~~~~+ Charge area |
1402 * 0--+----+--------------+----------+--- Temp
1403 * 0 jt_temp_l jt_temp_h 55
1410 uint8_t chgiset_org;
1411 uint8_t batset2_org;
1412 uint8_t set_vchg_h, set_vchg_l;
1413 uint8_t set_ichg_h, set_ichg_l;
1415 *is_jeita_updated = false;
1417 /* No execute if JEITA disabled */
1418 if (!info->jt_en || info->jt_hw_sw)
1421 /* Check FG Reset */
1422 if (info->soca->ready_fg) {
1423 temp = get_battery_temp(info) / 10;
1425 RICOH_FG_DBG(KERN_INFO "JEITA: %s *** cannot update by resetting FG ******\n", __func__);
1430 err = ricoh619_read(info->dev->parent, BATSET2_REG, &batset2_org);
1432 dev_err(info->dev, "Error in readng the battery setting register\n");
1435 vfchg = (batset2_org & 0x70) >> 4;
1436 batset2_org &= 0x8F;
1439 err = ricoh619_read(info->dev->parent, CHGISET_REG, &chgiset_org);
1441 dev_err(info->dev, "Error in readng the chrage setting register\n");
1444 chgiset_org &= 0xC0;
1446 set_ichg_h = (uint8_t)(chgiset_org | info->jt_ichg_h);
1447 set_ichg_l = (uint8_t)(chgiset_org | info->jt_ichg_l);
1449 set_vchg_h = (uint8_t)((info->jt_vfchg_h << 4) | batset2_org);
1450 set_vchg_l = (uint8_t)((info->jt_vfchg_l << 4) | batset2_org);
1452 RICOH_FG_DBG(KERN_INFO "PMU: %s *** Temperature: %d, vfchg: %d, SW status: %d, chg_status: %d ******\n",
1453 __func__, temp, vfchg, info->soca->status, info->soca->chg_status);
1455 if (temp <= 0 || 55 <= temp) {
1456 /* 1st and 5th temperature ranges (~0, 55~) */
1457 RICOH_FG_DBG(KERN_INFO "PMU: %s *** Temp(%d) is out of 0-55 ******\n", __func__, temp);
1458 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
1460 dev_err(info->dev, "Error in writing the control register\n");
1463 info->soca->jt_limit = 0;
1464 *is_jeita_updated = true;
1465 } else if (temp < info->jt_temp_l) {
1466 /* 2nd temperature range (0~12) */
1467 if (vfchg != info->jt_vfchg_h) {
1468 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 0<Temp<12, update to vfchg=%d ******\n",
1469 __func__, info->jt_vfchg_h);
1470 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
1472 dev_err(info->dev, "Error in writing the control register\n");
1476 /* set VFCHG/VRCHG */
1477 err = ricoh619_write(info->dev->parent,
1478 BATSET2_REG, set_vchg_h);
1480 dev_err(info->dev, "Error in writing the battery setting register\n");
1483 info->soca->jt_limit = 0;
1484 *is_jeita_updated = true;
1486 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 0<Temp<50, already set vfchg=%d, so no need to update ******\n",
1487 __func__, info->jt_vfchg_h);
1490 err = ricoh619_write(info->dev->parent, CHGISET_REG, set_ichg_l);
1492 dev_err(info->dev, "Error in writing the battery setting register\n");
1495 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x03);
1497 dev_err(info->dev, "Error in writing the control register\n");
1500 } else if (temp < info->jt_temp_h) {
1501 /* 3rd temperature range (12~50) */
1502 if (vfchg != info->jt_vfchg_h) {
1503 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 12<Temp<50, update to vfchg==%d ******\n", __func__, info->jt_vfchg_h);
1505 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
1507 dev_err(info->dev, "Error in writing the control register\n");
1510 /* set VFCHG/VRCHG */
1511 err = ricoh619_write(info->dev->parent,
1512 BATSET2_REG, set_vchg_h);
1514 dev_err(info->dev, "Error in writing the battery setting register\n");
1517 info->soca->jt_limit = 0;
1519 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 12<Temp<50, already set vfchg==%d, so no need to update ******\n",
1520 __func__, info->jt_vfchg_h);
1523 err = ricoh619_write(info->dev->parent, CHGISET_REG, set_ichg_h);
1525 dev_err(info->dev, "Error in writing the battery setting register\n");
1528 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x03);
1530 dev_err(info->dev, "Error in writing the control register\n");
1533 } else if (temp < 55) {
1534 /* 4th temperature range (50~55) */
1535 if (vfchg != info->jt_vfchg_l) {
1536 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 50<Temp<55, update to vfchg==%d ******\n", __func__, info->jt_vfchg_l);
1538 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
1540 dev_err(info->dev, "Error in writing the control register\n");
1543 /* set VFCHG/VRCHG */
1544 err = ricoh619_write(info->dev->parent,
1545 BATSET2_REG, set_vchg_l);
1547 dev_err(info->dev, "Error in writing the battery setting register\n");
1550 info->soca->jt_limit = 1;
1551 *is_jeita_updated = true;
1553 RICOH_FG_DBG(KERN_INFO "JEITA: %s *** 50<Temp<55, already set vfchg==%d, so no need to update ******\n",
1554 __func__, info->jt_vfchg_l);
1557 err = ricoh619_write(info->dev->parent, CHGISET_REG, set_ichg_h);
1559 dev_err(info->dev, "Error in writing the battery setting register\n");
1562 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x03);
1564 dev_err(info->dev, "Error in writing the control register\n");
1569 get_power_supply_status(info);
1570 RICOH_FG_DBG(KERN_INFO "PMU: %s *** Hope updating value in this timing after checking jeita, chg_status: %d, is_jeita_updated: %d ******\n",
1571 __func__, info->soca->chg_status, *is_jeita_updated);
1576 RICOH_FG_DBG(KERN_INFO "PMU: %s ERROR ******\n", __func__);
1580 static void ricoh619_jeita_work(struct work_struct *work)
1583 bool is_jeita_updated = false;
1584 struct ricoh619_battery_info *info = container_of(work,
1585 struct ricoh619_battery_info, jeita_work.work);
1587 mutex_lock(&info->lock);
1589 ret = check_jeita_status(info, &is_jeita_updated);
1591 queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
1592 RICOH619_JEITA_UPDATE_TIME * HZ);
1594 RICOH_FG_DBG(KERN_INFO "PMU: %s *** Call check_jeita_status() in jeita_work, err:%d ******\n",
1596 queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
1597 RICOH619_FG_RESET_TIME * HZ);
1600 mutex_unlock(&info->lock);
1602 if(true == is_jeita_updated)
1603 power_supply_changed(&info->battery);
1608 #ifdef ENABLE_FACTORY_MODE
1609 /*------------------------------------------------------*/
1611 /* Check Battery exist or not */
1612 /* If not, disabled Rapid to Complete State change */
1613 /*------------------------------------------------------*/
1614 static int ricoh619_factory_mode(struct ricoh619_battery_info *info)
1619 ret = ricoh619_read(info->dev->parent, RICOH619_INT_MON_CHGCTR, &val);
1621 dev_err(info->dev, "Error in reading the control register\n");
1624 if (!(val & 0x01)) /* No Adapter connected */
1627 /* Rapid to Complete State change disable */
1628 ret = ricoh619_set_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x40);
1630 dev_err(info->dev, "Error in writing the control register\n");
1634 /* Wait 1s for checking Charging State */
1635 queue_delayed_work(info->factory_mode_wqueue, &info->factory_mode_work,
1641 static void check_charging_state_work(struct work_struct *work)
1643 struct ricoh619_battery_info *info = container_of(work,
1644 struct ricoh619_battery_info, factory_mode_work.work);
1648 int chargeCurrent = 0;
1650 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &val);
1652 dev_err(info->dev, "Error in reading the control register\n");
1657 chargeCurrent = get_check_fuel_gauge_reg(info, CC_AVERAGE1_REG,
1658 CC_AVERAGE0_REG, 0x3fff);
1659 if (chargeCurrent < 0) {
1660 dev_err(info->dev, "Error in reading the FG register\n");
1664 /* Repid State && Charge Current about 0mA */
1665 if (((chargeCurrent >= 0x3ffc && chargeCurrent <= 0x3fff)
1666 || chargeCurrent < 0x05) && val == 0x43) {
1667 RICOH_FG_DBG("PMU:%s --- No battery !! Enter Factory mode ---\n"
1669 info->entry_factory_mode = true;
1670 /* clear FG_ACC bit */
1671 ret = ricoh619_clr_bits(info->dev->parent, RICOH619_FG_CTRL, 0x10);
1673 dev_err(info->dev, "Error in writing FG_CTRL\n");
1675 return; /* Factory Mode */
1678 /* Return Normal Mode --> Rapid to Complete State change enable */
1679 ret = ricoh619_clr_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x40);
1681 dev_err(info->dev, "Error in writing the control register\n");
1684 RICOH_FG_DBG("PMU:%s --- Battery exist !! Return Normal mode ---0x%2x\n"
1689 #endif /* ENABLE_FACTORY_MODE */
1691 static int Calc_Linear_Interpolation(int x0, int y0, int x1, int y1, int y)
1696 alpha = (y - y0)*100 / (y1 - y0);
1698 x = ((100 - alpha) * x0 + alpha * x1) / 100;
1703 static int ricoh619_set_OCV_table(struct ricoh619_battery_info *info)
1712 int OCV_percent_new[11];
1716 info->soca->target_vsys = info->fg_target_vsys;
1717 info->soca->target_ibat = info->fg_target_ibat;
1720 RICOH_FG_DBG("PMU : %s : target_vsys is %d target_ibat is %d",__func__,info->soca->target_vsys,info->soca->target_ibat);
1722 if ((info->soca->target_ibat == 0) || (info->soca->target_vsys == 0)) { /* normal version */
1723 } else { /*Slice cutoff voltage version. */
1724 /* get ocv table. this table is calculated by Apprication */
1725 for (i = 0; i <= 10; i = i+1) {
1726 temp = (battery_init_para[info->num][i*2]<<8)
1727 | (battery_init_para[info->num][i*2+1]);
1728 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
1729 temp = ((temp * 50000 * 10 / 4095) + 5) / 10;
1730 ocv_table[i] = temp;
1734 /* get internal impedence */
1735 temp = (battery_init_para[info->num][24]<<8) | (battery_init_para[info->num][25]);
1736 Rbat = temp * 1000 / 512 * 5000 / 4095;
1738 Ibat_min = -1 * info->soca->target_ibat;
1739 info->soca->Rsys = Rbat + 55;
1740 info->soca->cutoff_ocv = info->soca->target_vsys - Ibat_min * info->soca->Rsys / 1000;
1743 RICOH_FG_DBG("PMU: ------- Rbat= %d: Rsys= %d: cutoff_ocv= %d: =======\n",
1744 Rbat, info->soca->Rsys, info->soca->cutoff_ocv);
1747 for (i = 1; i < 11; i++) {
1748 if (ocv_table[i] >= info->soca->cutoff_ocv * 10) {
1749 /* unit is 0.001% */
1750 start_par = Calc_Linear_Interpolation(
1751 (i-1)*1000, ocv_table[i-1], i*1000,
1752 ocv_table[i], (info->soca->cutoff_ocv * 10));
1756 /* calc new ocv percent */
1757 percent_step = (10000 - start_par) / 10;
1759 for (i = 0; i < 11; i++) {
1761 = start_par + percent_step*(i - 0);
1764 /* calc new ocv voltage */
1765 for (i = 0; i < 11; i++) {
1766 for (j = 1; j < 11; j++) {
1767 if (1000*j >= OCV_percent_new[i]) {
1768 temp = Calc_Linear_Interpolation(
1769 ocv_table[j-1], (j-1)*1000,
1770 ocv_table[j] , j*1000,
1771 OCV_percent_new[i]);
1773 temp = temp * 4095 / 50000;
1775 battery_init_para[info->num][i*2 + 1] = temp;
1776 battery_init_para[info->num][i*2] = temp >> 8;
1783 for (i = 0; i <= 10; i = i+1) {
1784 temp = (battery_init_para[info->num][i*2]<<8)
1785 | (battery_init_para[info->num][i*2+1]);
1786 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
1787 temp = ((temp * 50000 * 10 / 4095) + 5) / 10;
1788 RICOH_FG_DBG("PMU: ------- ocv_table[%d]= %d: =======\n",
1793 /* calc available capacity */
1794 /* get avilable capacity */
1795 /* battery_init_para23-24 is designe capacity */
1796 available_cap = (battery_init_para[info->num][22]<<8)
1797 | (battery_init_para[info->num][23]);
1799 available_cap = available_cap
1800 * ((10000 - start_par) / 100) / 100 ;
1803 battery_init_para[info->num][23] = available_cap;
1804 battery_init_para[info->num][22] = available_cap >> 8;
1807 ret = ricoh619_bulk_writes_bank1(info->dev->parent,
1808 BAT_INIT_TOP_REG, 32, battery_init_para[info->num]);
1810 dev_err(info->dev, "batterry initialize error\n");
1817 /* Initial setting of battery */
1818 static int ricoh619_init_battery(struct ricoh619_battery_info *info)
1823 /* Need to implement initial setting of batery and error */
1824 /* -------------------------- */
1825 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
1827 /* set kanwa state */
1828 if (RICOH619_REL1_SEL_VALUE > 240)
1831 val = RICOH619_REL1_SEL_VALUE / 16 ;
1833 /* set kanwa state */
1834 if (RICOH619_REL2_SEL_VALUE > 120)
1837 val2 = RICOH619_REL2_SEL_VALUE / 8 ;
1839 val = val + (val2 << 4);
1841 ret = ricoh619_write_bank1(info->dev->parent, BAT_REL_SEL_REG, val);
1843 dev_err(info->dev, "Error in writing BAT_REL_SEL_REG\n");
1847 ret = ricoh619_read_bank1(info->dev->parent, BAT_REL_SEL_REG, &val);
1848 RICOH_FG_DBG("PMU: ------- BAT_REL_SEL= %xh: =======\n",
1851 ret = ricoh619_write_bank1(info->dev->parent, BAT_TA_SEL_REG, 0x00);
1853 dev_err(info->dev, "Error in writing BAT_TA_SEL_REG\n");
1857 ret = ricoh619_read(info->dev->parent, FG_CTRL_REG, &val);
1859 dev_err(info->dev, "Error in reading the control register\n");
1863 val = (val & 0x10) >> 4;
1864 info->first_pwon = (val == 0) ? 1 : 0;
1866 ret = ricoh619_set_OCV_table(info);
1868 dev_err(info->dev, "Error in writing the OCV Tabler\n");
1872 ret = ricoh619_write(info->dev->parent, FG_CTRL_REG, 0x11);
1874 dev_err(info->dev, "Error in writing the control register\n");
1880 ret = ricoh619_write(info->dev->parent, VINDAC_REG, 0x01);
1882 dev_err(info->dev, "Error in writing the control register\n");
1886 if (info->alarm_vol_mv < 2700 || info->alarm_vol_mv > 3400) {
1887 dev_err(info->dev, "alarm_vol_mv is out of range!\n");
1894 /* Initial setting of charger */
1895 static int ricoh619_init_charger(struct ricoh619_battery_info *info)
1904 info->chg_stat1 = 0;
1906 err = ricoh619_set_bits(info->dev->parent, RICOH619_PWR_FUNC, 0x20);
1908 dev_err(info->dev, "Error in writing the PWR FUNC register\n");
1912 charge_status = get_power_supply_status(info);
1914 if (charge_status != POWER_SUPPLY_STATUS_FULL)
1916 /* Disable charging */
1917 err = ricoh619_clr_bits(info->dev->parent,CHGCTL1_REG, 0x03);
1919 dev_err(info->dev, "Error in writing the control register\n");
1925 err = ricoh619_read(info->dev->parent, REGISET1_REG,&val);
1926 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);
1928 /* REGISET1:(0xB6) setting */
1929 if ((info->ch_ilim_adp != 0xFF) || (info->ch_ilim_adp <= 0x1D)) {
1930 val = info->ch_ilim_adp;
1932 err = ricoh619_write(info->dev->parent, REGISET1_REG,val);
1934 dev_err(info->dev, "Error in writing REGISET1_REG %d\n",
1941 err = ricoh619_read(info->dev->parent, REGISET1_REG,&val);
1942 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);
1945 err = ricoh619_read(info->dev->parent, REGISET2_REG,&val);
1946 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);
1948 /* REGISET2:(0xB7) setting */
1949 err = ricoh619_read(info->dev->parent, REGISET2_REG, &val);
1952 "Error in read REGISET2_REG %d\n", err);
1956 if ((info->ch_ilim_usb != 0xFF) || (info->ch_ilim_usb <= 0x1D)) {
1957 val2 = info->ch_ilim_usb;
1958 } else {/* Keep OTP value */
1959 val2 = (val & 0x1F);
1967 err = ricoh619_write(info->dev->parent, REGISET2_REG,val);
1969 dev_err(info->dev, "Error in writing REGISET2_REG %d\n",
1975 err = ricoh619_read(info->dev->parent, REGISET2_REG,&val);
1976 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);
1978 /* CHGISET_REG(0xB8) setting */
1980 err = ricoh619_read(info->dev->parent, CHGISET_REG,&val);
1981 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);
1983 err = ricoh619_read(info->dev->parent, CHGISET_REG, &val);
1986 "Error in read CHGISET_REG %d\n", err);
1990 /* Define Current settings value for charging (bit 4~0)*/
1991 if ((info->ch_ichg != 0xFF) || (info->ch_ichg <= 0x1D)) {
1992 val2 = info->ch_ichg;
1993 } else { /* Keep OTP value */
1994 val2 = (val & 0x1F);
1997 /* Define Current settings at the charge completion (bit 7~6)*/
1998 if ((info->ch_icchg != 0xFF) || (info->ch_icchg <= 0x03)) {
1999 val3 = info->ch_icchg << 6;
2000 } else { /* Keep OTP value */
2006 err = ricoh619_write(info->dev->parent, CHGISET_REG, val);
2008 dev_err(info->dev, "Error in writing CHGISET_REG %d\n",
2014 err = ricoh619_read(info->dev->parent, CHGISET_REG,&val);
2015 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);
2018 err = ricoh619_read(info->dev->parent, BATSET1_REG,&val);
2019 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);
2021 /* BATSET1_REG(0xBA) setting */
2022 err = ricoh619_read(info->dev->parent, BATSET1_REG, &val);
2025 "Error in read BATSET1 register %d\n", err);
2029 /* Define Battery overvoltage (bit 4)*/
2030 if ((info->ch_vbatovset != 0xFF) || (info->ch_vbatovset <= 0x1)) {
2031 val2 = info->ch_vbatovset;
2033 } else { /* Keep OTP value */
2034 val2 = (val & 0x10);
2037 /* keep bit 0-3 and bit 5-7 */
2042 err = ricoh619_write(info->dev->parent, BATSET1_REG, val);
2044 dev_err(info->dev, "Error in writing BAT1_REG %d\n",
2049 err = ricoh619_read(info->dev->parent, BATSET1_REG,&val);
2050 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);
2053 err = ricoh619_read(info->dev->parent, BATSET2_REG,&val);
2054 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);
2057 /* BATSET2_REG(0xBB) setting */
2058 err = ricoh619_read(info->dev->parent, BATSET2_REG, &val);
2061 "Error in read BATSET2 register %d\n", err);
2065 /* Define Re-charging voltage (bit 2~0)*/
2066 if ((info->ch_vrchg != 0xFF) || (info->ch_vrchg <= 0x04)) {
2067 val2 = info->ch_vrchg;
2068 } else { /* Keep OTP value */
2069 val2 = (val & 0x07);
2072 /* Define FULL charging voltage (bit 6~4)*/
2073 if ((info->ch_vfchg != 0xFF) || (info->ch_vfchg <= 0x04)) {
2074 val3 = info->ch_vfchg;
2076 } else { /* Keep OTP value */
2077 val3 = (val & 0x70);
2080 /* keep bit 3 and bit 7 */
2083 val = val + val2 + val3;
2085 err = ricoh619_write(info->dev->parent, BATSET2_REG, val);
2087 dev_err(info->dev, "Error in writing RICOH619_RE_CHARGE_VOLTAGE %d\n",
2093 err = ricoh619_read(info->dev->parent, BATSET2_REG,&val);
2094 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);
2096 /* Set rising edge setting ([1:0]=01b)for INT in charging */
2097 /* and rising edge setting ([3:2]=01b)for charge completion */
2098 err = ricoh619_read(info->dev->parent, RICOH619_CHG_STAT_DETMOD1, &val);
2100 dev_err(info->dev, "Error in reading CHG_STAT_DETMOD1 %d\n",
2106 err = ricoh619_write(info->dev->parent, RICOH619_CHG_STAT_DETMOD1, val);
2108 dev_err(info->dev, "Error in writing CHG_STAT_DETMOD1 %d\n",
2113 /* Unmask In charging/charge completion */
2114 err = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGSTS1, 0xfc);
2116 dev_err(info->dev, "Error in writing INT_MSK_CHGSTS1 %d\n",
2121 /* Set both edge for VUSB([3:2]=11b)/VADP([1:0]=11b) detect */
2122 err = ricoh619_read(info->dev->parent, RICOH619_CHG_CTRL_DETMOD1, &val);
2124 dev_err(info->dev, "Error in reading CHG_CTRL_DETMOD1 %d\n",
2130 err = ricoh619_write(info->dev->parent, RICOH619_CHG_CTRL_DETMOD1, val);
2132 dev_err(info->dev, "Error in writing CHG_CTRL_DETMOD1 %d\n",
2137 /* Unmask In VUSB/VADP completion */
2138 err = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGCTR, 0xfc);
2140 dev_err(info->dev, "Error in writing INT_MSK_CHGSTS1 %d\n",
2145 if (charge_status != POWER_SUPPLY_STATUS_FULL)
2147 /* Enable charging */
2148 err = ricoh619_set_bits(info->dev->parent,CHGCTL1_REG, 0x03);
2150 dev_err(info->dev, "Error in writing the control register\n");
2155 #ifdef ENABLE_LOW_BATTERY_DETECTION
2156 /* Set ADRQ=00 to stop ADC */
2157 ricoh619_write(info->dev->parent, RICOH619_ADC_CNT3, 0x0);
2158 /* Enable VSYS threshold Low interrupt */
2159 ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
2160 /* Set ADC auto conversion interval 250ms */
2161 ricoh619_write(info->dev->parent, RICOH619_ADC_CNT2, 0x0);
2162 /* Enable VSYS pin conversion in auto-ADC */
2163 ricoh619_write(info->dev->parent, RICOH619_ADC_CNT1, 0x10);
2164 /* Set VSYS threshold low voltage = 3.50v */
2165 ricoh619_write(info->dev->parent, RICOH619_ADC_VSYS_THL, 0x77);
2166 /* Start auto-mode & average 4-time conversion mode for ADC */
2167 ricoh619_write(info->dev->parent, RICOH619_ADC_CNT3, 0x28);
2168 /* Enable master ADC INT */
2169 ricoh619_set_bits(info->dev->parent, RICOH619_INTC_INTEN, ADC_INT);
2177 static int get_power_supply_status(struct ricoh619_battery_info *info)
2180 uint8_t supply_state;
2181 uint8_t charge_state;
2184 /* get power supply status */
2185 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &status);
2187 dev_err(info->dev, "Error in reading the control register\n");
2191 charge_state = (status & 0x1F);
2192 supply_state = ((status & 0xC0) >> 6);
2194 if (info->entry_factory_mode)
2195 return POWER_SUPPLY_STATUS_NOT_CHARGING;
2197 if (supply_state == SUPPLY_STATE_BAT) {
2198 info->soca->chg_status = POWER_SUPPLY_STATUS_DISCHARGING;
2200 switch (charge_state) {
2201 case CHG_STATE_CHG_OFF:
2202 info->soca->chg_status
2203 = POWER_SUPPLY_STATUS_DISCHARGING;
2205 case CHG_STATE_CHG_READY_VADP:
2206 info->soca->chg_status
2207 = POWER_SUPPLY_STATUS_NOT_CHARGING;
2209 case CHG_STATE_CHG_TRICKLE:
2210 info->soca->chg_status
2211 = POWER_SUPPLY_STATUS_CHARGING;
2213 case CHG_STATE_CHG_RAPID:
2214 info->soca->chg_status
2215 = POWER_SUPPLY_STATUS_CHARGING;
2217 case CHG_STATE_CHG_COMPLETE:
2218 info->soca->chg_status
2219 = POWER_SUPPLY_STATUS_FULL;
2221 case CHG_STATE_SUSPEND:
2222 info->soca->chg_status
2223 = POWER_SUPPLY_STATUS_DISCHARGING;
2225 case CHG_STATE_VCHG_OVER_VOL:
2226 info->soca->chg_status
2227 = POWER_SUPPLY_STATUS_DISCHARGING;
2229 case CHG_STATE_BAT_ERROR:
2230 info->soca->chg_status
2231 = POWER_SUPPLY_STATUS_NOT_CHARGING;
2233 case CHG_STATE_NO_BAT:
2234 info->soca->chg_status
2235 = POWER_SUPPLY_STATUS_NOT_CHARGING;
2237 case CHG_STATE_BAT_OVER_VOL:
2238 info->soca->chg_status
2239 = POWER_SUPPLY_STATUS_NOT_CHARGING;
2241 case CHG_STATE_BAT_TEMP_ERR:
2242 info->soca->chg_status
2243 = POWER_SUPPLY_STATUS_NOT_CHARGING;
2245 case CHG_STATE_DIE_ERR:
2246 info->soca->chg_status
2247 = POWER_SUPPLY_STATUS_NOT_CHARGING;
2249 case CHG_STATE_DIE_SHUTDOWN:
2250 info->soca->chg_status
2251 = POWER_SUPPLY_STATUS_DISCHARGING;
2253 case CHG_STATE_NO_BAT2:
2254 info->soca->chg_status
2255 = POWER_SUPPLY_STATUS_NOT_CHARGING;
2257 case CHG_STATE_CHG_READY_VUSB:
2258 info->soca->chg_status
2259 = POWER_SUPPLY_STATUS_NOT_CHARGING;
2262 info->soca->chg_status
2263 = POWER_SUPPLY_STATUS_UNKNOWN;
2268 return info->soca->chg_status;
2271 static void charger_irq_work(struct work_struct *work)
2273 struct ricoh619_battery_info *info
2274 = container_of(work, struct ricoh619_battery_info, irq_work);
2277 RICOH_FG_DBG("PMU:%s In\n", __func__);
2279 power_supply_changed(&info->battery);
2281 mutex_lock(&info->lock);
2283 if (info->chg_stat1 & 0x01) {
2284 ricoh619_read(info->dev->parent, CHGSTATE_REG, ®_val);
2285 if (reg_val & 0x40) { /* USE ADP */
2286 /* set adp limit current 2A */
2287 ricoh619_write(info->dev->parent, REGISET1_REG, 0x13);
2288 /* set charge current 2A */
2289 ricoh619_write(info->dev->parent, CHGISET_REG, 0xD3);
2291 else if (reg_val & 0x80) { /* USE USB */
2292 queue_work(info->usb_workqueue, &info->usb_irq_work);
2296 info->chg_stat1 = 0;
2298 /* Enable Interrupt for VADP/USB */
2299 ret = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGCTR, 0xfc);
2302 "%s(): Error in enable charger mask INT %d\n",
2305 /* Enable Interrupt for Charging & complete */
2306 ret = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGSTS1, 0xfc);
2309 "%s(): Error in enable charger mask INT %d\n",
2312 mutex_unlock(&info->lock);
2313 RICOH_FG_DBG("PMU:%s Out\n", __func__);
2316 #ifdef ENABLE_LOW_BATTERY_DETECTION
2317 static void low_battery_irq_work(struct work_struct *work)
2319 struct ricoh619_battery_info *info = container_of(work,
2320 struct ricoh619_battery_info, low_battery_work.work);
2324 RICOH_FG_DBG("PMU:%s In\n", __func__);
2326 power_supply_changed(&info->battery);
2328 /* Enable VADP threshold Low interrupt */
2329 ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
2332 "%s(): Error in enable adc mask INT %d\n",
2338 static void ricoh619_usb_charge_det(void)
2340 struct ricoh619 *ricoh619 = g_ricoh619;
2341 ricoh619_set_bits(ricoh619->dev,REGISET2_REG,(1 << 7)); //set usb limit current when SDP or other mode
2342 if(2 == dwc_vbus_status()){
2343 ricoh619_write(ricoh619->dev,REGISET2_REG,0x13); //set usb limit current 2A
2344 ricoh619_write(ricoh619->dev,CHGISET_REG,0xD3); //set charge current 2A
2346 else if(1 == dwc_vbus_status()){
2347 ricoh619_write(ricoh619->dev,REGISET2_REG,0x04); //set usb limit current 500ma
2348 ricoh619_write(ricoh619->dev,CHGISET_REG,0xC4); //set charge current 500ma
2352 static void usb_det_irq_work(struct work_struct *work)
2354 struct ricoh619_battery_info *info = container_of(work,
2355 struct ricoh619_battery_info, usb_irq_work);
2360 RICOH_FG_DBG("PMU:%s In\n", __func__);
2362 power_supply_changed(&info->battery);
2364 mutex_lock(&info->lock);
2366 /* Enable Interrupt for VUSB */
2367 ret = ricoh619_clr_bits(info->dev->parent,
2368 RICOH619_INT_MSK_CHGCTR, 0x02);
2371 "%s(): Error in enable charger mask INT %d\n",
2374 mutex_unlock(&info->lock);
2375 ret = ricoh619_read(info->dev->parent, RICOH619_INT_MON_CHGCTR, &sts);
2377 dev_err(info->dev, "Error in reading the control register\n");
2383 //ricoh619_usb_charge_det();
2389 /*********************/
2391 /*********************/
2394 RICOH_FG_DBG("PMU:%s Out\n", __func__);
2397 extern void rk28_send_wakeup_key(void);
2398 static irqreturn_t charger_in_isr(int irq, void *battery_info)
2400 struct ricoh619_battery_info *info = battery_info;
2401 struct ricoh619 *ricoh619 = g_ricoh619;
2403 RICOH_FG_DBG("PMU:%s\n", __func__);
2405 info->chg_stat1 |= 0x01;
2407 queue_work(info->workqueue, &info->irq_work);
2408 // rk28_send_wakeup_key();
2413 static irqreturn_t charger_complete_isr(int irq, void *battery_info)
2415 struct ricoh619_battery_info *info = battery_info;
2416 RICOH_FG_DBG("PMU:%s\n", __func__);
2418 info->chg_stat1 |= 0x02;
2419 queue_work(info->workqueue, &info->irq_work);
2420 rk28_send_wakeup_key();
2425 static irqreturn_t charger_usb_isr(int irq, void *battery_info)
2427 struct ricoh619_battery_info *info = battery_info;
2428 RICOH_FG_DBG("PMU:%s\n", __func__);
2430 info->chg_ctr |= 0x02;
2432 queue_work(info->workqueue, &info->irq_work);
2434 info->soca->dischg_state = 0;
2435 info->soca->chg_count = 0;
2437 // queue_work(info->usb_workqueue, &info->usb_irq_work);
2438 // rk28_send_wakeup_key();
2440 if (RICOH619_SOCA_UNSTABLE == info->soca->status
2441 || RICOH619_SOCA_FG_RESET == info->soca->status)
2442 info->soca->stable_count = 11;
2447 static irqreturn_t charger_adp_isr(int irq, void *battery_info)
2449 struct ricoh619_battery_info *info = battery_info;
2450 struct ricoh619 *ricoh619 = g_ricoh619;
2451 RICOH_FG_DBG("PMU:%s\n", __func__);
2453 info->chg_ctr |= 0x01;
2454 queue_work(info->workqueue, &info->irq_work);
2455 rk28_send_wakeup_key();
2457 info->soca->dischg_state = 0;
2458 info->soca->chg_count = 0;
2459 if (RICOH619_SOCA_UNSTABLE == info->soca->status
2460 || RICOH619_SOCA_FG_RESET == info->soca->status)
2461 info->soca->stable_count = 11;
2467 #ifdef ENABLE_LOW_BATTERY_DETECTION
2468 /*************************************************************/
2469 /* for Detecting Low Battery */
2470 /*************************************************************/
2472 static irqreturn_t adc_vsysl_isr(int irq, void *battery_info)
2475 struct ricoh619_battery_info *info = battery_info;
2478 RICOH_FG_DBG("PMU:%s\n", __func__);
2480 queue_delayed_work(info->monitor_wqueue, &info->low_battery_work,
2481 LOW_BATTERY_DETECTION_TIME*HZ);
2485 RICOH_FG_DBG("PMU:%s\n", __func__);
2486 // ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
2487 // rk28_send_wakeup_key();
2494 * Get Charger Priority
2495 * - get higher-priority between VADP and VUSB
2496 * @ data: higher-priority is stored
2500 static int get_charge_priority(struct ricoh619_battery_info *info, bool *data)
2505 ret = ricoh619_read(info->dev->parent, CHGCTL1_REG, &val);
2513 * Set Charger Priority
2514 * - set higher-priority between VADP and VUSB
2515 * - data: higher-priority is stored
2519 static int set_charge_priority(struct ricoh619_battery_info *info, bool *data)
2527 ret = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, val);
2531 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
2532 static int get_check_fuel_gauge_reg(struct ricoh619_battery_info *info,
2533 int Reg_h, int Reg_l, int enable_bit)
2535 uint8_t get_data_h, get_data_l;
2536 int old_data, current_data;
2542 for (i = 0; i < 5 ; i++) {
2543 ret = ricoh619_read(info->dev->parent, Reg_h, &get_data_h);
2545 dev_err(info->dev, "Error in reading the control register\n");
2549 ret = ricoh619_read(info->dev->parent, Reg_l, &get_data_l);
2551 dev_err(info->dev, "Error in reading the control register\n");
2555 current_data = ((get_data_h & 0xff) << 8) | (get_data_l & 0xff);
2556 current_data = (current_data & enable_bit);
2558 if (current_data == old_data)
2559 return current_data;
2561 old_data = current_data;
2564 return current_data;
2567 static int calc_capacity(struct ricoh619_battery_info *info)
2575 temperature = get_battery_temp(info) / 10; /* unit 0.1 degree -> 1 degree */
2577 if (temperature >= 25) {
2579 } else if (temperature >= 5) {
2580 nt = (25 - temperature) * RICOH619_TAH_SEL2 * 625 / 100;
2582 nt = (625 + (5 - temperature) * RICOH619_TAL_SEL2 * 625 / 100);
2585 /* get remaining battery capacity from fuel gauge */
2586 ret = ricoh619_read(info->dev->parent, SOC_REG, &capacity);
2588 dev_err(info->dev, "Error in reading the control register\n");
2592 temp = capacity * 100 * 100 / (10000 - nt);
2594 return temp; /* Unit is 1% */
2597 static int get_battery_temp(struct ricoh619_battery_info *info)
2602 ret = get_check_fuel_gauge_reg(info, TEMP_1_REG, TEMP_2_REG, 0x0fff);
2604 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
2608 /* bit3 of 0xED(TEMP_1) is sign_bit */
2609 sign_bit = ((ret & 0x0800) >> 11);
2611 ret = (ret & 0x07ff);
2613 if (sign_bit == 0) /* positive value part */
2614 /* conversion unit */
2615 /* 1 unit is 0.0625 degree and retun unit
2616 * should be 0.1 degree,
2618 ret = ret * 625 / 1000;
2619 else { /*negative value part */
2620 ret = (~ret + 1) & 0x7ff;
2621 ret = -1 * ret * 625 / 1000;
2627 static int get_time_to_empty(struct ricoh619_battery_info *info)
2631 ret = get_check_fuel_gauge_reg(info, TT_EMPTY_H_REG, TT_EMPTY_L_REG,
2634 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
2638 /* conversion unit */
2639 /* 1unit is 1miniute and return nnit should be 1 second */
2645 static int get_time_to_full(struct ricoh619_battery_info *info)
2649 ret = get_check_fuel_gauge_reg(info, TT_FULL_H_REG, TT_FULL_L_REG,
2652 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
2661 /* battery voltage is get from Fuel gauge */
2662 static int measure_vbatt_FG(struct ricoh619_battery_info *info, int *data)
2666 ret = get_check_fuel_gauge_reg(info, VOLTAGE_1_REG, VOLTAGE_2_REG,
2669 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
2674 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
2675 *data = *data * 50000 / 4095;
2676 /* return unit should be 1uV */
2677 *data = *data * 100;
2682 static int measure_Ibatt_FG(struct ricoh619_battery_info *info, int *data)
2686 ret = get_check_fuel_gauge_reg(info, CC_AVERAGE1_REG,
2687 CC_AVERAGE0_REG, 0x3fff);
2689 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
2693 *data = (ret > 0x1fff) ? (ret - 0x4000) : ret;
2697 static int get_OCV_init_Data(struct ricoh619_battery_info *info, int index)
2700 ret = (battery_init_para[info->num][index*2]<<8) | (battery_init_para[info->num][index*2+1]);
2704 static int get_OCV_voltage(struct ricoh619_battery_info *info, int index)
2707 ret = get_OCV_init_Data(info, index);
2708 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
2709 ret = ret * 50000 / 4095;
2710 /* return unit should be 1uV */
2716 /* battery voltage is get from ADC */
2717 static int measure_vbatt_ADC(struct ricoh619_battery_info *info, int *data)
2720 uint8_t data_l = 0, data_h = 0;
2723 /* ADC interrupt enable */
2724 ret = ricoh619_set_bits(info->dev->parent, INTEN_REG, 0x08);
2726 dev_err(info->dev, "Error in setting the control register bit\n");
2730 /* enable interrupt request of single mode */
2731 ret = ricoh619_set_bits(info->dev->parent, EN_ADCIR3_REG, 0x01);
2733 dev_err(info->dev, "Error in setting the control register bit\n");
2737 /* single request */
2738 ret = ricoh619_write(info->dev->parent, ADCCNT3_REG, 0x10);
2740 dev_err(info->dev, "Error in writing the control register\n");
2744 for (i = 0; i < 5; i++) {
2746 RICOH_FG_DBG("ADC conversion times: %d\n", i);
2747 /* read completed flag of ADC */
2748 ret = ricoh619_read(info->dev->parent, EN_ADCIR3_REG, &data_h);
2750 dev_err(info->dev, "Error in reading the control register\n");
2758 dev_err(info->dev, "ADC conversion too long!\n");
2762 ret = ricoh619_read(info->dev->parent, VBATDATAH_REG, &data_h);
2764 dev_err(info->dev, "Error in reading the control register\n");
2768 ret = ricoh619_read(info->dev->parent, VBATDATAL_REG, &data_l);
2770 dev_err(info->dev, "Error in reading the control register\n");
2774 *data = ((data_h & 0xff) << 4) | (data_l & 0x0f);
2775 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
2776 *data = *data * 5000 / 4095;
2777 /* return unit should be 1uV */
2778 *data = *data * 1000;
2787 static int measure_vsys_ADC(struct ricoh619_battery_info *info, int *data)
2789 uint8_t data_l = 0, data_h = 0;
2792 ret = ricoh619_read(info->dev->parent, VSYSDATAH_REG, &data_h);
2794 dev_err(info->dev, "Error in reading the control register\n");
2797 ret = ricoh619_read(info->dev->parent, VSYSDATAL_REG, &data_l);
2799 dev_err(info->dev, "Error in reading the control register\n");
2802 *data = ((data_h & 0xff) << 4) | (data_l & 0x0f);
2803 *data = *data * 1000 * 3 * 5 / 2 / 4095;
2804 /* return unit should be 1uV */
2805 *data = *data * 1000;
2810 static void ricoh619_external_power_changed(struct power_supply *psy)
2812 struct ricoh619_battery_info *info;
2814 info = container_of(psy, struct ricoh619_battery_info, battery);
2815 queue_delayed_work(info->monitor_wqueue,
2816 &info->changed_work, HZ / 2);
2821 static int ricoh619_batt_get_prop(struct power_supply *psy,
2822 enum power_supply_property psp,
2823 union power_supply_propval *val)
2825 struct ricoh619_battery_info *info = dev_get_drvdata(psy->dev->parent);
2830 mutex_lock(&info->lock);
2833 case POWER_SUPPLY_PROP_ONLINE:
2834 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &status);
2836 dev_err(info->dev, "Error in reading the control register\n");
2837 mutex_unlock(&info->lock);
2840 if (psy->type == POWER_SUPPLY_TYPE_MAINS)
2841 val->intval = (status & 0x40 ? 1 : 0);
2842 else if (psy->type == POWER_SUPPLY_TYPE_USB)
2843 val->intval = (status & 0x80 ? 1 : 0);
2845 /* this setting is same as battery driver of 584 */
2846 case POWER_SUPPLY_PROP_STATUS:
2847 ret = get_power_supply_status(info);
2850 /* RICOH_FG_DBG("Power Supply Status is %d\n",
2854 /* this setting is same as battery driver of 584 */
2855 case POWER_SUPPLY_PROP_PRESENT:
2856 val->intval = info->present;
2859 /* current voltage is get from fuel gauge */
2860 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2861 /* return real vbatt Voltage */
2862 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
2863 if (info->soca->ready_fg)
2864 ret = measure_vbatt_FG(info, &data);
2866 //val->intval = -EINVAL;
2867 data = info->cur_voltage * 1000;
2868 /* RICOH_FG_DBG( "battery voltage is not ready\n"); */
2871 ret = measure_vbatt_ADC(info, &data);
2874 /* convert unit uV -> mV */
2875 info->cur_voltage = data / 1000;
2877 RICOH_FG_DBG( "battery voltage is %d mV\n",
2881 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
2882 /* current battery capacity is get from fuel gauge */
2883 case POWER_SUPPLY_PROP_CAPACITY:
2884 if (info->entry_factory_mode){
2886 info->capacity = 100;
2887 } else if (info->soca->displayed_soc <= 0) {
2891 val->intval = (info->soca->displayed_soc + 50)/100;
2892 info->capacity = (info->soca->displayed_soc + 50)/100;
2894 /* RICOH_FG_DBG("battery capacity is %d%%\n",
2898 /* current temperature of battery */
2899 case POWER_SUPPLY_PROP_TEMP:
2900 if (info->soca->ready_fg) {
2902 val->intval = get_battery_temp(info);
2903 info->battery_temp = val->intval/10;
2904 RICOH_FG_DBG( "battery temperature is %d degree\n", info->battery_temp);
2906 val->intval = info->battery_temp * 10;
2907 /* RICOH_FG_DBG("battery temperature is not ready\n"); */
2911 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
2912 if (info->soca->ready_fg) {
2913 ret = get_time_to_empty(info);
2915 info->time_to_empty = ret/60;
2916 RICOH_FG_DBG("time of empty battery is %d minutes\n", info->time_to_empty);
2918 //val->intval = -EINVAL;
2919 val->intval = info->time_to_empty * 60;
2920 RICOH_FG_DBG("time of empty battery is %d minutes\n", info->time_to_empty);
2921 /* RICOH_FG_DBG( "time of empty battery is not ready\n"); */
2925 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
2926 if (info->soca->ready_fg) {
2927 ret = get_time_to_full(info);
2929 info->time_to_full = ret/60;
2930 RICOH_FG_DBG( "time of full battery is %d minutes\n", info->time_to_full);
2932 //val->intval = -EINVAL;
2933 val->intval = info->time_to_full * 60;
2934 RICOH_FG_DBG( "time of full battery is %d minutes\n", info->time_to_full);
2935 /* RICOH_FG_DBG("time of full battery is not ready\n"); */
2939 case POWER_SUPPLY_PROP_TECHNOLOGY:
2940 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
2944 case POWER_SUPPLY_PROP_HEALTH:
2945 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2948 case POWER_SUPPLY_PROP_CURRENT_AVG:
2949 measure_Ibatt_FG(info, &data);
2950 //RICOH_FG_DBG("average current xxxxxxxxxxxxxx %d \n", data);
2953 mutex_unlock(&info->lock);
2957 mutex_unlock(&info->lock);
2962 static enum power_supply_property ricoh619_batt_props[] = {
2963 POWER_SUPPLY_PROP_STATUS,
2964 POWER_SUPPLY_PROP_PRESENT,
2965 POWER_SUPPLY_PROP_VOLTAGE_NOW,
2966 POWER_SUPPLY_PROP_CURRENT_AVG,
2968 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
2969 POWER_SUPPLY_PROP_CAPACITY,
2970 POWER_SUPPLY_PROP_TEMP,
2971 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
2972 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
2974 POWER_SUPPLY_PROP_TECHNOLOGY,
2975 POWER_SUPPLY_PROP_HEALTH,
2978 static enum power_supply_property ricoh619_power_props[] = {
2979 POWER_SUPPLY_PROP_ONLINE,
2982 struct power_supply powerac = {
2984 .type = POWER_SUPPLY_TYPE_MAINS,
2985 .properties = ricoh619_power_props,
2986 .num_properties = ARRAY_SIZE(ricoh619_power_props),
2987 .get_property = ricoh619_batt_get_prop,
2990 struct power_supply powerusb = {
2992 .type = POWER_SUPPLY_TYPE_USB,
2993 .properties = ricoh619_power_props,
2994 .num_properties = ARRAY_SIZE(ricoh619_power_props),
2995 .get_property = ricoh619_batt_get_prop,
2998 static __devinit int ricoh619_battery_probe(struct platform_device *pdev)
3000 struct ricoh619_battery_info *info;
3001 struct ricoh619_battery_platform_data *pdata;
3005 RICOH_FG_DBG("PMU: %s\n", __func__);
3007 info = kzalloc(sizeof(struct ricoh619_battery_info), GFP_KERNEL);
3010 info->soca = kzalloc(sizeof(struct ricoh619_soca_info), GFP_KERNEL);
3014 info->dev = &pdev->dev;
3015 info->status = POWER_SUPPLY_STATUS_CHARGING;
3016 pdata = pdev->dev.platform_data;
3017 info->monitor_time = pdata->monitor_time * HZ;
3018 info->alarm_vol_mv = pdata->alarm_vol_mv;
3020 type_n = Battery_Type();
3021 info->num = Battery_Table();
3022 temp = sizeof(battery_init_para)/(sizeof(uint8_t)*32);
3023 RICOH_FG_DBG("%s temp=%d\n", __func__, temp);
3024 if(info->num >= (sizeof(battery_init_para)/(sizeof(uint8_t)*32)))
3026 RICOH_FG_DBG("%s type_n=%d\n", __func__, type_n);
3027 RICOH_FG_DBG("%s info->num=%d\n", __func__, info->num);
3028 /* these valuse are set in platform */
3031 info->ch_vfchg = pdata->ch_vfchg;
3032 info->ch_vrchg = pdata->ch_vrchg;
3033 info->ch_vbatovset = pdata->ch_vbatovset;
3034 info->ch_ichg = pdata->ch_ichg;
3035 info->ch_ilim_adp = pdata->ch_ilim_adp;
3036 info->ch_ilim_usb = pdata->ch_ilim_usb;
3037 info->ch_icchg = pdata->ch_icchg;
3038 info->fg_target_vsys = pdata->fg_target_vsys;
3039 info->fg_target_ibat = pdata->fg_target_ibat;
3040 info->jt_en = pdata->jt_en;
3041 info->jt_hw_sw = pdata->jt_hw_sw;
3042 info->jt_temp_h = pdata->jt_temp_h;
3043 info->jt_temp_l = pdata->jt_temp_l;
3044 info->jt_vfchg_h = pdata->jt_vfchg_h;
3045 info->jt_vfchg_l = pdata->jt_vfchg_l;
3046 info->jt_ichg_h = pdata->jt_ichg_h;
3047 info->jt_ichg_l = pdata->jt_ichg_l;
3050 info->adc_vdd_mv = ADC_VDD_MV; /* 2800; */
3051 info->min_voltage = MIN_VOLTAGE; /* 3100; */
3052 info->max_voltage = MAX_VOLTAGE; /* 4200; */
3054 info->entry_factory_mode = false;
3056 mutex_init(&info->lock);
3057 platform_set_drvdata(pdev, info);
3059 info->battery.name = "battery";
3060 info->battery.type = POWER_SUPPLY_TYPE_BATTERY;
3061 info->battery.properties = ricoh619_batt_props;
3062 info->battery.num_properties = ARRAY_SIZE(ricoh619_batt_props);
3063 info->battery.get_property = ricoh619_batt_get_prop;
3064 info->battery.set_property = NULL;
3065 /* info->battery.external_power_changed
3066 = ricoh619_external_power_changed; */
3068 /* Disable Charger/ADC interrupt */
3069 ret = ricoh619_clr_bits(info->dev->parent, RICOH619_INTC_INTEN,
3074 ret = ricoh619_init_battery(info);
3078 #ifdef ENABLE_FACTORY_MODE
3079 info->factory_mode_wqueue
3080 = create_singlethread_workqueue("ricoh619_factory_mode");
3081 INIT_DELAYED_WORK_DEFERRABLE(&info->factory_mode_work,
3082 check_charging_state_work);
3084 ret = ricoh619_factory_mode(info);
3090 ret = power_supply_register(&pdev->dev, &info->battery);
3093 info->battery.dev->parent = &pdev->dev;
3095 ret = power_supply_register(&pdev->dev, &powerac);
3096 ret = power_supply_register(&pdev->dev, &powerusb);
3098 info->monitor_wqueue
3099 = create_singlethread_workqueue("ricoh619_battery_monitor");
3101 info->workqueue = create_singlethread_workqueue("rc5t619_charger_in");
3102 INIT_WORK(&info->irq_work, charger_irq_work);
3105 = create_singlethread_workqueue("rc5t619_usb_det");
3106 INIT_WORK(&info->usb_irq_work, usb_det_irq_work);
3108 INIT_DELAYED_WORK_DEFERRABLE(&info->monitor_work,
3109 ricoh619_battery_work);
3110 INIT_DELAYED_WORK_DEFERRABLE(&info->displayed_work,
3111 ricoh619_displayed_work);
3112 INIT_DELAYED_WORK_DEFERRABLE(&info->charge_stable_work,
3113 ricoh619_stable_charge_countdown_work);
3114 INIT_DELAYED_WORK_DEFERRABLE(&info->charge_monitor_work,
3115 ricoh619_charge_monitor_work);
3116 INIT_DELAYED_WORK_DEFERRABLE(&info->get_charge_work,
3117 ricoh619_get_charge_work);
3118 INIT_DELAYED_WORK_DEFERRABLE(&info->jeita_work, ricoh619_jeita_work);
3119 INIT_DELAYED_WORK(&info->changed_work, ricoh619_changed_work);
3121 /* Charger IRQ workqueue settings */
3122 charger_irq = pdata->irq;
3124 ret = request_threaded_irq(charger_irq + RICOH619_IRQ_FONCHGINT,
3125 NULL, charger_in_isr, IRQF_ONESHOT,
3126 "rc5t619_charger_in", info);
3128 dev_err(&pdev->dev, "Can't get CHG_INT IRQ for chrager: %d\n",
3133 ret = request_threaded_irq(charger_irq + RICOH619_IRQ_FCHGCMPINT,
3134 NULL, charger_complete_isr,
3135 IRQF_ONESHOT, "rc5t619_charger_comp",
3138 dev_err(&pdev->dev, "Can't get CHG_COMP IRQ for chrager: %d\n",
3143 ret = request_threaded_irq(charger_irq + RICOH619_IRQ_FVUSBDETSINT,
3144 NULL, charger_usb_isr, IRQF_ONESHOT,
3145 "rc5t619_usb_det", info);
3147 dev_err(&pdev->dev, "Can't get USB_DET IRQ for chrager: %d\n",
3152 ret = request_threaded_irq(charger_irq + RICOH619_IRQ_FVADPDETSINT,
3153 NULL, charger_adp_isr, IRQF_ONESHOT,
3154 "rc5t619_adp_det", info);
3157 "Can't get ADP_DET IRQ for chrager: %d\n", ret);
3161 #ifdef ENABLE_LOW_BATTERY_DETECTION
3162 ret = request_threaded_irq(charger_irq + RICOH619_IRQ_VSYSLIR,
3163 NULL, adc_vsysl_isr, IRQF_ONESHOT,
3164 "rc5t619_adc_vsysl", info);
3167 "Can't get ADC_VSYSL IRQ for chrager: %d\n", ret);
3170 INIT_DELAYED_WORK_DEFERRABLE(&info->low_battery_work,
3171 low_battery_irq_work);
3174 /* Charger init and IRQ setting */
3175 ret = ricoh619_init_charger(info);
3179 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
3180 ret = ricoh619_init_fgsoca(info);
3182 queue_delayed_work(info->monitor_wqueue, &info->monitor_work,
3183 RICOH619_MONITOR_START_TIME*HZ);
3185 /* Enable Charger interrupt */
3186 ricoh619_set_bits(info->dev->parent, RICOH619_INTC_INTEN, CHG_INT);
3195 static int __devexit ricoh619_battery_remove(struct platform_device *pdev)
3197 struct ricoh619_battery_info *info = platform_get_drvdata(pdev);
3202 bool is_charging = true;
3203 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
3205 && (info->soca->status == RICOH619_SOCA_STABLE)) {
3206 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
3208 dev_err(info->dev, "Error in writing PSWR_REG\n");
3211 if (info->soca->displayed_soc < 0) {
3214 val = (info->soca->displayed_soc + 50)/100;
3217 ret = ricoh619_write(info->dev->parent, PSWR_REG, val);
3219 dev_err(info->dev, "Error in writing PSWR_REG\n");
3223 ret = calc_capacity_in_period(info, &cc_cap, &is_charging);
3225 dev_err(info->dev, "Read cc_sum Error !!-----\n");
3228 if (g_fg_on_mode == 0) {
3229 ret = ricoh619_clr_bits(info->dev->parent,
3232 dev_err(info->dev, "Error in clr FG EN\n");
3235 /* set rapid timer 300 min */
3236 err = ricoh619_set_bits(info->dev->parent, TIMSET_REG, 0x03);
3238 dev_err(info->dev, "Error in writing the control register\n");
3241 free_irq(charger_irq + RICOH619_IRQ_FONCHGINT, &info);
3242 free_irq(charger_irq + RICOH619_IRQ_FCHGCMPINT, &info);
3243 free_irq(charger_irq + RICOH619_IRQ_FVUSBDETSINT, &info);
3244 free_irq(charger_irq + RICOH619_IRQ_FVADPDETSINT, &info);
3245 #ifdef ENABLE_LOW_BATTERY_DETECTION
3246 free_irq(charger_irq + RICOH619_IRQ_VSYSLIR, &info);
3249 cancel_delayed_work(&info->monitor_work);
3250 cancel_delayed_work(&info->charge_stable_work);
3251 cancel_delayed_work(&info->charge_monitor_work);
3252 cancel_delayed_work(&info->get_charge_work);
3253 cancel_delayed_work(&info->displayed_work);
3255 cancel_delayed_work(&info->changed_work);
3256 #ifdef ENABLE_LOW_BATTERY_DETECTION
3257 cancel_delayed_work(&info->low_battery_work);
3259 cancel_delayed_work(&info->factory_mode_work);
3260 cancel_delayed_work(&info->jeita_work);
3262 cancel_work_sync(&info->irq_work);
3263 cancel_work_sync(&info->usb_irq_work);
3265 flush_workqueue(info->monitor_wqueue);
3266 flush_workqueue(info->workqueue);
3267 flush_workqueue(info->usb_workqueue);
3268 flush_workqueue(info->factory_mode_wqueue);
3270 destroy_workqueue(info->monitor_wqueue);
3271 destroy_workqueue(info->workqueue);
3272 destroy_workqueue(info->usb_workqueue);
3273 destroy_workqueue(info->factory_mode_wqueue);
3275 power_supply_unregister(&info->battery);
3277 platform_set_drvdata(pdev, NULL);
3282 static int ricoh619_battery_suspend(struct device *dev)
3284 struct ricoh619_battery_info *info = dev_get_drvdata(dev);
3289 bool is_charging = true;
3292 && (info->soca->status == RICOH619_SOCA_STABLE)) {
3293 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
3295 dev_err(info->dev, "Error in writing PSWR_REG\n");
3297 info->soca->suspend_soc = (info->soca->displayed_soc + 50)/100;
3299 if (info->soca->displayed_soc < 0) {
3302 val = (info->soca->displayed_soc + 50)/100;
3305 ret = ricoh619_write(info->dev->parent, PSWR_REG, val);
3307 dev_err(info->dev, "Error in writing PSWR_REG\n");
3311 info->soca->suspend_soc = (info->soca->displayed_soc + 50)/100;
3313 ret = calc_capacity_in_period(info, &cc_cap, &is_charging);
3315 dev_err(info->dev, "Read cc_sum Error !!-----\n");
3318 if (info->soca->status == RICOH619_SOCA_STABLE
3319 || info->soca->status == RICOH619_SOCA_FULL)
3320 info->soca->status = RICOH619_SOCA_DISP;
3322 /* set rapid timer 300 min */
3323 err = ricoh619_set_bits(info->dev->parent, TIMSET_REG, 0x03);
3325 dev_err(info->dev, "Error in writing the control register\n");
3328 // disable_irq(charger_irq + RICOH619_IRQ_FONCHGINT);
3329 // disable_irq(charger_irq + RICOH619_IRQ_FCHGCMPINT);
3330 // disable_irq(charger_irq + RICOH619_IRQ_FVUSBDETSINT);
3331 // disable_irq(charger_irq + RICOH619_IRQ_FVADPDETSINT);
3332 #ifdef ENABLE_LOW_BATTERY_DETECTION
3333 // disable_irq(charger_irq + RICOH619_IRQ_VSYSLIR);
3336 flush_delayed_work(&info->monitor_work);
3337 flush_delayed_work(&info->displayed_work);
3338 flush_delayed_work(&info->charge_stable_work);
3339 flush_delayed_work(&info->charge_monitor_work);
3340 flush_delayed_work(&info->get_charge_work);
3341 flush_delayed_work(&info->changed_work);
3342 #ifdef ENABLE_LOW_BATTERY_DETECTION
3343 flush_delayed_work(&info->low_battery_work);
3345 flush_delayed_work(&info->factory_mode_work);
3346 flush_delayed_work(&info->jeita_work);
3348 // flush_work(&info->irq_work);
3349 // flush_work(&info->usb_irq_work);
3355 static int ricoh619_battery_resume(struct device *dev)
3357 struct ricoh619_battery_info *info = dev_get_drvdata(dev);
3360 int displayed_soc_temp;
3362 bool is_charging = true;
3363 bool is_jeita_updated;
3366 RICOH_FG_DBG(KERN_INFO "PMU: %s: \n", __func__);
3368 ret = check_jeita_status(info, &is_jeita_updated);
3370 dev_err(info->dev, "Error in updating JEITA %d\n", ret);
3373 if (info->entry_factory_mode) {
3374 info->soca->displayed_soc = -EINVAL;
3375 } else if (RICOH619_SOCA_ZERO == info->soca->status) {
3376 if (calc_ocv(info) > get_OCV_voltage(info, 0)) {
3377 ret = ricoh619_read(info->dev->parent, PSWR_REG, &val);
3379 info->soca->soc = val * 100;
3382 "Error in reading PSWR_REG %d\n", ret);
3384 = calc_capacity(info) * 100;
3387 ret = calc_capacity_in_period(info, &cc_cap,
3390 dev_err(info->dev, "Read cc_sum Error !!-----\n");
3392 info->soca->cc_delta
3393 = (is_charging == true) ? cc_cap : -cc_cap;
3396 = info->soca->soc + info->soca->cc_delta;
3397 if (displayed_soc_temp < 0)
3398 displayed_soc_temp = 0;
3399 displayed_soc_temp = min(10000, displayed_soc_temp);
3400 displayed_soc_temp = max(0, displayed_soc_temp);
3401 info->soca->displayed_soc = displayed_soc_temp;
3403 ret = ricoh619_write(info->dev->parent,
3406 dev_err(info->dev, "Error in writing the control register\n");
3407 info->soca->ready_fg = 0;
3408 info->soca->status = RICOH619_SOCA_FG_RESET;
3411 info->soca->displayed_soc = 0;
3413 info->soca->soc = info->soca->suspend_soc * 100;
3415 ret = calc_capacity_in_period(info, &cc_cap, &is_charging);
3417 dev_err(info->dev, "Read cc_sum Error !!-----\n");
3419 info->soca->cc_delta = (is_charging == true) ? cc_cap : -cc_cap;
3421 displayed_soc_temp = info->soca->soc + info->soca->cc_delta;
3422 if (displayed_soc_temp < 0)
3423 displayed_soc_temp = 0;
3424 displayed_soc_temp = min(10000, displayed_soc_temp);
3425 displayed_soc_temp = max(0, displayed_soc_temp);
3427 if (0 == info->soca->jt_limit) {
3428 check_charge_status_2(info, displayed_soc_temp);
3430 info->soca->displayed_soc = displayed_soc_temp;
3433 if (RICOH619_SOCA_DISP == info->soca->status) {
3434 info->soca->last_soc = calc_capacity(info) * 100;
3435 info->soca->soc_delta = 0;
3438 info->soca->update_count = 0;
3440 ret = measure_vbatt_FG(info, &info->soca->Vbat_ave);
3441 ret = measure_vsys_ADC(info, &info->soca->Vsys_ave);
3442 ret = measure_Ibatt_FG(info, &info->soca->Ibat_ave);
3444 power_supply_changed(&info->battery);
3445 queue_delayed_work(info->monitor_wqueue, &info->displayed_work, HZ);
3447 if (RICOH619_SOCA_UNSTABLE == info->soca->status) {
3448 info->soca->stable_count = 10;
3449 queue_delayed_work(info->monitor_wqueue,
3450 &info->charge_stable_work,
3451 RICOH619_FG_STABLE_TIME*HZ/10);
3452 } else if (RICOH619_SOCA_FG_RESET == info->soca->status) {
3453 info->soca->stable_count = 1;
3455 for (i = 0; i < 3; i = i+1)
3456 info->soca->reset_soc[i] = 0;
3457 info->soca->reset_count = 0;
3459 queue_delayed_work(info->monitor_wqueue,
3460 &info->charge_stable_work,
3461 RICOH619_FG_RESET_TIME*HZ);
3464 queue_delayed_work(info->monitor_wqueue, &info->monitor_work,
3465 info->monitor_time);
3467 queue_delayed_work(info->monitor_wqueue, &info->charge_monitor_work,
3468 RICOH619_CHARGE_RESUME_TIME * HZ);
3470 info->soca->chg_count = 0;
3471 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
3472 RICOH619_CHARGE_RESUME_TIME * HZ);
3474 if (!info->jt_hw_sw) {
3475 queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
3476 RICOH619_JEITA_UPDATE_TIME * HZ);
3479 ricoh619_write(info->dev->parent, 0x9d, 0x00);
3480 // enable_irq(charger_irq + RICOH619_IRQ_FONCHGINT);
3481 // enable_irq(charger_irq + RICOH619_IRQ_FCHGCMPINT);
3482 // enable_irq(charger_irq + RICOH619_IRQ_FVUSBDETSINT);
3483 // enable_irq(charger_irq + RICOH619_IRQ_FVADPDETSINT);
3484 #ifdef ENABLE_LOW_BATTERY_DETECTION
3485 // enable_irq(charger_irq + RICOH619_IRQ_VSYSLIR);
3487 ricoh619_write(info->dev->parent, 0x9d, 0x4d);
3491 static const struct dev_pm_ops ricoh619_battery_pm_ops = {
3492 .suspend = ricoh619_battery_suspend,
3493 .resume = ricoh619_battery_resume,
3497 static struct platform_driver ricoh619_battery_driver = {
3499 .name = "ricoh619-battery",
3500 .owner = THIS_MODULE,
3502 .pm = &ricoh619_battery_pm_ops,
3505 .probe = ricoh619_battery_probe,
3506 .remove = __devexit_p(ricoh619_battery_remove),
3509 static int __init ricoh619_battery_init(void)
3511 RICOH_FG_DBG("PMU: %s\n", __func__);
3512 return platform_driver_register(&ricoh619_battery_driver);
3514 subsys_initcall_sync(ricoh619_battery_init);
3516 static void __exit ricoh619_battery_exit(void)
3518 platform_driver_unregister(&ricoh619_battery_driver);
3520 module_exit(ricoh619_battery_exit);
3522 MODULE_DESCRIPTION("RICOH619 Battery driver");
3523 MODULE_ALIAS("platform:ricoh619-battery");
3524 MODULE_LICENSE("GPL");