blob: 07f526514ac4faa73c2cbecc2ceee3d460c57058 [file] [log] [blame]
Umang Chheda62f409f2019-10-30 17:37:56 +05301/* Copyright (c) 2015, 2019, The Linux Foundation. All rights reserved.
2
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
12 * * Neither the name of The Linux Foundation nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <bits.h>
30#include <debug.h>
31#include <pm8x41.h>
32#include <pm8x41_hw.h>
33#include <qpnp-smb2.h>
34#include <qpnp-fg-gen3.h>
35#include <qtimer.h>
36#include <reboot.h>
37#include <target.h>
38
39#define BATT_CHARGER_STATUS_1_REG 0x1006
40#define BATT_CHARGER_STATUS_2_REG 0x1007
41#define BATT_IF_INT_RT_STS 0x1210
42#define USBIN_PLUGIN_RT_STS_REG 0x1310
43
44#define BATT_STATUS_MASK 0x07
45#define DISABLE_CHARGE 0x07
46
47static bool is_battery_present(void)
48{
49 uint8_t val = 0;
50
51 val = REG_READ(BATT_IF_INT_RT_STS);
52
53 /*
54 * If BATT_TERMINAL_MISSING_RT_STS BIT(5) or BATT_THERM_OR_ID_MISSING_RT_STS BIT(4)
55 * are set, battery is not present.
56 */
57 if ((val & BIT(5)) | (val & BIT(4)))
58 return false;
59
60 return true;
61}
62
63static void dump_jeita_status(void)
64{
65 uint8_t val = 0;
66
67 val = REG_READ(BATT_CHARGER_STATUS_2_REG);
68 dprintf(INFO,"BATT_CHARGER_STATUS_2_REG value : %d\n",val);
69}
70
71static bool is_usb_present(void)
72{
73 uint8_t val = 0;
74
75 val = REG_READ(USBIN_PLUGIN_RT_STS_REG);
76
77 if (val & BIT(4))
78 return true;
79
80 return false;
81}
82
83static bool is_battery_charging(void)
84{
85 uint8_t val = 0;
86
87 val = REG_READ(BATT_CHARGER_STATUS_1_REG);
88 dprintf(INFO,"BATT_CHARGER_STATUS_1_REG value : %d\n",val);
89 val = val & BATT_STATUS_MASK;
90
91 if (val == DISABLE_CHARGE)
92 return false;
93
94 return true;
95}
96
97void weak_battery_charging(void)
98{
99 uint32_t current_vbat = 0;
100 int batt_curr = 0;
101
102 current_vbat = fg_gen3_get_battery_voltage();
103 batt_curr = fg_gen3_get_battery_current();
104 dprintf(INFO,"Entering LK charging loop: Battery current=%d mA voltage=%d mV\n",
105 batt_curr,current_vbat);
106
107 while (current_vbat < LK_BATT_VOLT_THRESHOLD)
108 {
109 if (!is_battery_present()) {
110 dprintf(CRITICAL, "Battery is not present.Exiting weak battery charging\n");
111 break;
112 }
113
114 if (!is_usb_present()) {
115 dprintf(CRITICAL, "Shutting down because USB is not present\n");
116 shutdown_device();
117 }
118
119 if (!is_battery_charging()) {
120 dump_jeita_status();
121 dprintf(CRITICAL, "Shutting down because battery is not charging\n");
122 shutdown_device();
123 }
124
125 current_vbat = fg_gen3_get_battery_voltage();
126 batt_curr = fg_gen3_get_battery_current();
127 dprintf(INFO, "Battery Charging: current=%d mA voltage=%d mV\n",
128 batt_curr,current_vbat);
129 mdelay(1000);
130 }
131
132 dprintf(INFO,"Battery Charging is completed Booting to HLOS\n");
133}