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 static void usb_det_irq_work(struct work_struct *work)
1375 struct ricoh619_battery_info *info = container_of(work,
1376 struct ricoh619_battery_info, usb_irq_work);
1381 RICOH_FG_DBG("PMU:%s In\n", __func__);
1383 power_supply_changed(&info->battery);
1385 mutex_lock(&info->lock);
1387 /* Enable Interrupt for VUSB */
1388 ret = ricoh619_clr_bits(info->dev->parent,
1389 RICOH619_INT_MSK_CHGCTR, 0x02);
1392 "%s(): Error in enable charger mask INT %d\n",
1395 mutex_unlock(&info->lock);
1397 ret = ricoh619_read(info->dev->parent, RICOH619_INT_MON_CHGCTR, &sts);
1399 dev_err(info->dev, "Error in reading the control register\n");
1405 /*********************/
1407 /*********************/
1410 RICOH_FG_DBG("PMU:%s Out\n", __func__);
1413 extern void rk28_send_wakeup_key(void);
1414 static irqreturn_t charger_in_isr(int irq, void *battery_info)
1416 struct ricoh619_battery_info *info = battery_info;
1417 printk("PMU:%s\n", __func__);
1419 info->chg_stat1 |= 0x01;
1420 queue_work(info->workqueue, &info->irq_work);
1421 rk28_send_wakeup_key();
1425 static irqreturn_t charger_complete_isr(int irq, void *battery_info)
1427 struct ricoh619_battery_info *info = battery_info;
1428 printk("PMU:%s\n", __func__);
1430 info->chg_stat1 |= 0x02;
1431 queue_work(info->workqueue, &info->irq_work);
1436 static irqreturn_t charger_usb_isr(int irq, void *battery_info)
1438 struct ricoh619_battery_info *info = battery_info;
1439 printk("PMU:%s\n", __func__);
1441 info->chg_ctr |= 0x02;
1442 queue_work(info->usb_workqueue, &info->usb_irq_work);
1443 rk28_send_wakeup_key();
1445 if (RICOH619_SOCA_UNSTABLE == info->soca->status
1446 || RICOH619_SOCA_FG_RESET == info->soca->status)
1447 info->soca->stable_count = 11;
1452 static irqreturn_t charger_adp_isr(int irq, void *battery_info)
1454 struct ricoh619_battery_info *info = battery_info;
1455 struct ricoh619 *ricoh619 = g_ricoh619;
1456 printk("PMU:%s\n", __func__);
1458 info->chg_ctr |= 0x01;
1459 queue_work(info->workqueue, &info->irq_work);
1460 rk28_send_wakeup_key();
1461 /* clr usb det irq */
1462 ricoh619_clr_bits(ricoh619->dev, RICOH619_INT_IR_CHGCTR,
1465 /* set adp limit current 2A */
1466 ricoh619_write(ricoh619->dev, 0xb6, 0x13);
1467 /* set charge current 2A */
1468 ricoh619_write(ricoh619->dev, 0xb8, 0x13);
1470 if (RICOH619_SOCA_UNSTABLE == info->soca->status
1471 || RICOH619_SOCA_FG_RESET == info->soca->status)
1472 info->soca->stable_count = 11;
1478 #ifdef ENABLE_LOW_BATTERY_DETECTION
1479 /*************************************************************/
1480 /* for Detecting Low Battery */
1481 /*************************************************************/
1483 static irqreturn_t adc_vsysl_isr(int irq, void *battery_info)
1486 struct ricoh619_battery_info *info = battery_info;
1489 RICOH_FG_DBG("PMU:%s\n", __func__);
1491 queue_delayed_work(info->monitor_wqueue, &info->low_battery_work,
1492 LOW_BATTERY_DETECTION_TIME*HZ);
1496 RICOH_FG_DBG("PMU:%s\n", __func__);
1497 ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
1498 rk28_send_wakeup_key();
1505 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
1506 static int get_check_fuel_gauge_reg(struct ricoh619_battery_info *info,
1507 int Reg_h, int Reg_l, int enable_bit)
1509 uint8_t get_data_h, get_data_l;
1510 int old_data, current_data;
1516 for (i = 0; i < 5 ; i++) {
1517 ret = ricoh619_read(info->dev->parent, Reg_h, &get_data_h);
1519 dev_err(info->dev, "Error in reading the control register\n");
1523 ret = ricoh619_read(info->dev->parent, Reg_l, &get_data_l);
1525 dev_err(info->dev, "Error in reading the control register\n");
1529 current_data = ((get_data_h & 0xff) << 8) | (get_data_l & 0xff);
1530 current_data = (current_data & enable_bit);
1532 if (current_data == old_data)
1533 return current_data;
1535 old_data = current_data;
1538 return current_data;
1541 static int calc_capacity(struct ricoh619_battery_info *info)
1547 /* get remaining battery capacity from fuel gauge */
1548 ret = ricoh619_read(info->dev->parent, SOC_REG, &capacity);
1550 dev_err(info->dev, "Error in reading the control register\n");
1556 return temp; /* Unit is 1% */
1559 static int get_battery_temp(struct ricoh619_battery_info *info)
1564 ret = get_check_fuel_gauge_reg(info, TEMP_1_REG, TEMP_2_REG, 0x0fff);
1566 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
1570 /* bit3 of 0xED(TEMP_1) is sign_bit */
1571 sign_bit = ((ret & 0x0800) >> 11);
1573 ret = (ret & 0x07ff);
1575 if (sign_bit == 0) /* positive value part */
1576 /* conversion unit */
1577 /* 1 unit is 0.0625 degree and retun unit
1578 * should be 0.1 degree,
1580 ret = ret * 625 / 1000;
1581 else /*negative value part */
1582 ret = -1 * ret * 625 / 1000;
1587 static int get_time_to_empty(struct ricoh619_battery_info *info)
1591 ret = get_check_fuel_gauge_reg(info, TT_EMPTY_H_REG, TT_EMPTY_L_REG,
1594 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
1598 /* conversion unit */
1599 /* 1unit is 1miniute and return nnit should be 1 second */
1605 static int get_time_to_full(struct ricoh619_battery_info *info)
1609 ret = get_check_fuel_gauge_reg(info, TT_FULL_H_REG, TT_FULL_L_REG,
1612 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
1621 /* battery voltage is get from Fuel gauge */
1622 static int measure_vbatt_FG(struct ricoh619_battery_info *info, int *data)
1626 ret = get_check_fuel_gauge_reg(info, VOLTAGE_1_REG, VOLTAGE_2_REG,
1629 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
1634 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
1635 *data = *data * 50000 / 4095;
1636 /* return unit should be 1uV */
1637 *data = *data * 100;
1642 static int measure_Ibatt_FG(struct ricoh619_battery_info *info, int *data)
1646 ret = get_check_fuel_gauge_reg(info, CC_AVERAGE1_REG,
1647 CC_AVERAGE0_REG, 0x3fff);
1649 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
1653 *data = (ret > 0x1fff) ? (ret - 0x4000) : ret;
1657 static int get_OCV_init_Data(struct ricoh619_battery_info *info, int index)
1660 ret = (battery_init_para[index*2]<<8) | (battery_init_para[index*2+1]);
1664 static int get_OCV_voltage(struct ricoh619_battery_info *info, int index)
1667 ret = get_OCV_init_Data(info, index);
1668 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
1669 ret = ret * 50000 / 4095;
1670 /* return unit should be 1uV */
1676 /* battery voltage is get from ADC */
1677 static int measure_vbatt_ADC(struct ricoh619_battery_info *info, int *data)
1680 uint8_t data_l = 0, data_h = 0;
1683 /* ADC interrupt enable */
1684 ret = ricoh619_set_bits(info->dev->parent, INTEN_REG, 0x08);
1686 dev_err(info->dev, "Error in setting the control register bit\n");
1690 /* enable interrupt request of single mode */
1691 ret = ricoh619_set_bits(info->dev->parent, EN_ADCIR3_REG, 0x01);
1693 dev_err(info->dev, "Error in setting the control register bit\n");
1697 /* single request */
1698 ret = ricoh619_write(info->dev->parent, ADCCNT3_REG, 0x10);
1700 dev_err(info->dev, "Error in writing the control register\n");
1704 for (i = 0; i < 5; i++) {
1706 RICOH_FG_DBG("ADC conversion times: %d\n", i);
1707 /* read completed flag of ADC */
1708 ret = ricoh619_read(info->dev->parent, EN_ADCIR3_REG, &data_h);
1710 dev_err(info->dev, "Error in reading the control register\n");
1718 dev_err(info->dev, "ADC conversion too long!\n");
1722 ret = ricoh619_read(info->dev->parent, VBATDATAH_REG, &data_h);
1724 dev_err(info->dev, "Error in reading the control register\n");
1728 ret = ricoh619_read(info->dev->parent, VBATDATAL_REG, &data_l);
1730 dev_err(info->dev, "Error in reading the control register\n");
1734 *data = ((data_h & 0xff) << 4) | (data_l & 0x0f);
1735 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
1736 *data = *data * 5000 / 4095;
1737 /* return unit should be 1uV */
1738 *data = *data * 1000;
1748 static void ricoh619_external_power_changed(struct power_supply *psy)
1750 struct ricoh619_battery_info *info;
1752 info = container_of(psy, struct ricoh619_battery_info, battery);
1753 queue_delayed_work(info->monitor_wqueue,
1754 &info->changed_work, HZ / 2);
1759 static int ricoh619_batt_get_prop(struct power_supply *psy,
1760 enum power_supply_property psp,
1761 union power_supply_propval *val)
1763 struct ricoh619_battery_info *info = dev_get_drvdata(psy->dev->parent);
1768 mutex_lock(&info->lock);
1771 case POWER_SUPPLY_PROP_ONLINE:
1772 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &status);
1774 dev_err(info->dev, "Error in reading the control register\n");
1775 mutex_unlock(&info->lock);
1778 if (psy->type == POWER_SUPPLY_TYPE_MAINS)
1779 val->intval = (status & 0x40 ? 1 : 0);
1780 else if (psy->type == POWER_SUPPLY_TYPE_USB)
1781 val->intval = (status & 0x80 ? 1 : 0);
1783 /* this setting is same as battery driver of 584 */
1784 case POWER_SUPPLY_PROP_STATUS:
1785 ret = get_power_supply_status(info);
1788 /* RICOH_FG_DBG("Power Supply Status is %d\n",
1792 /* this setting is same as battery driver of 584 */
1793 case POWER_SUPPLY_PROP_PRESENT:
1794 val->intval = info->present;
1797 /* current voltage is get from fuel gauge */
1798 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1799 /* return real vbatt Voltage */
1800 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
1801 if (info->soca->ready_fg)
1802 ret = measure_vbatt_FG(info, &data);
1804 val->intval = -EINVAL;
1805 RICOH_FG_DBG( "battery voltage is not ready\n");
1809 ret = measure_vbatt_ADC(info, &data);
1812 /* convert unit uV -> mV */
1813 info->cur_voltage = data / 1000;
1814 RICOH_FG_DBG( "battery voltage is %d mV\n",
1818 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
1819 /* current battery capacity is get from fuel gauge */
1820 case POWER_SUPPLY_PROP_CAPACITY:
1821 val->intval = (info->soca->displayed_soc + 50)/100;
1822 info->capacity = (info->soca->displayed_soc + 50)/100;
1823 /* RICOH_FG_DBG("battery capacity is %d%%\n",
1827 /* current temperature of battery */
1828 case POWER_SUPPLY_PROP_TEMP:
1829 if (info->soca->ready_fg) {
1830 ret = get_battery_temp(info);
1832 info->battery_temp = ret/10;
1833 RICOH_FG_DBG( "battery temperature is %d degree\n", info->battery_temp);
1835 val->intval = -EINVAL;
1836 RICOH_FG_DBG("battery temperature is not ready\n");
1840 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
1841 if (info->soca->ready_fg) {
1842 ret = get_time_to_empty(info);
1844 info->time_to_empty = ret/60;
1845 RICOH_FG_DBG("time of empty battery is %d minutes\n", info->time_to_empty);
1847 val->intval = -EINVAL;
1848 RICOH_FG_DBG( "time of empty battery is not ready\n");
1852 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
1853 if (info->soca->ready_fg) {
1854 ret = get_time_to_full(info);
1856 info->time_to_full = ret/60;
1857 RICOH_FG_DBG( "time of full battery is %d minutes\n", info->time_to_full);
1859 val->intval = -EINVAL;
1860 RICOH_FG_DBG("time of full battery is not ready\n");
1864 case POWER_SUPPLY_PROP_TECHNOLOGY:
1865 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
1869 case POWER_SUPPLY_PROP_HEALTH:
1870 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1875 mutex_unlock(&info->lock);
1879 mutex_unlock(&info->lock);
1884 static enum power_supply_property ricoh619_batt_props[] = {
1885 POWER_SUPPLY_PROP_STATUS,
1886 POWER_SUPPLY_PROP_PRESENT,
1887 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1889 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
1890 POWER_SUPPLY_PROP_CAPACITY,
1891 POWER_SUPPLY_PROP_TEMP,
1892 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
1893 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
1895 POWER_SUPPLY_PROP_TECHNOLOGY,
1896 POWER_SUPPLY_PROP_HEALTH,
1899 static enum power_supply_property ricoh619_power_props[] = {
1900 POWER_SUPPLY_PROP_ONLINE,
1903 struct power_supply powerac = {
1905 .type = POWER_SUPPLY_TYPE_MAINS,
1906 .properties = ricoh619_power_props,
1907 .num_properties = ARRAY_SIZE(ricoh619_power_props),
1908 .get_property = ricoh619_batt_get_prop,
1911 struct power_supply powerusb = {
1913 .type = POWER_SUPPLY_TYPE_USB,
1914 .properties = ricoh619_power_props,
1915 .num_properties = ARRAY_SIZE(ricoh619_power_props),
1916 .get_property = ricoh619_batt_get_prop,
1919 static __devinit int ricoh619_battery_probe(struct platform_device *pdev)
1921 struct ricoh619_battery_info *info;
1922 struct ricoh619_battery_platform_data *pdata;
1925 RICOH_FG_DBG("PMU: %s\n", __func__);
1927 info = kzalloc(sizeof(struct ricoh619_battery_info), GFP_KERNEL);
1930 info->soca = kzalloc(sizeof(struct ricoh619_soca_info), GFP_KERNEL);
1934 info->dev = &pdev->dev;
1935 info->status = POWER_SUPPLY_STATUS_CHARGING;
1936 pdata = pdev->dev.platform_data;
1937 info->monitor_time = pdata->monitor_time * HZ;
1938 info->alarm_vol_mv = pdata->alarm_vol_mv;
1939 info->adc_vdd_mv = ADC_VDD_MV; /* 2800; */
1940 info->min_voltage = MIN_VOLTAGE; /* 3100; */
1941 info->max_voltage = MAX_VOLTAGE; /* 4200; */
1943 info->entry_factory_mode = false;
1945 mutex_init(&info->lock);
1946 platform_set_drvdata(pdev, info);
1948 info->battery.name = "battery";
1949 info->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1950 info->battery.properties = ricoh619_batt_props;
1951 info->battery.num_properties = ARRAY_SIZE(ricoh619_batt_props);
1952 info->battery.get_property = ricoh619_batt_get_prop;
1953 info->battery.set_property = NULL;
1954 /* info->battery.external_power_changed
1955 = ricoh619_external_power_changed; */
1957 /* Disable Charger/ADC interrupt */
1958 ret = ricoh619_clr_bits(info->dev->parent, RICOH619_INTC_INTEN,
1963 ret = ricoh619_init_battery(info);
1967 #ifdef ENABLE_FACTORY_MODE
1968 info->factory_mode_wqueue
1969 = create_singlethread_workqueue("ricoh619_factory_mode");
1970 INIT_DELAYED_WORK_DEFERRABLE(&info->factory_mode_work,
1971 check_charging_state_work);
1973 ret = ricoh619_factory_mode(info);
1979 ret = power_supply_register(&pdev->dev, &info->battery);
1982 info->battery.dev->parent = &pdev->dev;
1984 ret = power_supply_register(&pdev->dev, &powerac);
1985 ret = power_supply_register(&pdev->dev, &powerusb);
1987 info->monitor_wqueue
1988 = create_singlethread_workqueue("ricoh619_battery_monitor");
1989 INIT_DELAYED_WORK_DEFERRABLE(&info->monitor_work,
1990 ricoh619_battery_work);
1991 INIT_DELAYED_WORK_DEFERRABLE(&info->displayed_work,
1992 ricoh619_displayed_work);
1993 INIT_DELAYED_WORK_DEFERRABLE(&info->charge_stable_work,
1994 ricoh619_stable_charge_countdown_work);
1995 INIT_DELAYED_WORK(&info->changed_work, ricoh619_changed_work);
1996 queue_delayed_work(info->monitor_wqueue, &info->monitor_work,
1997 RICOH619_MONITOR_START_TIME*HZ);
2000 /* Charger IRQ workqueue settings */
2001 charger_irq = pdata->irq;
2003 info->workqueue = create_singlethread_workqueue("rc5t619_charger_in");
2004 INIT_WORK(&info->irq_work, charger_irq_work);
2006 ret = request_threaded_irq(charger_irq + RICOH619_IRQ_FONCHGINT,
2007 NULL, charger_in_isr, IRQF_ONESHOT,
2008 "rc5t619_charger_in", info);
2010 dev_err(&pdev->dev, "Can't get CHG_INT IRQ for chrager: %d\n",
2014 info->workqueue = create_singlethread_workqueue("rc5t619_charger_in");
2015 INIT_WORK(&info->irq_work, charger_irq_work);
2017 ret = request_threaded_irq(charger_irq + RICOH619_IRQ_FCHGCMPINT,
2018 NULL, charger_complete_isr,
2019 IRQF_ONESHOT, "rc5t619_charger_comp",
2022 dev_err(&pdev->dev, "Can't get CHG_COMP IRQ for chrager: %d\n",
2027 ret = request_threaded_irq(charger_irq + RICOH619_IRQ_FVUSBDETSINT,
2028 NULL, charger_usb_isr, IRQF_ONESHOT,
2029 "rc5t619_usb_det", info);
2031 dev_err(&pdev->dev, "Can't get USB_DET IRQ for chrager: %d\n",
2036 ret = request_threaded_irq(charger_irq + RICOH619_IRQ_FVADPDETSINT,
2037 NULL, charger_adp_isr, IRQF_ONESHOT,
2038 "rc5t619_adp_det", info);
2041 "Can't get ADP_DET IRQ for chrager: %d\n", ret);
2046 = create_singlethread_workqueue("rc5t619_usb_det");
2047 INIT_WORK(&info->usb_irq_work, usb_det_irq_work);
2050 #ifdef ENABLE_LOW_BATTERY_DETECTION
2051 ret = request_threaded_irq(charger_irq + RICOH619_IRQ_VSYSLIR,
2052 NULL, adc_vsysl_isr, IRQF_ONESHOT,
2053 "rc5t619_adc_vsysl", info);
2056 "Can't get ADC_VSYSL IRQ for chrager: %d\n", ret);
2059 INIT_DELAYED_WORK_DEFERRABLE(&info->low_battery_work,
2060 low_battery_irq_work);
2063 /* Charger init and IRQ setting */
2064 ret = ricoh619_init_charger(info);
2068 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
2069 ret = ricoh619_init_fgsoca(info);
2072 /* Enable Charger interrupt */
2073 ricoh619_set_bits(info->dev->parent, RICOH619_INTC_INTEN, CHG_INT);
2082 static int __devexit ricoh619_battery_remove(struct platform_device *pdev)
2084 struct ricoh619_battery_info *info = platform_get_drvdata(pdev);
2089 bool is_charging = true;
2092 && (info->soca->status == RICOH619_SOCA_STABLE)) {
2093 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
2095 dev_err(info->dev, "Error in writing PSWR_REG\n");
2098 val = (info->soca->displayed_soc + 50)/100;
2100 ret = ricoh619_write(info->dev->parent, PSWR_REG, val);
2102 dev_err(info->dev, "Error in writing PSWR_REG\n");
2104 g_soc = (info->soca->displayed_soc + 50)/100;
2106 ret = calc_capacity_in_period(info, &cc_cap, &is_charging);
2108 dev_err(info->dev, "Read cc_sum Error !!-----\n");
2111 if (g_fg_on_mode == 0) {
2112 ret = ricoh619_clr_bits(info->dev->parent,
2115 dev_err(info->dev, "Error in clr FG EN\n");
2118 cancel_delayed_work(&info->monitor_work);
2119 cancel_delayed_work(&info->charge_stable_work);
2120 cancel_delayed_work(&info->changed_work);
2121 #ifdef ENABLE_LOW_BATTERY_DETECTION
2122 cancel_delayed_work(&info->low_battery_work);
2125 flush_work(&info->irq_work);
2126 flush_work(&info->usb_irq_work);
2128 flush_workqueue(info->monitor_wqueue);
2129 flush_workqueue(info->workqueue);
2130 flush_workqueue(info->usb_workqueue);
2132 destroy_workqueue(info->monitor_wqueue);
2133 destroy_workqueue(info->workqueue);
2134 destroy_workqueue(info->usb_workqueue);
2136 power_supply_unregister(&info->battery);
2138 platform_set_drvdata(pdev, NULL);
2143 static int ricoh619_battery_suspend(struct device *dev)
2145 struct ricoh619_battery_info *info = dev_get_drvdata(dev);
2150 bool is_charging = true;
2153 && (info->soca->status == RICOH619_SOCA_STABLE)) {
2154 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
2156 dev_err(info->dev, "Error in writing PSWR_REG\n");
2159 val = (info->soca->displayed_soc + 50)/100;
2161 ret = ricoh619_write(info->dev->parent, PSWR_REG, val);
2163 dev_err(info->dev, "Error in writing PSWR_REG\n");
2165 g_soc = (info->soca->displayed_soc + 50)/100;
2167 ret = calc_capacity_in_period(info, &cc_cap, &is_charging);
2169 dev_err(info->dev, "Read cc_sum Error !!-----\n");
2172 disable_irq(charger_irq + RICOH619_IRQ_FONCHGINT);
2173 disable_irq(charger_irq + RICOH619_IRQ_FCHGCMPINT);
2174 disable_irq(charger_irq + RICOH619_IRQ_FVUSBDETSINT);
2175 disable_irq(charger_irq + RICOH619_IRQ_FVADPDETSINT);
2176 #ifdef ENABLE_LOW_BATTERY_DETECTION
2177 disable_irq(charger_irq + RICOH619_IRQ_VSYSLIR);
2180 cancel_delayed_work_sync(&info->monitor_work);
2181 cancel_delayed_work_sync(&info->displayed_work);
2182 cancel_delayed_work_sync(&info->charge_stable_work);
2183 cancel_delayed_work_sync(&info->changed_work);
2184 #ifdef ENABLE_LOW_BATTERY_DETECTION
2185 cancel_delayed_work_sync(&info->low_battery_work);
2187 cancel_work_sync(&info->irq_work);
2188 cancel_work_sync(&info->usb_irq_work);
2190 flush_workqueue(info->monitor_wqueue);
2191 flush_workqueue(info->workqueue);
2192 flush_workqueue(info->usb_workqueue);
2197 static int ricoh619_battery_resume(struct device *dev)
2199 struct ricoh619_battery_info *info = dev_get_drvdata(dev);
2202 int displayed_soc_temp;
2204 bool is_charging = true;
2207 if (info->entry_factory_mode) {
2208 info->soca->displayed_soc = -EINVAL;
2209 } else if (RICOH619_SOCA_STABLE == info->soca->status) {
2210 info->soca->soc = calc_capacity(info) * 100;
2211 info->soca->displayed_soc = info->soca->soc;
2212 } else if (RICOH619_SOCA_ZERO == info->soca->status) {
2213 if (calc_ocv(info) > get_OCV_voltage(info, 0)) {
2214 ret = ricoh619_read(info->dev->parent, PSWR_REG, &val);
2216 info->soca->soc = val * 100;
2217 if ((ret < 0) || (val == 0)) {
2219 "Error in reading PSWR_REG %d\n", ret);
2221 = calc_capacity(info) * 100 + 50;
2224 ret = calc_capacity_in_period(info, &cc_cap,
2227 dev_err(info->dev, "Read cc_sum Error !!-----\n");
2229 info->soca->cc_delta
2230 = (is_charging == true) ? cc_cap : -cc_cap;
2233 = info->soca->soc + info->soca->cc_delta;
2234 displayed_soc_temp = min(10000, displayed_soc_temp);
2235 displayed_soc_temp = max(0, displayed_soc_temp);
2236 info->soca->displayed_soc = displayed_soc_temp;
2238 ret = ricoh619_write(info->dev->parent,
2241 dev_err(info->dev, "Error in writing the control register\n");
2242 info->soca->ready_fg = 0;
2243 info->soca->status = RICOH619_SOCA_FG_RESET;
2246 info->soca->displayed_soc = 0;
2248 ret = ricoh619_read(info->dev->parent, PSWR_REG, &val);
2250 info->soca->soc = val * 100;
2251 if ((ret < 0) || (val == 0)) {
2253 "Error in reading PSWR_REG %d\n", ret);
2255 = calc_capacity(info) * 100 + 50;
2258 ret = calc_capacity_in_period(info, &cc_cap, &is_charging);
2260 dev_err(info->dev, "Read cc_sum Error !!-----\n");
2262 info->soca->cc_delta = (is_charging == true) ? cc_cap : -cc_cap;
2264 displayed_soc_temp = info->soca->soc + info->soca->cc_delta;
2265 displayed_soc_temp = min(10000, displayed_soc_temp);
2266 displayed_soc_temp = max(0, displayed_soc_temp);
2267 info->soca->displayed_soc = displayed_soc_temp;
2268 if (RICOH619_SOCA_DISP == info->soca->status)
2269 info->soca->last_soc = calc_capacity(info) * 100;
2272 power_supply_changed(&info->battery);
2273 queue_delayed_work(info->monitor_wqueue, &info->displayed_work, HZ);
2275 if (RICOH619_SOCA_UNSTABLE == info->soca->status) {
2276 info->soca->stable_count = 10;
2277 queue_delayed_work(info->monitor_wqueue,
2278 &info->charge_stable_work,
2279 RICOH619_FG_STABLE_TIME*HZ/10);
2280 } else if (RICOH619_SOCA_FG_RESET == info->soca->status) {
2281 info->soca->stable_count = 1;
2283 for (i = 0; i < 3; i = i+1)
2284 info->soca->reset_soc[i] = 0;
2285 info->soca->reset_count = 0;
2287 queue_delayed_work(info->monitor_wqueue,
2288 &info->charge_stable_work,
2289 RICOH619_FG_RESET_TIME*HZ);
2292 queue_delayed_work(info->monitor_wqueue, &info->monitor_work,
2293 info->monitor_time);
2295 enable_irq(charger_irq + RICOH619_IRQ_FONCHGINT);
2296 enable_irq(charger_irq + RICOH619_IRQ_FCHGCMPINT);
2297 enable_irq(charger_irq + RICOH619_IRQ_FVUSBDETSINT);
2298 enable_irq(charger_irq + RICOH619_IRQ_FVADPDETSINT);
2299 #ifdef ENABLE_LOW_BATTERY_DETECTION
2300 enable_irq(charger_irq + RICOH619_IRQ_VSYSLIR);
2305 static const struct dev_pm_ops ricoh619_battery_pm_ops = {
2306 .suspend = ricoh619_battery_suspend,
2307 .resume = ricoh619_battery_resume,
2311 static struct platform_driver ricoh619_battery_driver = {
2313 .name = "ricoh619-battery",
2314 .owner = THIS_MODULE,
2316 .pm = &ricoh619_battery_pm_ops,
2319 .probe = ricoh619_battery_probe,
2320 .remove = __devexit_p(ricoh619_battery_remove),
2323 static int __init ricoh619_battery_init(void)
2325 RICOH_FG_DBG("PMU: %s\n", __func__);
2326 return platform_driver_register(&ricoh619_battery_driver);
2328 subsys_initcall_sync(ricoh619_battery_init);
2330 static void __exit ricoh619_battery_exit(void)
2332 platform_driver_unregister(&ricoh619_battery_driver);
2334 module_exit(ricoh619_battery_exit);
2336 MODULE_DESCRIPTION("RICOH619 Battery driver");
2337 MODULE_ALIAS("platform:ricoh619-battery");
2338 MODULE_LICENSE("GPL");