Umang Chheda | 62f409f | 2019-10-30 17:37:56 +0530 | [diff] [blame] | 1 | /* 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> |
Umang Chheda | 4c140de | 2019-12-19 14:30:38 +0530 | [diff] [blame^] | 33 | #include <pm_smbchg_common.h> |
Umang Chheda | 62f409f | 2019-10-30 17:37:56 +0530 | [diff] [blame] | 34 | #include <qpnp-smb2.h> |
| 35 | #include <qpnp-fg-gen3.h> |
| 36 | #include <qtimer.h> |
| 37 | #include <reboot.h> |
| 38 | #include <target.h> |
| 39 | |
| 40 | #define BATT_CHARGER_STATUS_1_REG 0x1006 |
| 41 | #define BATT_CHARGER_STATUS_2_REG 0x1007 |
| 42 | #define BATT_IF_INT_RT_STS 0x1210 |
| 43 | #define USBIN_PLUGIN_RT_STS_REG 0x1310 |
| 44 | |
| 45 | #define BATT_STATUS_MASK 0x07 |
| 46 | #define DISABLE_CHARGE 0x07 |
| 47 | |
Umang Chheda | 4c140de | 2019-12-19 14:30:38 +0530 | [diff] [blame^] | 48 | char panel_name[256]; |
| 49 | |
Umang Chheda | 62f409f | 2019-10-30 17:37:56 +0530 | [diff] [blame] | 50 | static bool is_battery_present(void) |
| 51 | { |
| 52 | uint8_t val = 0; |
| 53 | |
| 54 | val = REG_READ(BATT_IF_INT_RT_STS); |
| 55 | |
| 56 | /* |
| 57 | * If BATT_TERMINAL_MISSING_RT_STS BIT(5) or BATT_THERM_OR_ID_MISSING_RT_STS BIT(4) |
| 58 | * are set, battery is not present. |
| 59 | */ |
| 60 | if ((val & BIT(5)) | (val & BIT(4))) |
| 61 | return false; |
| 62 | |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | static void dump_jeita_status(void) |
| 67 | { |
| 68 | uint8_t val = 0; |
| 69 | |
| 70 | val = REG_READ(BATT_CHARGER_STATUS_2_REG); |
| 71 | dprintf(INFO,"BATT_CHARGER_STATUS_2_REG value : %d\n",val); |
| 72 | } |
| 73 | |
| 74 | static bool is_usb_present(void) |
| 75 | { |
| 76 | uint8_t val = 0; |
| 77 | |
| 78 | val = REG_READ(USBIN_PLUGIN_RT_STS_REG); |
| 79 | |
| 80 | if (val & BIT(4)) |
| 81 | return true; |
| 82 | |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | static bool is_battery_charging(void) |
| 87 | { |
| 88 | uint8_t val = 0; |
| 89 | |
| 90 | val = REG_READ(BATT_CHARGER_STATUS_1_REG); |
| 91 | dprintf(INFO,"BATT_CHARGER_STATUS_1_REG value : %d\n",val); |
| 92 | val = val & BATT_STATUS_MASK; |
| 93 | |
| 94 | if (val == DISABLE_CHARGE) |
| 95 | return false; |
| 96 | |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | void weak_battery_charging(void) |
| 101 | { |
| 102 | uint32_t current_vbat = 0; |
| 103 | int batt_curr = 0; |
| 104 | |
| 105 | current_vbat = fg_gen3_get_battery_voltage(); |
| 106 | batt_curr = fg_gen3_get_battery_current(); |
| 107 | dprintf(INFO,"Entering LK charging loop: Battery current=%d mA voltage=%d mV\n", |
| 108 | batt_curr,current_vbat); |
| 109 | |
| 110 | while (current_vbat < LK_BATT_VOLT_THRESHOLD) |
| 111 | { |
| 112 | if (!is_battery_present()) { |
| 113 | dprintf(CRITICAL, "Battery is not present.Exiting weak battery charging\n"); |
| 114 | break; |
| 115 | } |
| 116 | |
| 117 | if (!is_usb_present()) { |
| 118 | dprintf(CRITICAL, "Shutting down because USB is not present\n"); |
| 119 | shutdown_device(); |
| 120 | } |
| 121 | |
| 122 | if (!is_battery_charging()) { |
| 123 | dump_jeita_status(); |
| 124 | dprintf(CRITICAL, "Shutting down because battery is not charging\n"); |
| 125 | shutdown_device(); |
| 126 | } |
| 127 | |
Umang Chheda | 4c140de | 2019-12-19 14:30:38 +0530 | [diff] [blame^] | 128 | if (!display_initialized) { |
| 129 | charge_in_progress = true; |
| 130 | target_display_init(panel_name); |
| 131 | display_initialized = true; |
| 132 | } |
| 133 | |
Umang Chheda | 62f409f | 2019-10-30 17:37:56 +0530 | [diff] [blame] | 134 | current_vbat = fg_gen3_get_battery_voltage(); |
| 135 | batt_curr = fg_gen3_get_battery_current(); |
| 136 | dprintf(INFO, "Battery Charging: current=%d mA voltage=%d mV\n", |
| 137 | batt_curr,current_vbat); |
| 138 | mdelay(1000); |
| 139 | } |
| 140 | |
Umang Chheda | 4c140de | 2019-12-19 14:30:38 +0530 | [diff] [blame^] | 141 | charge_in_progress = false; |
Umang Chheda | 62f409f | 2019-10-30 17:37:56 +0530 | [diff] [blame] | 142 | dprintf(INFO,"Battery Charging is completed Booting to HLOS\n"); |
Umang Chheda | 4c140de | 2019-12-19 14:30:38 +0530 | [diff] [blame^] | 143 | } |