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>
36 #include <linux/interrupt.h>
37 #include <linux/irq.h>
40 /* define for function */
41 #define ENABLE_FUEL_GAUGE_FUNCTION
42 #define ENABLE_LOW_BATTERY_DETECTION
43 #define ENABLE_FACTORY_MODE
44 #define ENABLE_FG_KEEP_ON_MODE
46 /* define for current limit. unit is mA */
47 /* if value is "0", these settings set by OTP */
48 #define RICOH619_MAX_CHARGE_CURRENT 0 /* mA */
49 #define RICOH619_MAX_ADP_CURRENT 0 /* mA */
50 #define RICOH619_MAX_USB_CURRENT 0 /* mA */
51 #define RICOH619_CHARGE_COMPLETION_CURRENT 200 /* mA Range 50~200
53 #define RICOH619_FULL_CHARGING_VOLTAGE 0 /* mv can set 4050,
55 * 4350(default 4100) */
56 #define RICOH619_RE_CHARGING_VOLTAGE 0 /* mv can set 3850,
58 * 4100(default 3900) */
61 #define CUTOFF_VOL 0 /* mV "0" means cutoff
64 #define RICOH619_REL1_SEL_VALUE 64 /* mv Range 0~240
75 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
76 /* define for FG parameter */
77 #define RICOH619_MONITOR_START_TIME 15
78 #define RICOH619_FG_RESET_TIME 6
79 #define RICOH619_FG_STABLE_TIME 120
80 #define RICOH619_DISPLAY_UPDATE_TIME 60
81 #define RICOH619_SOCA_DISP_UPDATE_TIME 60
82 #define RICOH619_MAX_RESET_SOC_DIFF 5
84 /* define for FG status */
87 RICOH619_SOCA_UNSTABLE,
88 RICOH619_SOCA_FG_RESET,
95 #ifdef ENABLE_LOW_BATTERY_DETECTION
96 #define LOW_BATTERY_DETECTION_TIME 10
99 struct ricoh619_soca_info {
103 int soc; /* Latest FG SOC value */
105 int status; /* SOCA status 0: Not initial; 5: Finished */
107 int chg_status; /* chg_status */
108 int soc_delta; /* soc delta for status3(DISP) */
116 struct ricoh619_battery_info {
118 struct power_supply battery;
119 struct delayed_work monitor_work;
120 struct delayed_work usb_work;
121 struct delayed_work displayed_work;
122 struct delayed_work charge_stable_work;
123 struct delayed_work changed_work;
124 #ifdef ENABLE_LOW_BATTERY_DETECTION
125 struct delayed_work low_battery_work;
128 struct work_struct irq_work; /* for Charging & VUSB/VADP */
129 struct work_struct usb_irq_work; /* for ADC_VUSB */
131 struct workqueue_struct *monitor_wqueue;
132 struct workqueue_struct *workqueue; /* for Charging & VUSB/VADP */
133 struct workqueue_struct *usb_workqueue; /* for ADC_VUSB */
135 #ifdef ENABLE_FACTORY_MODE
136 struct delayed_work factory_mode_work;
137 struct workqueue_struct *factory_mode_wqueue;
141 unsigned long monitor_time;
157 struct ricoh619_soca_info *soca;
159 bool entry_factory_mode;
163 /* this value is for mfd fucntion */
167 static void ricoh619_battery_work(struct work_struct *work)
169 struct ricoh619_battery_info *info = container_of(work,
170 struct ricoh619_battery_info, monitor_work.work);
172 RICOH_FG_DBG("PMU: %s\n", __func__);
173 power_supply_changed(&info->battery);
174 queue_delayed_work(info->monitor_wqueue, &info->monitor_work,
178 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
179 static int measure_vbatt_FG(struct ricoh619_battery_info *info, int *data);
180 static int measure_Ibatt_FG(struct ricoh619_battery_info *info, int *data);
181 static int calc_capacity(struct ricoh619_battery_info *info);
182 static int get_OCV_init_Data(struct ricoh619_battery_info *info, int index);
183 static int get_OCV_voltage(struct ricoh619_battery_info *info, int index);
184 static int get_check_fuel_gauge_reg(struct ricoh619_battery_info *info,
185 int Reg_h, int Reg_l, int enable_bit);
186 static int calc_capacity_in_period(struct ricoh619_battery_info *info,
187 int *cc_cap, bool *is_charging);
189 /* check charge status.
190 * if CHG not Complete && SOC == 100 -> Stop charge
191 * if CHG Complete && SOC =! 100 -> SOC reset
193 static int check_charge_status(struct ricoh619_battery_info *info)
196 uint8_t supply_state;
197 uint8_t charge_state;
201 /* get power supply status */
202 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &status);
205 "Error in reading the control register\n");
210 charge_state = (status & 0x1F);
211 supply_state = ((status & 0xC0) >> 6);
213 current_SOC = (info->soca->displayed_soc + 50)/100;
215 if (charge_state == CHG_STATE_CHG_COMPLETE) {
217 if (current_SOC != 100) {
218 ret = ricoh619_write(info->dev->parent,
221 dev_err(info->dev, "Error in writing the control register\n");
224 info->soca->ready_fg = 0;
226 info->soca->displayed_soc = 100 * 100;
228 info->soca->status = RICOH619_SOCA_STABLE;
231 } else { /* chg not complete */
232 if (current_SOC == 100) {
233 ret = ricoh619_clr_bits(info->dev->parent,
236 dev_err(info->dev, "Error in writing the control register\n");
239 info->soca->status = RICOH619_SOCA_STABLE;
241 ret = ricoh619_set_bits(info->dev->parent,
244 dev_err(info->dev, "Error in writing the control register\n");
252 static int calc_ocv(struct ricoh619_battery_info *info)
259 ret = measure_vbatt_FG(info, &Vbat);
260 ret = measure_Ibatt_FG(info, &Ibat);
262 ocv = Vbat - Ibat * info->soca->Rbat;
268 * Calculate Capacity in a period
269 * - read CC_SUM & FA_CAP from Coulom Counter
270 * - and calculate Capacity.
271 * @cc_cap: capacity in a period, unit 0.01%
272 * @is_charging: Flag of charging current direction
273 * TRUE : charging (plus)
274 * FALSE: discharging (minus)
276 static int calc_capacity_in_period(struct ricoh619_battery_info *info,
277 int *cc_cap, bool *is_charging)
280 uint8_t cc_sum_reg[4];
281 uint8_t cc_clr[4] = {0, 0, 0, 0};
282 uint8_t fa_cap_reg[2];
286 *is_charging = true; /* currrent state initialize -> charging */
288 if (info->entry_factory_mode)
291 /* Disable Charging/Completion Interrupt */
292 err = ricoh619_set_bits(info->dev->parent,
293 RICOH619_INT_MSK_CHGSTS1, 0x01);
297 /* In suspend - disable charging */
298 err = ricoh619_set_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x08);
302 err = ricoh619_write(info->dev->parent, CC_CTRL_REG, 0x01);
306 err = ricoh619_bulk_reads(info->dev->parent,
307 CC_SUMREG3_REG, 4, cc_sum_reg);
312 err = ricoh619_bulk_writes(info->dev->parent,
313 CC_SUMREG3_REG, 4, cc_clr);
318 err = ricoh619_write(info->dev->parent, CC_CTRL_REG, 0);
321 /* out suspend - enable charging */
322 err = ricoh619_clr_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x08);
328 /* Clear Charging Interrupt status */
329 err = ricoh619_clr_bits(info->dev->parent,
330 RICOH619_INT_IR_CHGSTS1, 0x01);
334 /* ricoh619_read(info->dev->parent, RICOH619_INT_IR_CHGSTS1, &val);
335 RICOH_FG_DBG("INT_IR_CHGSTS1 = 0x%x\n",val); */
337 /* Enable Charging Interrupt */
338 err = ricoh619_clr_bits(info->dev->parent,
339 RICOH619_INT_MSK_CHGSTS1, 0x01);
344 err = ricoh619_bulk_reads(info->dev->parent,
345 FA_CAP_H_REG, 2, fa_cap_reg);
349 /* fa_cap = *(uint16_t*)fa_cap_reg & 0x7fff; */
350 fa_cap = (fa_cap_reg[0] << 8 | fa_cap_reg[1]) & 0x7fff;
352 /* calculation two's complement of CC_SUM */
353 cc_sum = cc_sum_reg[0] << 24 | cc_sum_reg[1] << 16 |
354 cc_sum_reg[2] << 8 | cc_sum_reg[3];
356 /* cc_sum = *(uint32_t*)cc_sum_reg; */
357 if (cc_sum & 0x80000000) {
358 cc_sum = (cc_sum^0xffffffff)+0x01;
359 *is_charging = false; /* discharge */
362 *cc_cap = cc_sum*25/9/fa_cap; /* CC_SUM/3600/FA_CAP */
366 dev_err(info->dev, "Error !!-----\n");
370 static void ricoh619_displayed_work(struct work_struct *work)
377 int displayed_soc_temp;
379 bool is_charging = true;
381 struct ricoh619_battery_info *info = container_of(work,
382 struct ricoh619_battery_info, displayed_work.work);
384 if (info->entry_factory_mode) {
385 info->soca->status = RICOH619_SOCA_STABLE;
386 info->soca->displayed_soc = -EINVAL;
387 info->soca->ready_fg = 0;
391 mutex_lock(&info->lock);
393 if ((RICOH619_SOCA_START == info->soca->status)
394 || (RICOH619_SOCA_STABLE == info->soca->status))
395 info->soca->ready_fg = 1;
397 if (RICOH619_SOCA_STABLE == info->soca->status) {
398 info->soca->soc = calc_capacity(info) * 100;
399 info->soca->displayed_soc = info->soca->soc;
400 } else if (RICOH619_SOCA_DISP == info->soca->status) {
402 info->soca->soc = calc_capacity(info) * 100;
404 soc_round = info->soca->soc / 100;
405 last_soc_round = info->soca->last_soc / 100;
406 last_disp_round = (info->soca->displayed_soc + 50) / 100;
408 info->soca->soc_delta =
409 info->soca->soc_delta + (soc_round - last_soc_round);
411 info->soca->last_soc = info->soca->soc;
414 if (last_disp_round == soc_round) {
415 /* if SOC == DISPLAY move to stable */
416 info->soca->displayed_soc = info->soca->soc ;
417 info->soca->status = RICOH619_SOCA_STABLE;
419 } else if ((soc_round == 100) || (soc_round == 0)) {
420 /* if SOC is 0% or 100% , finish display state*/
421 info->soca->displayed_soc = info->soca->soc ;
422 info->soca->status = RICOH619_SOCA_STABLE;
424 } else if ((info->soca->chg_status) ==
425 (POWER_SUPPLY_STATUS_CHARGING)) {
427 if (last_disp_round < soc_round) {
428 /* Case 1 : Charge, Display < SOC */
429 if (info->soca->soc_delta >= 1) {
430 info->soca->displayed_soc
432 + info->soca->soc_delta)*100;
433 info->soca->soc_delta = 0;
435 info->soca->displayed_soc
436 = (last_disp_round + 1)*100;
439 if (last_disp_round >= soc_round) {
440 info->soca->displayed_soc
443 = RICOH619_SOCA_STABLE;
445 } else if (last_disp_round > soc_round) {
446 /* Case 2 : Charge, Display > SOC */
447 if (info->soca->soc_delta >= 3) {
448 info->soca->displayed_soc =
449 (last_disp_round + 1)*100;
450 info->soca->soc_delta = 0;
452 if (last_disp_round <= soc_round) {
453 info->soca->displayed_soc
456 = RICOH619_SOCA_STABLE;
461 if (last_disp_round > soc_round) {
462 /* Case 3 : Dis-Charge, Display > SOC */
463 if (info->soca->soc_delta <= -1) {
464 info->soca->displayed_soc
466 + info->soca->soc_delta)*100;
467 info->soca->soc_delta = 0;
469 info->soca->displayed_soc
470 = (last_disp_round - 1)*100;
472 if (last_disp_round <= soc_round) {
473 info->soca->displayed_soc
476 = RICOH619_SOCA_STABLE;
478 } else if (last_disp_round < soc_round) {
479 /* dis Charge, Display < SOC */
480 if (info->soca->soc_delta <= -3) {
481 info->soca->displayed_soc
482 = (last_disp_round - 1)*100;
483 info->soca->soc_delta = 0;
485 if (last_disp_round >= soc_round) {
486 info->soca->displayed_soc
489 = RICOH619_SOCA_STABLE;
493 } else if (RICOH619_SOCA_UNSTABLE == info->soca->status
494 || RICOH619_SOCA_FG_RESET == info->soca->status) {
496 } else if (RICOH619_SOCA_START == info->soca->status) {
497 err = ricoh619_read(info->dev->parent, PSWR_REG, &val);
499 if (info->first_pwon) {
500 info->soca->soc = calc_capacity(info) * 100;
501 if ((info->soca->soc == 0) && (calc_ocv(info)
502 < get_OCV_voltage(info, 0))) {
503 info->soca->displayed_soc = 0;
504 info->soca->status = RICOH619_SOCA_ZERO;
506 info->soca->displayed_soc = info->soca->soc;
507 info->soca->status = RICOH619_SOCA_UNSTABLE;
509 } else if (g_fg_on_mode && (val == 0x7f)) {
510 info->soca->soc = calc_capacity(info) * 100;
511 if ((info->soca->soc == 0) && (calc_ocv(info)
512 < get_OCV_voltage(info, 0))) {
513 info->soca->displayed_soc = 0;
514 info->soca->status = RICOH619_SOCA_ZERO;
516 info->soca->displayed_soc = info->soca->soc;
517 info->soca->status = RICOH619_SOCA_STABLE;
520 info->soca->soc = val * 100;
521 if ((err < 0) || (val == 0)) {
523 "Error in reading PSWR_REG %d\n", err);
525 = calc_capacity(info) * 100 + 50;
528 err = calc_capacity_in_period(info, &cc_cap,
531 dev_err(info->dev, "Read cc_sum Error !!-----\n");
534 = (is_charging == true) ? cc_cap : -cc_cap;
535 if (calc_ocv(info) < get_OCV_voltage(info, 0)) {
536 info->soca->displayed_soc = 0;
537 info->soca->status = RICOH619_SOCA_ZERO;
540 = info->soca->soc + info->soca->cc_delta;
542 = min(10000, displayed_soc_temp);
543 displayed_soc_temp = max(0, displayed_soc_temp);
544 info->soca->displayed_soc = displayed_soc_temp;
545 info->soca->status = RICOH619_SOCA_UNSTABLE;
548 } else if (RICOH619_SOCA_ZERO == info->soca->status) {
549 if (calc_ocv(info) > get_OCV_voltage(info, 0)) {
550 err = ricoh619_write(info->dev->parent,
553 dev_err(info->dev, "Error in writing the control register\n");
554 info->soca->status = RICOH619_SOCA_STABLE;
555 info->soca->ready_fg = 0;
557 info->soca->displayed_soc = 0;
560 /* Ceck charge status */
561 err = check_charge_status(info);
563 dev_err(info->dev, "Error in writing the control register\n");
566 && (info->soca->status == RICOH619_SOCA_STABLE)) {
567 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
569 dev_err(info->dev, "Error in writing PSWR_REG\n");
572 val = (info->soca->displayed_soc + 50)/100;
574 err = ricoh619_write(info->dev->parent, PSWR_REG, val);
576 dev_err(info->dev, "Error in writing PSWR_REG\n");
578 g_soc = (info->soca->displayed_soc + 50)/100;
580 err = calc_capacity_in_period(info, &cc_cap, &is_charging);
582 dev_err(info->dev, "Read cc_sum Error !!-----\n");
585 if (0 == info->soca->ready_fg)
586 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
587 RICOH619_FG_RESET_TIME * HZ);
588 else if (RICOH619_SOCA_DISP == info->soca->status)
589 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
590 RICOH619_SOCA_DISP_UPDATE_TIME * HZ);
592 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
593 RICOH619_DISPLAY_UPDATE_TIME * HZ);
595 mutex_unlock(&info->lock);
600 static void ricoh619_stable_charge_countdown_work(struct work_struct *work)
606 struct ricoh619_battery_info *info = container_of(work,
607 struct ricoh619_battery_info, charge_stable_work.work);
609 if (info->entry_factory_mode)
612 mutex_lock(&info->lock);
613 if (RICOH619_SOCA_FG_RESET == info->soca->status)
614 info->soca->ready_fg = 1;
616 if (2 <= info->soca->stable_count) {
617 if (3 == info->soca->stable_count
618 && RICOH619_SOCA_FG_RESET == info->soca->status) {
619 ret = ricoh619_write(info->dev->parent,
622 dev_err(info->dev, "Error in writing the control register\n");
623 info->soca->ready_fg = 0;
625 info->soca->stable_count = info->soca->stable_count - 1;
626 queue_delayed_work(info->monitor_wqueue,
627 &info->charge_stable_work,
628 RICOH619_FG_STABLE_TIME * HZ / 10);
629 } else if (0 >= info->soca->stable_count) {
630 /* Finished queue, ignore */
631 } else if (1 == info->soca->stable_count) {
632 if (RICOH619_SOCA_UNSTABLE == info->soca->status) {
633 /* Judge if FG need reset or Not */
634 info->soca->soc = calc_capacity(info) * 100;
635 if (info->chg_ctr != 0) {
636 queue_delayed_work(info->monitor_wqueue,
637 &info->charge_stable_work,
638 RICOH619_FG_STABLE_TIME * HZ / 10);
639 mutex_unlock(&info->lock);
642 /* Do reset setting */
643 ret = ricoh619_write(info->dev->parent,
646 dev_err(info->dev, "Error in writing the control register\n");
648 info->soca->ready_fg = 0;
649 info->soca->status = RICOH619_SOCA_FG_RESET;
651 /* Delay for addition Reset Time (6s) */
652 queue_delayed_work(info->monitor_wqueue,
653 &info->charge_stable_work,
654 RICOH619_FG_RESET_TIME*HZ);
655 } else if (RICOH619_SOCA_FG_RESET == info->soca->status) {
656 info->soca->reset_soc[2] = info->soca->reset_soc[1];
657 info->soca->reset_soc[1] = info->soca->reset_soc[0];
658 info->soca->reset_soc[0] = calc_capacity(info) * 100;
659 info->soca->reset_count++;
661 if (info->soca->reset_count > 10) {
662 /* Reset finished; */
663 info->soca->soc = info->soca->reset_soc[0];
664 info->soca->stable_count = 0;
668 for (i = 0; i < 3; i++) {
669 if (max < info->soca->reset_soc[i]/100)
670 max = info->soca->reset_soc[i]/100;
671 if (min > info->soca->reset_soc[i]/100)
672 min = info->soca->reset_soc[i]/100;
675 if ((info->soca->reset_count > 3) && ((max - min)
676 < RICOH619_MAX_RESET_SOC_DIFF)) {
677 /* Reset finished; */
678 info->soca->soc = info->soca->reset_soc[0];
679 info->soca->stable_count = 0;
682 /* Do reset setting */
683 ret = ricoh619_write(info->dev->parent,
686 dev_err(info->dev, "Error in writing the control register\n");
688 info->soca->ready_fg = 0;
690 /* Delay for addition Reset Time (6s) */
691 queue_delayed_work(info->monitor_wqueue,
692 &info->charge_stable_work,
693 RICOH619_FG_RESET_TIME*HZ);
695 /* Finished queue From now, select FG as result; */
696 } else if (RICOH619_SOCA_START == info->soca->status) {
697 /* Normal condition */
698 } else { /* other state ZERO/DISP/STABLE */
699 info->soca->stable_count = 0;
702 mutex_unlock(&info->lock);
706 info->soca->last_soc = info->soca->soc;
707 info->soca->status = RICOH619_SOCA_DISP;
710 mutex_unlock(&info->lock);
714 /* Initial setting of FuelGauge SOCA function */
715 static int ricoh619_init_fgsoca(struct ricoh619_battery_info *info)
722 for (i = 0; i <= 10; i = i+1) {
723 info->soca->ocv_table[i] = get_OCV_voltage(info, i);
724 RICOH_FG_DBG("PMU: %s : * %d0%% voltage = %d uV\n",
725 __func__, i, info->soca->ocv_table[i]);
728 for (i = 0; i < 3; i = i+1)
729 info->soca->reset_soc[i] = 0;
730 info->soca->reset_count = 0;
732 if (info->first_pwon) {
734 err = ricoh619_read(info->dev->parent, CHGISET_REG, &val);
737 "Error in read CHGISET_REG%d\n", err);
739 err = ricoh619_write(info->dev->parent, CHGISET_REG, 0);
742 "Error in writing CHGISET_REG%d\n", err);
746 err = ricoh619_write(info->dev->parent,
749 dev_err(info->dev, "Error in writing the control register\n");
753 err = ricoh619_write(info->dev->parent, CHGISET_REG, val);
756 "Error in writing CHGISET_REG%d\n", err);
759 /* Rbat : Transfer */
760 info->soca->Rbat = get_OCV_init_Data(info, 12) * 1000 / 512
762 info->soca->n_cap = get_OCV_init_Data(info, 11);
764 info->soca->displayed_soc = 0;
765 info->soca->ready_fg = 0;
766 info->soca->soc_delta = 0;
767 info->soca->status = RICOH619_SOCA_START;
768 /* stable count down 11->2, 1: reset; 0: Finished; */
769 info->soca->stable_count = 11;
771 #ifdef ENABLE_FG_KEEP_ON_MODE
777 /* Start first Display job */
778 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
779 RICOH619_FG_RESET_TIME*HZ);
781 /* Start first Waiting stable job */
782 queue_delayed_work(info->monitor_wqueue, &info->charge_stable_work,
783 RICOH619_FG_STABLE_TIME*HZ/10);
785 RICOH_FG_DBG("PMU: %s : * Rbat = %d mOhm n_cap = %d mAH\n",
786 __func__, info->soca->Rbat, info->soca->n_cap);
791 static void ricoh619_changed_work(struct work_struct *work)
793 struct ricoh619_battery_info *info = container_of(work,
794 struct ricoh619_battery_info, changed_work.work);
796 RICOH_FG_DBG("PMU: %s\n", __func__);
797 power_supply_changed(&info->battery);
802 #ifdef ENABLE_FACTORY_MODE
803 /*------------------------------------------------------*/
805 /* Check Battery exist or not */
806 /* If not, disabled Rapid to Complete State change */
807 /*------------------------------------------------------*/
808 static int ricoh619_factory_mode(struct ricoh619_battery_info *info)
813 ret = ricoh619_read(info->dev->parent, RICOH619_INT_MON_CHGCTR, &val);
815 dev_err(info->dev, "Error in reading the control register\n");
818 if (!(val & 0x01)) /* No Adapter connected */
821 /* Rapid to Complete State change disable */
822 ret = ricoh619_write(info->dev->parent, RICOH619_CHG_CTL1, 0xe3);
824 dev_err(info->dev, "Error in writing the control register\n");
828 /* Wait 1s for checking Charging State */
829 queue_delayed_work(info->factory_mode_wqueue, &info->factory_mode_work,
835 static void check_charging_state_work(struct work_struct *work)
837 struct ricoh619_battery_info *info = container_of(work,
838 struct ricoh619_battery_info, factory_mode_work.work);
842 int chargeCurrent = 0;
844 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &val);
846 dev_err(info->dev, "Error in reading the control register\n");
851 chargeCurrent = get_check_fuel_gauge_reg(info, CC_AVERAGE1_REG,
852 CC_AVERAGE0_REG, 0x3fff);
853 if (chargeCurrent < 0) {
854 dev_err(info->dev, "Error in reading the FG register\n");
858 /* Repid State && Charge Current about 0mA */
859 if (((chargeCurrent > 0x3ffc && chargeCurrent < 0x3fff)
860 || chargeCurrent < 0x05) && val == 0x43) {
861 RICOH_FG_DBG("PMU:%s --- No battery !! Enter Factory mode ---\n"
863 info->entry_factory_mode = true;
864 return; /* Factory Mode */
867 /* Return Normal Mode --> Rapid to Complete State change enable */
868 ret = ricoh619_write(info->dev->parent, RICOH619_CHG_CTL1, 0xa3);
870 dev_err(info->dev, "Error in writing the control register\n");
873 RICOH_FG_DBG("PMU:%s --- Battery exist !! Return Normal mode ---0x%2x\n"
878 #endif /* ENABLE_FACTORY_MODE */
880 static int Calc_Linear_Interpolation(int x0, int y0, int x1, int y1, int y)
885 alpha = (y - y0)*100 / (y1 - y0);
887 x = ((100 - alpha) * x0 + alpha * x1) / 100;
892 static int ricoh619_set_OCV_table(struct ricoh619_battery_info *info)
901 int OCV_percent_new[11];
903 if (CUTOFF_VOL == 0) { /* normal version */
904 } else { /*Slice cutoff voltage version. */
906 /* get ocv table. this table is calculated by Apprication */
907 for (i = 0; i <= 10; i = i+1) {
908 temp = (battery_init_para[i*2]<<8)
909 | (battery_init_para[i*2+1]);
910 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
911 temp = ((temp * 50000 * 10 / 4095) + 5) / 10;
917 for (i = 1; i < 11; i++) {
918 if (ocv_table[i] >= CUTOFF_VOL * 10) {
920 start_par = Calc_Linear_Interpolation(
921 (i-1)*1000, ocv_table[i-1], i*1000,
922 ocv_table[i], (CUTOFF_VOL * 10));
926 /* calc new ocv percent */
927 percent_step = (10000 - start_par) / 10;
929 for (i = 0; i < 11; i++) {
931 = start_par + percent_step*(i - 0);
934 /* calc new ocv voltage */
935 for (i = 0; i < 11; i++) {
936 for (j = 1; j < 11; j++) {
937 if (1000*j >= OCV_percent_new[i]) {
938 temp = Calc_Linear_Interpolation(
939 ocv_table[j-1], (j-1)*1000,
940 ocv_table[j] , j*1000,
943 temp = temp * 4095 / 50000;
945 battery_init_para[i*2 + 1] = temp;
946 battery_init_para[i*2] = temp >> 8;
953 /* calc available capacity */
954 /* get avilable capacity */
955 /* battery_init_para23-24 is designe capacity */
956 available_cap = (battery_init_para[22]<<8)
957 | (battery_init_para[23]);
959 available_cap = available_cap
960 * ((10000 - start_par) / 100) / 100 ;
963 battery_init_para[23] = available_cap;
964 battery_init_para[22] = available_cap >> 8;
967 ret = ricoh619_bulk_writes_bank1(info->dev->parent,
968 BAT_INIT_TOP_REG, 32, battery_init_para);
970 dev_err(info->dev, "batterry initialize error\n");
977 /* Initial setting of battery */
978 static int ricoh619_init_battery(struct ricoh619_battery_info *info)
982 /* Need to implement initial setting of batery and error */
983 /* -------------------------- */
984 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
986 /* set kanwa state */
987 if (RICOH619_REL1_SEL_VALUE > 240)
990 val = RICOH619_REL1_SEL_VALUE / 16 ;
994 ret = ricoh619_write_bank1(info->dev->parent, BAT_REL_SEL_REG, val);
996 dev_err(info->dev, "Error in writing the OCV Tabler\n");
1000 ret = ricoh619_read(info->dev->parent, FG_CTRL_REG, &val);
1002 dev_err(info->dev, "Error in reading the control register\n");
1006 val = (val & 0x10) >> 4;
1007 info->first_pwon = (val == 0) ? 1 : 0;
1009 ret = ricoh619_set_OCV_table(info);
1011 dev_err(info->dev, "Error in writing the OCV Tabler\n");
1015 ret = ricoh619_write(info->dev->parent, FG_CTRL_REG, 0x11);
1017 dev_err(info->dev, "Error in writing the control register\n");
1023 if (info->alarm_vol_mv < 2700 || info->alarm_vol_mv > 3400) {
1024 dev_err(info->dev, "alarm_vol_mv is out of range!\n");
1031 /* Initial setting of charger */
1032 static int ricoh619_init_charger(struct ricoh619_battery_info *info)
1039 info->chg_stat1 = 0;
1041 /* In suspend - disable charging */
1042 err = ricoh619_clr_bits(info->dev->parent,CHGCTL1_REG, 0x03);
1044 dev_err(info->dev, "Error in writing the control register\n");
1048 if (RICOH619_MAX_ADP_CURRENT != 0) {
1049 /* Change ADP Current to 2.5A. */
1050 err = ricoh619_write(info->dev->parent, 0xb6,
1051 (RICOH619_MAX_ADP_CURRENT-1)/100);
1053 dev_err(info->dev, "Error in writing INT_MSK_CHGSTS1 %d\n",
1059 if (RICOH619_MAX_USB_CURRENT != 0) {
1060 /* Set Max Change USB Current (0xB7) */
1061 err = ricoh619_write(info->dev->parent, REGISET2_REG,
1062 (RICOH619_MAX_USB_CURRENT-1)/100);
1065 "Error in writing RICOH619_MAX_USB_CURRENT %d\n", err);
1070 /* Set Charge competion current (0xB8) */
1071 /* this value for bit 4-0 */
1072 if (RICOH619_MAX_CHARGE_CURRENT != 0) {
1073 val = (RICOH619_MAX_CHARGE_CURRENT-1)/100;
1075 err = ricoh619_read(info->dev->parent, CHGISET_REG, &val);
1078 "Error in read RICOH619_MAX_CHARGE_CURRENT %d\n", err);
1084 /* Set Charge competion current (0xB8) */
1085 /* this value for bit 7-6 */
1086 if(RICOH619_CHARGE_COMPLETION_CURRENT != 0) {
1087 val2 = (RICOH619_CHARGE_COMPLETION_CURRENT - 50) / 50;
1089 err = ricoh619_read(info->dev->parent, CHGISET_REG, &val2);
1092 "Error in read RICOH619_MAX_CHARGE_CURRENT %d\n", err);
1097 val = val + (val2 << 6);
1098 err = ricoh619_write(info->dev->parent, CHGISET_REG, val);
1101 "Error in writing RICOH619_MAX_CHARGE_CURRENT %d\n", err);
1105 /* Change Charger Voltege to 4.2V. Recharge Point to 4.1V */
1106 /* for define FULL charging voltage (bit 6~4)*/
1107 if (RICOH619_FULL_CHARGING_VOLTAGE != 0) {
1108 if (RICOH619_FULL_CHARGING_VOLTAGE < 4050)
1110 else if (RICOH619_FULL_CHARGING_VOLTAGE > 4200)
1113 val2 = (RICOH619_FULL_CHARGING_VOLTAGE - 4050) / 50;
1115 err = ricoh619_read(info->dev->parent, BATSET2_REG, &val2);
1118 "Error in read RICOH619_FULL_CHARGE_VOLTAGE %d\n", err);
1124 /* for define re-charging voltage (bit 2~0)*/
1125 if (RICOH619_RE_CHARGING_VOLTAGE != 0) {
1126 if (RICOH619_RE_CHARGING_VOLTAGE < 3850)
1128 else if (RICOH619_RE_CHARGING_VOLTAGE > 4000)
1131 val = (RICOH619_RE_CHARGING_VOLTAGE - 3850) / 50;
1133 err = ricoh619_read(info->dev->parent, BATSET2_REG, &val);
1136 "Error in read RICOH619_RE_CHARGE_VOLTAGE %d\n", err);
1142 val = val + (val2 << 4);
1144 err = ricoh619_write(info->dev->parent, BATSET2_REG, val);
1146 dev_err(info->dev, "Error in writing RICOH619_RE_CHARGE_VOLTAGE %d\n",
1151 /* out suspend - enable charging */
1152 err = ricoh619_set_bits(info->dev->parent,CHGCTL1_REG, 0x03);
1154 dev_err(info->dev, "Error in writing the control register\n");
1158 /* Set rising edge setting ([1:0]=01b)for INT in charging */
1159 /* and rising edge setting ([3:2]=01b)for charge completion */
1160 err = ricoh619_read(info->dev->parent, RICOH619_CHG_STAT_DETMOD1, &val);
1162 dev_err(info->dev, "Error in reading CHG_STAT_DETMOD1 %d\n",
1168 err = ricoh619_write(info->dev->parent, RICOH619_CHG_STAT_DETMOD1, val);
1170 dev_err(info->dev, "Error in writing CHG_STAT_DETMOD1 %d\n",
1175 /* Unmask In charging/charge completion */
1176 err = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGSTS1, 0xfc);
1178 dev_err(info->dev, "Error in writing INT_MSK_CHGSTS1 %d\n",
1183 /* Set both edge for VUSB([3:2]=11b)/VADP([1:0]=11b) detect */
1184 err = ricoh619_read(info->dev->parent, RICOH619_CHG_CTRL_DETMOD1, &val);
1186 dev_err(info->dev, "Error in reading CHG_CTRL_DETMOD1 %d\n",
1192 err = ricoh619_write(info->dev->parent, RICOH619_CHG_CTRL_DETMOD1, val);
1194 dev_err(info->dev, "Error in writing CHG_CTRL_DETMOD1 %d\n",
1199 /* Unmask In VUSB/VADP completion */
1200 err = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGCTR, 0xfc);
1202 dev_err(info->dev, "Error in writing INT_MSK_CHGSTS1 %d\n",
1207 #ifdef ENABLE_LOW_BATTERY_DETECTION
1208 /* Set ADRQ=00 to stop ADC */
1209 ricoh619_write(info->dev->parent, RICOH619_ADC_CNT3, 0x0);
1210 /* Enable VSYS threshold Low interrupt */
1211 ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
1212 /* Set ADC auto conversion interval 250ms */
1213 ricoh619_write(info->dev->parent, RICOH619_ADC_CNT2, 0x0);
1214 /* Enable VSYS pin conversion in auto-ADC */
1215 ricoh619_write(info->dev->parent, RICOH619_ADC_CNT1, 0x10);
1216 /* Set VSYS threshold low voltage = 3.50v */
1217 ricoh619_write(info->dev->parent, RICOH619_ADC_VSYS_THL, 0x77);
1218 /* Start auto-mode & average 4-time conversion mode for ADC */
1219 ricoh619_write(info->dev->parent, RICOH619_ADC_CNT3, 0x28);
1220 /* Enable master ADC INT */
1221 ricoh619_set_bits(info->dev->parent, RICOH619_INTC_INTEN, ADC_INT);
1229 static int get_power_supply_status(struct ricoh619_battery_info *info)
1232 uint8_t supply_state;
1233 uint8_t charge_state;
1236 /* get power supply status */
1237 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &status);
1239 dev_err(info->dev, "Error in reading the control register\n");
1243 charge_state = (status & 0x1F);
1244 supply_state = ((status & 0xC0) >> 6);
1246 if (supply_state == SUPPLY_STATE_BAT) {
1247 info->soca->chg_status = POWER_SUPPLY_STATUS_DISCHARGING;
1249 switch (charge_state) {
1250 case CHG_STATE_CHG_OFF:
1251 info->soca->chg_status
1252 = POWER_SUPPLY_STATUS_DISCHARGING;
1254 case CHG_STATE_CHG_READY_VADP:
1255 info->soca->chg_status
1256 = POWER_SUPPLY_STATUS_NOT_CHARGING;
1258 case CHG_STATE_CHG_TRICKLE:
1259 info->soca->chg_status
1260 = POWER_SUPPLY_STATUS_CHARGING;
1262 case CHG_STATE_CHG_RAPID:
1263 info->soca->chg_status
1264 = POWER_SUPPLY_STATUS_CHARGING;
1266 case CHG_STATE_CHG_COMPLETE:
1267 info->soca->chg_status
1268 = POWER_SUPPLY_STATUS_FULL;
1270 case CHG_STATE_SUSPEND:
1271 info->soca->chg_status
1272 = POWER_SUPPLY_STATUS_DISCHARGING;
1274 case CHG_STATE_VCHG_OVER_VOL:
1275 info->soca->chg_status
1276 = POWER_SUPPLY_STATUS_DISCHARGING;
1278 case CHG_STATE_BAT_ERROR:
1279 info->soca->chg_status
1280 = POWER_SUPPLY_STATUS_NOT_CHARGING;
1282 case CHG_STATE_NO_BAT:
1283 info->soca->chg_status
1284 = POWER_SUPPLY_STATUS_NOT_CHARGING;
1286 case CHG_STATE_BAT_OVER_VOL:
1287 info->soca->chg_status
1288 = POWER_SUPPLY_STATUS_NOT_CHARGING;
1290 case CHG_STATE_BAT_TEMP_ERR:
1291 info->soca->chg_status
1292 = POWER_SUPPLY_STATUS_NOT_CHARGING;
1294 case CHG_STATE_DIE_ERR:
1295 info->soca->chg_status
1296 = POWER_SUPPLY_STATUS_NOT_CHARGING;
1298 case CHG_STATE_DIE_SHUTDOWN:
1299 info->soca->chg_status
1300 = POWER_SUPPLY_STATUS_DISCHARGING;
1302 case CHG_STATE_NO_BAT2:
1303 info->soca->chg_status
1304 = POWER_SUPPLY_STATUS_NOT_CHARGING;
1306 case CHG_STATE_CHG_READY_VUSB:
1307 info->soca->chg_status
1308 = POWER_SUPPLY_STATUS_NOT_CHARGING;
1311 info->soca->chg_status
1312 = POWER_SUPPLY_STATUS_UNKNOWN;
1317 return info->soca->chg_status;
1320 static void charger_irq_work(struct work_struct *work)
1322 struct ricoh619_battery_info *info
1323 = container_of(work, struct ricoh619_battery_info, irq_work);
1325 RICOH_FG_DBG("PMU:%s In\n", __func__);
1327 power_supply_changed(&info->battery);
1329 mutex_lock(&info->lock);
1331 info->chg_stat1 = 0;
1333 /* Enable Interrupt for VADP */
1334 ret = ricoh619_clr_bits(info->dev->parent,
1335 RICOH619_INT_MSK_CHGCTR, 0x01);
1338 "%s(): Error in enable charger mask INT %d\n",
1341 /* Enable Interrupt for Charging & complete */
1342 ret = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGSTS1, 0xfc);
1345 "%s(): Error in enable charger mask INT %d\n",
1348 mutex_unlock(&info->lock);
1349 RICOH_FG_DBG("PMU:%s Out\n", __func__);
1352 #ifdef ENABLE_LOW_BATTERY_DETECTION
1353 static void low_battery_irq_work(struct work_struct *work)
1355 struct ricoh619_battery_info *info = container_of(work,
1356 struct ricoh619_battery_info, low_battery_work.work);
1360 RICOH_FG_DBG("PMU:%s In\n", __func__);
1362 power_supply_changed(&info->battery);
1364 /* Enable VADP threshold Low interrupt */
1365 ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
1368 "%s(): Error in enable adc mask INT %d\n",
1373 extern int dwc_vbus_status(void);
1374 static void ricoh619_usb_charge_det(void)
1376 struct ricoh619 *ricoh619 = g_ricoh619;
1377 ricoh619_set_bits(ricoh619->dev,REGISET2_REG,(1 << 7)); //set usb limit current when SDP or other mode
1378 if(2 == dwc_vbus_status()){
1379 ricoh619_write(ricoh619->dev,REGISET2_REG,0x13); //set usb limit current 2A
1380 ricoh619_write(ricoh619->dev,CHGISET_REG,0x13); //set charge current 2A
1382 else if(1 == dwc_vbus_status()){
1383 ricoh619_write(ricoh619->dev,REGISET2_REG,0x04); //set usb limit current 500ma
1384 ricoh619_write(ricoh619->dev,CHGISET_REG,0x04); //set charge current 500ma
1388 static void usb_det_irq_work(struct work_struct *work)
1390 struct ricoh619_battery_info *info = container_of(work,
1391 struct ricoh619_battery_info, usb_irq_work);
1396 RICOH_FG_DBG("PMU:%s In\n", __func__);
1398 power_supply_changed(&info->battery);
1400 mutex_lock(&info->lock);
1402 /* Enable Interrupt for VUSB */
1403 ret = ricoh619_clr_bits(info->dev->parent,
1404 RICOH619_INT_MSK_CHGCTR, 0x02);
1407 "%s(): Error in enable charger mask INT %d\n",
1410 mutex_unlock(&info->lock);
1412 ret = ricoh619_read(info->dev->parent, RICOH619_INT_MON_CHGCTR, &sts);
1414 dev_err(info->dev, "Error in reading the control register\n");
1420 ricoh619_usb_charge_det();
1426 /*********************/
1428 /*********************/
1431 RICOH_FG_DBG("PMU:%s Out\n", __func__);
1434 extern void rk28_send_wakeup_key(void);
1435 static irqreturn_t charger_in_isr(int irq, void *battery_info)
1437 struct ricoh619_battery_info *info = battery_info;
1438 struct ricoh619 *ricoh619 = g_ricoh619;
1441 printk("PMU:%s\n", __func__);
1443 info->chg_stat1 |= 0x01;
1444 queue_work(info->workqueue, &info->irq_work);
1445 rk28_send_wakeup_key();
1447 ricoh619_read(ricoh619->dev, 0xbd, ®_val);
1448 if ( reg_val & 0x40)
1450 /* set adp limit current 2A */
1451 ricoh619_write(ricoh619->dev, 0xb6, 0x13);
1452 /* set charge current 2A */
1453 ricoh619_write(ricoh619->dev, 0xb8, 0x13);
1455 else if (reg_val & 0x80)
1457 queue_work(info->usb_workqueue, &info->usb_irq_work);
1462 static irqreturn_t charger_complete_isr(int irq, void *battery_info)
1464 struct ricoh619_battery_info *info = battery_info;
1465 printk("PMU:%s\n", __func__);
1467 info->chg_stat1 |= 0x02;
1468 queue_work(info->workqueue, &info->irq_work);
1473 static irqreturn_t charger_usb_isr(int irq, void *battery_info)
1475 struct ricoh619_battery_info *info = battery_info;
1476 printk("PMU:%s\n", __func__);
1478 info->chg_ctr |= 0x02;
1479 // queue_work(info->usb_workqueue, &info->usb_irq_work);
1480 rk28_send_wakeup_key();
1482 if (RICOH619_SOCA_UNSTABLE == info->soca->status
1483 || RICOH619_SOCA_FG_RESET == info->soca->status)
1484 info->soca->stable_count = 11;
1489 static irqreturn_t charger_adp_isr(int irq, void *battery_info)
1491 struct ricoh619_battery_info *info = battery_info;
1492 struct ricoh619 *ricoh619 = g_ricoh619;
1493 printk("PMU:%s\n", __func__);
1495 info->chg_ctr |= 0x01;
1496 queue_work(info->workqueue, &info->irq_work);
1497 /* clr usb det irq */
1498 ricoh619_clr_bits(ricoh619->dev, RICOH619_INT_IR_CHGCTR,
1500 /* set adp limit current 2A */
1501 // ricoh619_write(ricoh619->dev, 0xb6, 0x13);
1502 /* set charge current 2A */
1503 // ricoh619_write(ricoh619->dev, 0xb8, 0x13);
1504 rk28_send_wakeup_key();
1505 if (RICOH619_SOCA_UNSTABLE == info->soca->status
1506 || RICOH619_SOCA_FG_RESET == info->soca->status)
1507 info->soca->stable_count = 11;
1513 #ifdef ENABLE_LOW_BATTERY_DETECTION
1514 /*************************************************************/
1515 /* for Detecting Low Battery */
1516 /*************************************************************/
1518 static irqreturn_t adc_vsysl_isr(int irq, void *battery_info)
1521 struct ricoh619_battery_info *info = battery_info;
1524 RICOH_FG_DBG("PMU:%s\n", __func__);
1526 queue_delayed_work(info->monitor_wqueue, &info->low_battery_work,
1527 LOW_BATTERY_DETECTION_TIME*HZ);
1531 RICOH_FG_DBG("PMU:%s\n", __func__);
1532 ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
1533 rk28_send_wakeup_key();
1540 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
1541 static int get_check_fuel_gauge_reg(struct ricoh619_battery_info *info,
1542 int Reg_h, int Reg_l, int enable_bit)
1544 uint8_t get_data_h, get_data_l;
1545 int old_data, current_data;
1551 for (i = 0; i < 5 ; i++) {
1552 ret = ricoh619_read(info->dev->parent, Reg_h, &get_data_h);
1554 dev_err(info->dev, "Error in reading the control register\n");
1558 ret = ricoh619_read(info->dev->parent, Reg_l, &get_data_l);
1560 dev_err(info->dev, "Error in reading the control register\n");
1564 current_data = ((get_data_h & 0xff) << 8) | (get_data_l & 0xff);
1565 current_data = (current_data & enable_bit);
1567 if (current_data == old_data)
1568 return current_data;
1570 old_data = current_data;
1573 return current_data;
1576 static int calc_capacity(struct ricoh619_battery_info *info)
1582 /* get remaining battery capacity from fuel gauge */
1583 ret = ricoh619_read(info->dev->parent, SOC_REG, &capacity);
1585 dev_err(info->dev, "Error in reading the control register\n");
1591 return temp; /* Unit is 1% */
1594 static int get_battery_temp(struct ricoh619_battery_info *info)
1599 ret = get_check_fuel_gauge_reg(info, TEMP_1_REG, TEMP_2_REG, 0x0fff);
1601 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
1605 /* bit3 of 0xED(TEMP_1) is sign_bit */
1606 sign_bit = ((ret & 0x0800) >> 11);
1608 ret = (ret & 0x07ff);
1610 if (sign_bit == 0) /* positive value part */
1611 /* conversion unit */
1612 /* 1 unit is 0.0625 degree and retun unit
1613 * should be 0.1 degree,
1615 ret = ret * 625 / 1000;
1616 else /*negative value part */
1617 ret = -1 * ret * 625 / 1000;
1622 static int get_time_to_empty(struct ricoh619_battery_info *info)
1626 ret = get_check_fuel_gauge_reg(info, TT_EMPTY_H_REG, TT_EMPTY_L_REG,
1629 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
1633 /* conversion unit */
1634 /* 1unit is 1miniute and return nnit should be 1 second */
1640 static int get_time_to_full(struct ricoh619_battery_info *info)
1644 ret = get_check_fuel_gauge_reg(info, TT_FULL_H_REG, TT_FULL_L_REG,
1647 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
1656 /* battery voltage is get from Fuel gauge */
1657 static int measure_vbatt_FG(struct ricoh619_battery_info *info, int *data)
1661 ret = get_check_fuel_gauge_reg(info, VOLTAGE_1_REG, VOLTAGE_2_REG,
1664 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
1669 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
1670 *data = *data * 50000 / 4095;
1671 /* return unit should be 1uV */
1672 *data = *data * 100;
1677 static int measure_Ibatt_FG(struct ricoh619_battery_info *info, int *data)
1681 ret = get_check_fuel_gauge_reg(info, CC_AVERAGE1_REG,
1682 CC_AVERAGE0_REG, 0x3fff);
1684 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
1688 *data = (ret > 0x1fff) ? (ret - 0x4000) : ret;
1692 static int get_OCV_init_Data(struct ricoh619_battery_info *info, int index)
1695 ret = (battery_init_para[index*2]<<8) | (battery_init_para[index*2+1]);
1699 static int get_OCV_voltage(struct ricoh619_battery_info *info, int index)
1702 ret = get_OCV_init_Data(info, index);
1703 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
1704 ret = ret * 50000 / 4095;
1705 /* return unit should be 1uV */
1711 /* battery voltage is get from ADC */
1712 static int measure_vbatt_ADC(struct ricoh619_battery_info *info, int *data)
1715 uint8_t data_l = 0, data_h = 0;
1718 /* ADC interrupt enable */
1719 ret = ricoh619_set_bits(info->dev->parent, INTEN_REG, 0x08);
1721 dev_err(info->dev, "Error in setting the control register bit\n");
1725 /* enable interrupt request of single mode */
1726 ret = ricoh619_set_bits(info->dev->parent, EN_ADCIR3_REG, 0x01);
1728 dev_err(info->dev, "Error in setting the control register bit\n");
1732 /* single request */
1733 ret = ricoh619_write(info->dev->parent, ADCCNT3_REG, 0x10);
1735 dev_err(info->dev, "Error in writing the control register\n");
1739 for (i = 0; i < 5; i++) {
1741 RICOH_FG_DBG("ADC conversion times: %d\n", i);
1742 /* read completed flag of ADC */
1743 ret = ricoh619_read(info->dev->parent, EN_ADCIR3_REG, &data_h);
1745 dev_err(info->dev, "Error in reading the control register\n");
1753 dev_err(info->dev, "ADC conversion too long!\n");
1757 ret = ricoh619_read(info->dev->parent, VBATDATAH_REG, &data_h);
1759 dev_err(info->dev, "Error in reading the control register\n");
1763 ret = ricoh619_read(info->dev->parent, VBATDATAL_REG, &data_l);
1765 dev_err(info->dev, "Error in reading the control register\n");
1769 *data = ((data_h & 0xff) << 4) | (data_l & 0x0f);
1770 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
1771 *data = *data * 5000 / 4095;
1772 /* return unit should be 1uV */
1773 *data = *data * 1000;
1783 static void ricoh619_external_power_changed(struct power_supply *psy)
1785 struct ricoh619_battery_info *info;
1787 info = container_of(psy, struct ricoh619_battery_info, battery);
1788 queue_delayed_work(info->monitor_wqueue,
1789 &info->changed_work, HZ / 2);
1794 static int ricoh619_batt_get_prop(struct power_supply *psy,
1795 enum power_supply_property psp,
1796 union power_supply_propval *val)
1798 struct ricoh619_battery_info *info = dev_get_drvdata(psy->dev->parent);
1803 mutex_lock(&info->lock);
1806 case POWER_SUPPLY_PROP_ONLINE:
1807 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &status);
1809 dev_err(info->dev, "Error in reading the control register\n");
1810 mutex_unlock(&info->lock);
1813 if (psy->type == POWER_SUPPLY_TYPE_MAINS)
1814 val->intval = (status & 0x40 ? 1 : 0);
1815 else if (psy->type == POWER_SUPPLY_TYPE_USB)
1816 val->intval = (status & 0x80 ? 1 : 0);
1818 /* this setting is same as battery driver of 584 */
1819 case POWER_SUPPLY_PROP_STATUS:
1820 ret = get_power_supply_status(info);
1823 /* RICOH_FG_DBG("Power Supply Status is %d\n",
1827 /* this setting is same as battery driver of 584 */
1828 case POWER_SUPPLY_PROP_PRESENT:
1829 val->intval = info->present;
1832 /* current voltage is get from fuel gauge */
1833 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1834 /* return real vbatt Voltage */
1835 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
1836 if (info->soca->ready_fg)
1837 ret = measure_vbatt_FG(info, &data);
1839 val->intval = -EINVAL;
1840 RICOH_FG_DBG( "battery voltage is not ready\n");
1844 ret = measure_vbatt_ADC(info, &data);
1847 /* convert unit uV -> mV */
1848 info->cur_voltage = data / 1000;
1849 RICOH_FG_DBG( "battery voltage is %d mV\n",
1853 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
1854 /* current battery capacity is get from fuel gauge */
1855 case POWER_SUPPLY_PROP_CAPACITY:
1856 val->intval = (info->soca->displayed_soc + 50)/100;
1857 info->capacity = (info->soca->displayed_soc + 50)/100;
1858 /* RICOH_FG_DBG("battery capacity is %d%%\n",
1862 /* current temperature of battery */
1863 case POWER_SUPPLY_PROP_TEMP:
1864 if (info->soca->ready_fg) {
1865 ret = get_battery_temp(info);
1867 info->battery_temp = ret/10;
1868 RICOH_FG_DBG( "battery temperature is %d degree\n", info->battery_temp);
1870 val->intval = -EINVAL;
1871 RICOH_FG_DBG("battery temperature is not ready\n");
1875 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
1876 if (info->soca->ready_fg) {
1877 ret = get_time_to_empty(info);
1879 info->time_to_empty = ret/60;
1880 RICOH_FG_DBG("time of empty battery is %d minutes\n", info->time_to_empty);
1882 val->intval = -EINVAL;
1883 RICOH_FG_DBG( "time of empty battery is not ready\n");
1887 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
1888 if (info->soca->ready_fg) {
1889 ret = get_time_to_full(info);
1891 info->time_to_full = ret/60;
1892 RICOH_FG_DBG( "time of full battery is %d minutes\n", info->time_to_full);
1894 val->intval = -EINVAL;
1895 RICOH_FG_DBG("time of full battery is not ready\n");
1899 case POWER_SUPPLY_PROP_TECHNOLOGY:
1900 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
1904 case POWER_SUPPLY_PROP_HEALTH:
1905 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1910 mutex_unlock(&info->lock);
1914 mutex_unlock(&info->lock);
1919 static enum power_supply_property ricoh619_batt_props[] = {
1920 POWER_SUPPLY_PROP_STATUS,
1921 POWER_SUPPLY_PROP_PRESENT,
1922 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1924 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
1925 POWER_SUPPLY_PROP_CAPACITY,
1926 POWER_SUPPLY_PROP_TEMP,
1927 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
1928 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
1930 POWER_SUPPLY_PROP_TECHNOLOGY,
1931 POWER_SUPPLY_PROP_HEALTH,
1934 static enum power_supply_property ricoh619_power_props[] = {
1935 POWER_SUPPLY_PROP_ONLINE,
1938 struct power_supply powerac = {
1940 .type = POWER_SUPPLY_TYPE_MAINS,
1941 .properties = ricoh619_power_props,
1942 .num_properties = ARRAY_SIZE(ricoh619_power_props),
1943 .get_property = ricoh619_batt_get_prop,
1946 struct power_supply powerusb = {
1948 .type = POWER_SUPPLY_TYPE_USB,
1949 .properties = ricoh619_power_props,
1950 .num_properties = ARRAY_SIZE(ricoh619_power_props),
1951 .get_property = ricoh619_batt_get_prop,
1954 static __devinit int ricoh619_battery_probe(struct platform_device *pdev)
1956 struct ricoh619_battery_info *info;
1957 struct ricoh619_battery_platform_data *pdata;
1960 RICOH_FG_DBG("PMU: %s\n", __func__);
1962 info = kzalloc(sizeof(struct ricoh619_battery_info), GFP_KERNEL);
1965 info->soca = kzalloc(sizeof(struct ricoh619_soca_info), GFP_KERNEL);
1969 info->dev = &pdev->dev;
1970 info->status = POWER_SUPPLY_STATUS_CHARGING;
1971 pdata = pdev->dev.platform_data;
1972 info->monitor_time = pdata->monitor_time * HZ;
1973 info->alarm_vol_mv = pdata->alarm_vol_mv;
1974 info->adc_vdd_mv = ADC_VDD_MV; /* 2800; */
1975 info->min_voltage = MIN_VOLTAGE; /* 3100; */
1976 info->max_voltage = MAX_VOLTAGE; /* 4200; */
1978 info->entry_factory_mode = false;
1980 mutex_init(&info->lock);
1981 platform_set_drvdata(pdev, info);
1983 info->battery.name = "battery";
1984 info->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1985 info->battery.properties = ricoh619_batt_props;
1986 info->battery.num_properties = ARRAY_SIZE(ricoh619_batt_props);
1987 info->battery.get_property = ricoh619_batt_get_prop;
1988 info->battery.set_property = NULL;
1989 /* info->battery.external_power_changed
1990 = ricoh619_external_power_changed; */
1992 /* Disable Charger/ADC interrupt */
1993 ret = ricoh619_clr_bits(info->dev->parent, RICOH619_INTC_INTEN,
1998 ret = ricoh619_init_battery(info);
2002 #ifdef ENABLE_FACTORY_MODE
2003 info->factory_mode_wqueue
2004 = create_singlethread_workqueue("ricoh619_factory_mode");
2005 INIT_DELAYED_WORK_DEFERRABLE(&info->factory_mode_work,
2006 check_charging_state_work);
2008 ret = ricoh619_factory_mode(info);
2014 ret = power_supply_register(&pdev->dev, &info->battery);
2017 info->battery.dev->parent = &pdev->dev;
2019 ret = power_supply_register(&pdev->dev, &powerac);
2020 ret = power_supply_register(&pdev->dev, &powerusb);
2022 info->monitor_wqueue
2023 = create_singlethread_workqueue("ricoh619_battery_monitor");
2024 INIT_DELAYED_WORK_DEFERRABLE(&info->monitor_work,
2025 ricoh619_battery_work);
2026 INIT_DELAYED_WORK_DEFERRABLE(&info->displayed_work,
2027 ricoh619_displayed_work);
2028 INIT_DELAYED_WORK_DEFERRABLE(&info->charge_stable_work,
2029 ricoh619_stable_charge_countdown_work);
2030 INIT_DELAYED_WORK(&info->changed_work, ricoh619_changed_work);
2031 queue_delayed_work(info->monitor_wqueue, &info->monitor_work,
2032 RICOH619_MONITOR_START_TIME*HZ);
2035 /* Charger IRQ workqueue settings */
2036 charger_irq = pdata->irq;
2038 info->workqueue = create_singlethread_workqueue("rc5t619_charger_in");
2039 INIT_WORK(&info->irq_work, charger_irq_work);
2041 ret = request_threaded_irq(charger_irq + RICOH619_IRQ_FONCHGINT,
2042 NULL, charger_in_isr, IRQF_ONESHOT,
2043 "rc5t619_charger_in", info);
2045 dev_err(&pdev->dev, "Can't get CHG_INT IRQ for chrager: %d\n",
2049 info->workqueue = create_singlethread_workqueue("rc5t619_charger_in");
2050 INIT_WORK(&info->irq_work, charger_irq_work);
2052 ret = request_threaded_irq(charger_irq + RICOH619_IRQ_FCHGCMPINT,
2053 NULL, charger_complete_isr,
2054 IRQF_ONESHOT, "rc5t619_charger_comp",
2057 dev_err(&pdev->dev, "Can't get CHG_COMP IRQ for chrager: %d\n",
2062 ret = request_threaded_irq(charger_irq + RICOH619_IRQ_FVUSBDETSINT,
2063 NULL, charger_usb_isr, IRQF_ONESHOT,
2064 "rc5t619_usb_det", info);
2066 dev_err(&pdev->dev, "Can't get USB_DET IRQ for chrager: %d\n",
2071 ret = request_threaded_irq(charger_irq + RICOH619_IRQ_FVADPDETSINT,
2072 NULL, charger_adp_isr, IRQF_ONESHOT,
2073 "rc5t619_adp_det", info);
2076 "Can't get ADP_DET IRQ for chrager: %d\n", ret);
2081 = create_singlethread_workqueue("rc5t619_usb_det");
2082 INIT_WORK(&info->usb_irq_work, usb_det_irq_work);
2085 #ifdef ENABLE_LOW_BATTERY_DETECTION
2086 ret = request_threaded_irq(charger_irq + RICOH619_IRQ_VSYSLIR,
2087 NULL, adc_vsysl_isr, IRQF_ONESHOT,
2088 "rc5t619_adc_vsysl", info);
2091 "Can't get ADC_VSYSL IRQ for chrager: %d\n", ret);
2094 INIT_DELAYED_WORK_DEFERRABLE(&info->low_battery_work,
2095 low_battery_irq_work);
2098 /* Charger init and IRQ setting */
2099 ret = ricoh619_init_charger(info);
2103 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
2104 ret = ricoh619_init_fgsoca(info);
2107 /* Enable Charger interrupt */
2108 ricoh619_set_bits(info->dev->parent, RICOH619_INTC_INTEN, CHG_INT);
2117 static int __devexit ricoh619_battery_remove(struct platform_device *pdev)
2119 struct ricoh619_battery_info *info = platform_get_drvdata(pdev);
2124 bool is_charging = true;
2127 && (info->soca->status == RICOH619_SOCA_STABLE)) {
2128 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
2130 dev_err(info->dev, "Error in writing PSWR_REG\n");
2133 val = (info->soca->displayed_soc + 50)/100;
2135 ret = ricoh619_write(info->dev->parent, PSWR_REG, val);
2137 dev_err(info->dev, "Error in writing PSWR_REG\n");
2139 g_soc = (info->soca->displayed_soc + 50)/100;
2141 ret = calc_capacity_in_period(info, &cc_cap, &is_charging);
2143 dev_err(info->dev, "Read cc_sum Error !!-----\n");
2146 if (g_fg_on_mode == 0) {
2147 ret = ricoh619_clr_bits(info->dev->parent,
2150 dev_err(info->dev, "Error in clr FG EN\n");
2153 cancel_delayed_work(&info->monitor_work);
2154 cancel_delayed_work(&info->charge_stable_work);
2155 cancel_delayed_work(&info->changed_work);
2156 #ifdef ENABLE_LOW_BATTERY_DETECTION
2157 cancel_delayed_work(&info->low_battery_work);
2160 flush_work(&info->irq_work);
2161 flush_work(&info->usb_irq_work);
2163 flush_workqueue(info->monitor_wqueue);
2164 flush_workqueue(info->workqueue);
2165 flush_workqueue(info->usb_workqueue);
2167 destroy_workqueue(info->monitor_wqueue);
2168 destroy_workqueue(info->workqueue);
2169 destroy_workqueue(info->usb_workqueue);
2171 power_supply_unregister(&info->battery);
2173 platform_set_drvdata(pdev, NULL);
2178 static int ricoh619_battery_suspend(struct device *dev)
2180 struct ricoh619_battery_info *info = dev_get_drvdata(dev);
2185 bool is_charging = true;
2188 && (info->soca->status == RICOH619_SOCA_STABLE)) {
2189 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
2191 dev_err(info->dev, "Error in writing PSWR_REG\n");
2194 val = (info->soca->displayed_soc + 50)/100;
2196 ret = ricoh619_write(info->dev->parent, PSWR_REG, val);
2198 dev_err(info->dev, "Error in writing PSWR_REG\n");
2200 g_soc = (info->soca->displayed_soc + 50)/100;
2202 ret = calc_capacity_in_period(info, &cc_cap, &is_charging);
2204 dev_err(info->dev, "Read cc_sum Error !!-----\n");
2207 disable_irq(charger_irq + RICOH619_IRQ_FONCHGINT);
2208 disable_irq(charger_irq + RICOH619_IRQ_FCHGCMPINT);
2209 disable_irq(charger_irq + RICOH619_IRQ_FVUSBDETSINT);
2210 disable_irq(charger_irq + RICOH619_IRQ_FVADPDETSINT);
2211 #ifdef ENABLE_LOW_BATTERY_DETECTION
2212 disable_irq(charger_irq + RICOH619_IRQ_VSYSLIR);
2215 cancel_delayed_work_sync(&info->monitor_work);
2216 cancel_delayed_work_sync(&info->displayed_work);
2217 cancel_delayed_work_sync(&info->charge_stable_work);
2218 cancel_delayed_work_sync(&info->changed_work);
2219 #ifdef ENABLE_LOW_BATTERY_DETECTION
2220 cancel_delayed_work_sync(&info->low_battery_work);
2222 cancel_work_sync(&info->irq_work);
2223 cancel_work_sync(&info->usb_irq_work);
2225 flush_workqueue(info->monitor_wqueue);
2226 flush_workqueue(info->workqueue);
2227 flush_workqueue(info->usb_workqueue);
2232 static int ricoh619_battery_resume(struct device *dev)
2234 struct ricoh619_battery_info *info = dev_get_drvdata(dev);
2237 int displayed_soc_temp;
2239 bool is_charging = true;
2242 if (info->entry_factory_mode) {
2243 info->soca->displayed_soc = -EINVAL;
2244 } else if (RICOH619_SOCA_STABLE == info->soca->status) {
2245 info->soca->soc = calc_capacity(info) * 100;
2246 info->soca->displayed_soc = info->soca->soc;
2247 } else if (RICOH619_SOCA_ZERO == info->soca->status) {
2248 if (calc_ocv(info) > get_OCV_voltage(info, 0)) {
2249 ret = ricoh619_read(info->dev->parent, PSWR_REG, &val);
2251 info->soca->soc = val * 100;
2252 if ((ret < 0) || (val == 0)) {
2254 "Error in reading PSWR_REG %d\n", ret);
2256 = calc_capacity(info) * 100 + 50;
2259 ret = calc_capacity_in_period(info, &cc_cap,
2262 dev_err(info->dev, "Read cc_sum Error !!-----\n");
2264 info->soca->cc_delta
2265 = (is_charging == true) ? cc_cap : -cc_cap;
2268 = info->soca->soc + info->soca->cc_delta;
2269 displayed_soc_temp = min(10000, displayed_soc_temp);
2270 displayed_soc_temp = max(0, displayed_soc_temp);
2271 info->soca->displayed_soc = displayed_soc_temp;
2273 ret = ricoh619_write(info->dev->parent,
2276 dev_err(info->dev, "Error in writing the control register\n");
2277 info->soca->ready_fg = 0;
2278 info->soca->status = RICOH619_SOCA_FG_RESET;
2281 info->soca->displayed_soc = 0;
2283 ret = ricoh619_read(info->dev->parent, PSWR_REG, &val);
2285 info->soca->soc = val * 100;
2286 if ((ret < 0) || (val == 0)) {
2288 "Error in reading PSWR_REG %d\n", ret);
2290 = calc_capacity(info) * 100 + 50;
2293 ret = calc_capacity_in_period(info, &cc_cap, &is_charging);
2295 dev_err(info->dev, "Read cc_sum Error !!-----\n");
2297 info->soca->cc_delta = (is_charging == true) ? cc_cap : -cc_cap;
2299 displayed_soc_temp = info->soca->soc + info->soca->cc_delta;
2300 displayed_soc_temp = min(10000, displayed_soc_temp);
2301 displayed_soc_temp = max(0, displayed_soc_temp);
2302 info->soca->displayed_soc = displayed_soc_temp;
2303 if (RICOH619_SOCA_DISP == info->soca->status)
2304 info->soca->last_soc = calc_capacity(info) * 100;
2307 power_supply_changed(&info->battery);
2308 queue_delayed_work(info->monitor_wqueue, &info->displayed_work, HZ);
2310 if (RICOH619_SOCA_UNSTABLE == info->soca->status) {
2311 info->soca->stable_count = 10;
2312 queue_delayed_work(info->monitor_wqueue,
2313 &info->charge_stable_work,
2314 RICOH619_FG_STABLE_TIME*HZ/10);
2315 } else if (RICOH619_SOCA_FG_RESET == info->soca->status) {
2316 info->soca->stable_count = 1;
2318 for (i = 0; i < 3; i = i+1)
2319 info->soca->reset_soc[i] = 0;
2320 info->soca->reset_count = 0;
2322 queue_delayed_work(info->monitor_wqueue,
2323 &info->charge_stable_work,
2324 RICOH619_FG_RESET_TIME*HZ);
2327 queue_delayed_work(info->monitor_wqueue, &info->monitor_work,
2328 info->monitor_time);
2330 enable_irq(charger_irq + RICOH619_IRQ_FONCHGINT);
2331 enable_irq(charger_irq + RICOH619_IRQ_FCHGCMPINT);
2332 enable_irq(charger_irq + RICOH619_IRQ_FVUSBDETSINT);
2333 enable_irq(charger_irq + RICOH619_IRQ_FVADPDETSINT);
2334 #ifdef ENABLE_LOW_BATTERY_DETECTION
2335 enable_irq(charger_irq + RICOH619_IRQ_VSYSLIR);
2340 static const struct dev_pm_ops ricoh619_battery_pm_ops = {
2341 .suspend = ricoh619_battery_suspend,
2342 .resume = ricoh619_battery_resume,
2346 static struct platform_driver ricoh619_battery_driver = {
2348 .name = "ricoh619-battery",
2349 .owner = THIS_MODULE,
2351 .pm = &ricoh619_battery_pm_ops,
2354 .probe = ricoh619_battery_probe,
2355 .remove = __devexit_p(ricoh619_battery_remove),
2358 static int __init ricoh619_battery_init(void)
2360 RICOH_FG_DBG("PMU: %s\n", __func__);
2361 return platform_driver_register(&ricoh619_battery_driver);
2363 subsys_initcall_sync(ricoh619_battery_init);
2365 static void __exit ricoh619_battery_exit(void)
2367 platform_driver_unregister(&ricoh619_battery_driver);
2369 module_exit(ricoh619_battery_exit);
2371 MODULE_DESCRIPTION("RICOH619 Battery driver");
2372 MODULE_ALIAS("platform:ricoh619-battery");
2373 MODULE_LICENSE("GPL");