Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2016, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #define pr_fmt(fmt) "FG: %s: " fmt, __func__ |
| 14 | |
| 15 | #include <linux/of.h> |
| 16 | #include <linux/of_irq.h> |
| 17 | #include <linux/of_platform.h> |
| 18 | #include <linux/of_batterydata.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/power_supply.h> |
| 21 | #include <linux/iio/consumer.h> |
| 22 | #include <linux/qpnp/qpnp-revid.h> |
| 23 | #include "fg-core.h" |
| 24 | #include "fg-reg.h" |
| 25 | |
| 26 | #define FG_GEN3_DEV_NAME "qcom,fg-gen3" |
| 27 | |
| 28 | #define PERPH_SUBTYPE_REG 0x05 |
| 29 | #define FG_BATT_SOC_PMICOBALT 0x10 |
| 30 | #define FG_BATT_INFO_PMICOBALT 0x11 |
| 31 | #define FG_MEM_INFO_PMICOBALT 0x0D |
| 32 | |
| 33 | /* SRAM address and offset in ascending order */ |
| 34 | #define CUTOFF_VOLT_WORD 5 |
| 35 | #define CUTOFF_VOLT_OFFSET 0 |
| 36 | #define SYS_TERM_CURR_WORD 6 |
| 37 | #define SYS_TERM_CURR_OFFSET 0 |
| 38 | #define DELTA_SOC_THR_WORD 12 |
| 39 | #define DELTA_SOC_THR_OFFSET 3 |
| 40 | #define RECHARGE_SOC_THR_WORD 14 |
| 41 | #define RECHARGE_SOC_THR_OFFSET 0 |
| 42 | #define CHG_TERM_CURR_WORD 14 |
| 43 | #define CHG_TERM_CURR_OFFSET 1 |
| 44 | #define EMPTY_VOLT_WORD 15 |
| 45 | #define EMPTY_VOLT_OFFSET 0 |
| 46 | #define VBATT_LOW_WORD 15 |
| 47 | #define VBATT_LOW_OFFSET 1 |
Nicholas Troast | dcf8fe6 | 2016-08-04 14:30:02 -0700 | [diff] [blame] | 48 | #define ESR_TIMER_DISCHG_MAX_WORD 17 |
| 49 | #define ESR_TIMER_DISCHG_MAX_OFFSET 0 |
| 50 | #define ESR_TIMER_DISCHG_INIT_WORD 17 |
| 51 | #define ESR_TIMER_DISCHG_INIT_OFFSET 2 |
| 52 | #define ESR_TIMER_CHG_MAX_WORD 18 |
| 53 | #define ESR_TIMER_CHG_MAX_OFFSET 0 |
| 54 | #define ESR_TIMER_CHG_INIT_WORD 18 |
| 55 | #define ESR_TIMER_CHG_INIT_OFFSET 2 |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 56 | #define PROFILE_LOAD_WORD 24 |
| 57 | #define PROFILE_LOAD_OFFSET 0 |
| 58 | #define NOM_CAP_WORD 58 |
| 59 | #define NOM_CAP_OFFSET 0 |
| 60 | #define PROFILE_INTEGRITY_WORD 79 |
| 61 | #define PROFILE_INTEGRITY_OFFSET 3 |
| 62 | #define BATT_SOC_WORD 91 |
| 63 | #define BATT_SOC_OFFSET 0 |
| 64 | #define MONOTONIC_SOC_WORD 94 |
| 65 | #define MONOTONIC_SOC_OFFSET 2 |
| 66 | #define VOLTAGE_PRED_WORD 97 |
| 67 | #define VOLTAGE_PRED_OFFSET 0 |
| 68 | #define OCV_WORD 97 |
| 69 | #define OCV_OFFSET 2 |
| 70 | #define RSLOW_WORD 101 |
| 71 | #define RSLOW_OFFSET 0 |
| 72 | #define LAST_BATT_SOC_WORD 119 |
| 73 | #define LAST_BATT_SOC_OFFSET 0 |
| 74 | #define LAST_MONOTONIC_SOC_WORD 119 |
| 75 | #define LAST_MONOTONIC_SOC_OFFSET 2 |
Nicholas Troast | 69da225 | 2016-09-07 16:17:47 -0700 | [diff] [blame] | 76 | #define ALG_FLAGS_WORD 120 |
| 77 | #define ALG_FLAGS_OFFSET 1 |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 78 | |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 79 | /* v2 SRAM address and offset in ascending order */ |
| 80 | #define DELTA_SOC_THR_v2_WORD 13 |
| 81 | #define DELTA_SOC_THR_v2_OFFSET 0 |
| 82 | #define RECHARGE_SOC_THR_v2_WORD 14 |
| 83 | #define RECHARGE_SOC_THR_v2_OFFSET 1 |
| 84 | #define CHG_TERM_CURR_v2_WORD 15 |
| 85 | #define CHG_TERM_CURR_v2_OFFSET 1 |
| 86 | #define EMPTY_VOLT_v2_WORD 15 |
Subbaraman Narayanamurthy | 4bf3ce2 | 2016-09-19 11:17:59 -0700 | [diff] [blame^] | 87 | #define EMPTY_VOLT_v2_OFFSET 3 |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 88 | #define VBATT_LOW_v2_WORD 16 |
| 89 | #define VBATT_LOW_v2_OFFSET 0 |
| 90 | |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 91 | static int fg_decode_value_16b(struct fg_sram_param *sp, |
| 92 | enum fg_sram_param_id id, int val); |
| 93 | static int fg_decode_default(struct fg_sram_param *sp, |
| 94 | enum fg_sram_param_id id, int val); |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 95 | static void fg_encode_voltage(struct fg_sram_param *sp, |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 96 | enum fg_sram_param_id id, int val, u8 *buf); |
| 97 | static void fg_encode_current(struct fg_sram_param *sp, |
| 98 | enum fg_sram_param_id id, int val, u8 *buf); |
| 99 | static void fg_encode_default(struct fg_sram_param *sp, |
| 100 | enum fg_sram_param_id id, int val, u8 *buf); |
| 101 | |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 102 | #define PARAM(_id, _addr_word, _addr_byte, _len, _num, _den, _offset, \ |
| 103 | _enc, _dec) \ |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 104 | [FG_SRAM_##_id] = { \ |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 105 | .addr_word = _addr_word, \ |
| 106 | .addr_byte = _addr_byte, \ |
| 107 | .len = _len, \ |
| 108 | .numrtr = _num, \ |
| 109 | .denmtr = _den, \ |
| 110 | .offset = _offset, \ |
| 111 | .encode = _enc, \ |
| 112 | .decode = _dec, \ |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 113 | } \ |
| 114 | |
| 115 | static struct fg_sram_param pmicobalt_v1_sram_params[] = { |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 116 | PARAM(BATT_SOC, BATT_SOC_WORD, BATT_SOC_OFFSET, 4, 1, 1, 0, NULL, |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 117 | fg_decode_default), |
| 118 | PARAM(VOLTAGE_PRED, VOLTAGE_PRED_WORD, VOLTAGE_PRED_OFFSET, 2, 244141, |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 119 | 1000, 0, NULL, fg_decode_value_16b), |
| 120 | PARAM(OCV, OCV_WORD, OCV_OFFSET, 2, 244141, 1000, 0, NULL, |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 121 | fg_decode_value_16b), |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 122 | PARAM(RSLOW, RSLOW_WORD, RSLOW_OFFSET, 2, 244141, 1000, 0, NULL, |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 123 | fg_decode_value_16b), |
Nicholas Troast | 69da225 | 2016-09-07 16:17:47 -0700 | [diff] [blame] | 124 | PARAM(ALG_FLAGS, ALG_FLAGS_WORD, ALG_FLAGS_OFFSET, 1, 1, 1, 0, NULL, |
| 125 | fg_decode_default), |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 126 | /* Entries below here are configurable during initialization */ |
| 127 | PARAM(CUTOFF_VOLT, CUTOFF_VOLT_WORD, CUTOFF_VOLT_OFFSET, 2, 1000000, |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 128 | 244141, 0, fg_encode_voltage, NULL), |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 129 | PARAM(EMPTY_VOLT, EMPTY_VOLT_WORD, EMPTY_VOLT_OFFSET, 1, 100000, 390625, |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 130 | -2500, fg_encode_voltage, NULL), |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 131 | PARAM(VBATT_LOW, VBATT_LOW_WORD, VBATT_LOW_OFFSET, 1, 100000, 390625, |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 132 | -2500, fg_encode_voltage, NULL), |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 133 | PARAM(SYS_TERM_CURR, SYS_TERM_CURR_WORD, SYS_TERM_CURR_OFFSET, 3, |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 134 | 1000000, 122070, 0, fg_encode_current, NULL), |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 135 | PARAM(CHG_TERM_CURR, CHG_TERM_CURR_WORD, CHG_TERM_CURR_OFFSET, 1, |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 136 | 100000, 390625, 0, fg_encode_current, NULL), |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 137 | PARAM(DELTA_SOC_THR, DELTA_SOC_THR_WORD, DELTA_SOC_THR_OFFSET, 1, 256, |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 138 | 100, 0, fg_encode_default, NULL), |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 139 | PARAM(RECHARGE_SOC_THR, RECHARGE_SOC_THR_WORD, RECHARGE_SOC_THR_OFFSET, |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 140 | 1, 256, 100, 0, fg_encode_default, NULL), |
Nicholas Troast | dcf8fe6 | 2016-08-04 14:30:02 -0700 | [diff] [blame] | 141 | PARAM(ESR_TIMER_DISCHG_MAX, ESR_TIMER_DISCHG_MAX_WORD, |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 142 | ESR_TIMER_DISCHG_MAX_OFFSET, 2, 1, 1, 0, fg_encode_default, |
| 143 | NULL), |
Nicholas Troast | dcf8fe6 | 2016-08-04 14:30:02 -0700 | [diff] [blame] | 144 | PARAM(ESR_TIMER_DISCHG_INIT, ESR_TIMER_DISCHG_INIT_WORD, |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 145 | ESR_TIMER_DISCHG_INIT_OFFSET, 2, 1, 1, 0, fg_encode_default, |
Nicholas Troast | dcf8fe6 | 2016-08-04 14:30:02 -0700 | [diff] [blame] | 146 | NULL), |
| 147 | PARAM(ESR_TIMER_CHG_MAX, ESR_TIMER_CHG_MAX_WORD, |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 148 | ESR_TIMER_CHG_MAX_OFFSET, 2, 1, 1, 0, fg_encode_default, NULL), |
Nicholas Troast | dcf8fe6 | 2016-08-04 14:30:02 -0700 | [diff] [blame] | 149 | PARAM(ESR_TIMER_CHG_INIT, ESR_TIMER_CHG_INIT_WORD, |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 150 | ESR_TIMER_CHG_INIT_OFFSET, 2, 1, 1, 0, fg_encode_default, NULL), |
| 151 | }; |
| 152 | |
| 153 | static struct fg_sram_param pmicobalt_v2_sram_params[] = { |
| 154 | PARAM(BATT_SOC, BATT_SOC_WORD, BATT_SOC_OFFSET, 4, 1, 1, 0, NULL, |
| 155 | fg_decode_default), |
| 156 | PARAM(VOLTAGE_PRED, VOLTAGE_PRED_WORD, VOLTAGE_PRED_OFFSET, 2, 244141, |
| 157 | 1000, 0, NULL, fg_decode_value_16b), |
| 158 | PARAM(OCV, OCV_WORD, OCV_OFFSET, 2, 244141, 1000, 0, NULL, |
| 159 | fg_decode_value_16b), |
| 160 | PARAM(RSLOW, RSLOW_WORD, RSLOW_OFFSET, 2, 244141, 1000, 0, NULL, |
| 161 | fg_decode_value_16b), |
Nicholas Troast | 69da225 | 2016-09-07 16:17:47 -0700 | [diff] [blame] | 162 | PARAM(ALG_FLAGS, ALG_FLAGS_WORD, ALG_FLAGS_OFFSET, 1, 1, 1, 0, NULL, |
| 163 | fg_decode_default), |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 164 | /* Entries below here are configurable during initialization */ |
| 165 | PARAM(CUTOFF_VOLT, CUTOFF_VOLT_WORD, CUTOFF_VOLT_OFFSET, 2, 1000000, |
| 166 | 244141, 0, fg_encode_voltage, NULL), |
Subbaraman Narayanamurthy | 4bf3ce2 | 2016-09-19 11:17:59 -0700 | [diff] [blame^] | 167 | PARAM(EMPTY_VOLT, EMPTY_VOLT_v2_WORD, EMPTY_VOLT_v2_OFFSET, 1, 1000, |
| 168 | 15625, -2000, fg_encode_voltage, NULL), |
| 169 | PARAM(VBATT_LOW, VBATT_LOW_v2_WORD, VBATT_LOW_v2_OFFSET, 1, 1000, |
| 170 | 15625, -2000, fg_encode_voltage, NULL), |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 171 | PARAM(SYS_TERM_CURR, SYS_TERM_CURR_WORD, SYS_TERM_CURR_OFFSET, 3, |
| 172 | 1000000, 122070, 0, fg_encode_current, NULL), |
| 173 | PARAM(CHG_TERM_CURR, CHG_TERM_CURR_v2_WORD, CHG_TERM_CURR_v2_OFFSET, 1, |
| 174 | 100000, 390625, 0, fg_encode_current, NULL), |
| 175 | PARAM(DELTA_SOC_THR, DELTA_SOC_THR_v2_WORD, DELTA_SOC_THR_v2_OFFSET, 1, |
| 176 | 256, 100, 0, fg_encode_default, NULL), |
| 177 | PARAM(RECHARGE_SOC_THR, RECHARGE_SOC_THR_v2_WORD, |
| 178 | RECHARGE_SOC_THR_v2_OFFSET, 1, 256, 100, 0, fg_encode_default, |
| 179 | NULL), |
| 180 | PARAM(ESR_TIMER_DISCHG_MAX, ESR_TIMER_DISCHG_MAX_WORD, |
| 181 | ESR_TIMER_DISCHG_MAX_OFFSET, 2, 1, 1, 0, fg_encode_default, |
| 182 | NULL), |
| 183 | PARAM(ESR_TIMER_DISCHG_INIT, ESR_TIMER_DISCHG_INIT_WORD, |
| 184 | ESR_TIMER_DISCHG_INIT_OFFSET, 2, 1, 1, 0, fg_encode_default, |
| 185 | NULL), |
| 186 | PARAM(ESR_TIMER_CHG_MAX, ESR_TIMER_CHG_MAX_WORD, |
| 187 | ESR_TIMER_CHG_MAX_OFFSET, 2, 1, 1, 0, fg_encode_default, NULL), |
| 188 | PARAM(ESR_TIMER_CHG_INIT, ESR_TIMER_CHG_INIT_WORD, |
| 189 | ESR_TIMER_CHG_INIT_OFFSET, 2, 1, 1, 0, fg_encode_default, NULL), |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 190 | }; |
| 191 | |
Nicholas Troast | 69da225 | 2016-09-07 16:17:47 -0700 | [diff] [blame] | 192 | static struct fg_alg_flag pmicobalt_v1_alg_flags[] = { |
| 193 | [ALG_FLAG_SOC_LT_OTG_MIN] = { |
| 194 | .name = "SOC_LT_OTG_MIN", |
| 195 | .bit = BIT(0), |
| 196 | }, |
| 197 | [ALG_FLAG_SOC_LT_RECHARGE] = { |
| 198 | .name = "SOC_LT_RECHARGE", |
| 199 | .bit = BIT(1), |
| 200 | }, |
| 201 | [ALG_FLAG_IBATT_LT_ITERM] = { |
| 202 | .name = "IBATT_LT_ITERM", |
| 203 | .bit = BIT(2), |
| 204 | }, |
| 205 | [ALG_FLAG_IBATT_GT_HPM] = { |
| 206 | .name = "IBATT_GT_HPM", |
| 207 | .bit = BIT(3), |
| 208 | }, |
| 209 | [ALG_FLAG_IBATT_GT_UPM] = { |
| 210 | .name = "IBATT_GT_UPM", |
| 211 | .bit = BIT(4), |
| 212 | }, |
| 213 | [ALG_FLAG_VBATT_LT_RECHARGE] = { |
| 214 | .name = "VBATT_LT_RECHARGE", |
| 215 | .bit = BIT(5), |
| 216 | }, |
| 217 | [ALG_FLAG_VBATT_GT_VFLOAT] = { |
| 218 | .invalid = true, |
| 219 | }, |
| 220 | }; |
| 221 | |
| 222 | static struct fg_alg_flag pmicobalt_v2_alg_flags[] = { |
| 223 | [ALG_FLAG_SOC_LT_OTG_MIN] = { |
| 224 | .name = "SOC_LT_OTG_MIN", |
| 225 | .bit = BIT(0), |
| 226 | }, |
| 227 | [ALG_FLAG_SOC_LT_RECHARGE] = { |
| 228 | .name = "SOC_LT_RECHARGE", |
| 229 | .bit = BIT(1), |
| 230 | }, |
| 231 | [ALG_FLAG_IBATT_LT_ITERM] = { |
| 232 | .name = "IBATT_LT_ITERM", |
| 233 | .bit = BIT(2), |
| 234 | }, |
| 235 | [ALG_FLAG_IBATT_GT_HPM] = { |
| 236 | .name = "IBATT_GT_HPM", |
| 237 | .bit = BIT(4), |
| 238 | }, |
| 239 | [ALG_FLAG_IBATT_GT_UPM] = { |
| 240 | .name = "IBATT_GT_UPM", |
| 241 | .bit = BIT(5), |
| 242 | }, |
| 243 | [ALG_FLAG_VBATT_LT_RECHARGE] = { |
| 244 | .name = "VBATT_LT_RECHARGE", |
| 245 | .bit = BIT(6), |
| 246 | }, |
| 247 | [ALG_FLAG_VBATT_GT_VFLOAT] = { |
| 248 | .name = "VBATT_GT_VFLOAT", |
| 249 | .bit = BIT(7), |
| 250 | }, |
| 251 | }; |
| 252 | |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 253 | static int fg_gen3_debug_mask; |
| 254 | module_param_named( |
| 255 | debug_mask, fg_gen3_debug_mask, int, 0600 |
| 256 | ); |
| 257 | |
| 258 | static int fg_sram_update_period_ms = 30000; |
| 259 | module_param_named( |
| 260 | sram_update_period_ms, fg_sram_update_period_ms, int, 0600 |
| 261 | ); |
| 262 | |
| 263 | /* Other functions HERE */ |
| 264 | |
| 265 | static int fg_awake_cb(struct votable *votable, void *data, int awake, |
| 266 | const char *client) |
| 267 | { |
| 268 | struct fg_chip *chip = data; |
| 269 | |
| 270 | if (awake) |
| 271 | pm_stay_awake(chip->dev); |
| 272 | else |
| 273 | pm_relax(chip->dev); |
| 274 | |
| 275 | pr_debug("client: %s awake: %d\n", client, awake); |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | static bool is_charger_available(struct fg_chip *chip) |
| 280 | { |
| 281 | if (!chip->batt_psy) |
| 282 | chip->batt_psy = power_supply_get_by_name("battery"); |
| 283 | |
| 284 | if (!chip->batt_psy) |
| 285 | return false; |
| 286 | |
| 287 | return true; |
| 288 | } |
| 289 | |
| 290 | static void status_change_work(struct work_struct *work) |
| 291 | { |
| 292 | struct fg_chip *chip = container_of(work, |
| 293 | struct fg_chip, status_change_work); |
| 294 | union power_supply_propval prop = {0, }; |
| 295 | |
| 296 | if (!is_charger_available(chip)) { |
| 297 | fg_dbg(chip, FG_STATUS, "Charger not available?!\n"); |
| 298 | return; |
| 299 | } |
| 300 | |
| 301 | power_supply_get_property(chip->batt_psy, POWER_SUPPLY_PROP_STATUS, |
| 302 | &prop); |
| 303 | switch (prop.intval) { |
| 304 | case POWER_SUPPLY_STATUS_CHARGING: |
| 305 | fg_dbg(chip, FG_POWER_SUPPLY, "Charging\n"); |
| 306 | break; |
| 307 | case POWER_SUPPLY_STATUS_DISCHARGING: |
| 308 | fg_dbg(chip, FG_POWER_SUPPLY, "Discharging\n"); |
| 309 | break; |
| 310 | case POWER_SUPPLY_STATUS_FULL: |
| 311 | fg_dbg(chip, FG_POWER_SUPPLY, "Full\n"); |
| 312 | break; |
| 313 | default: |
| 314 | break; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | #define PROFILE_LEN 224 |
| 319 | #define PROFILE_COMP_LEN 32 |
| 320 | #define SOC_READY_WAIT_MS 2000 |
| 321 | static void profile_load_work(struct work_struct *work) |
| 322 | { |
| 323 | struct fg_chip *chip = container_of(work, |
| 324 | struct fg_chip, |
| 325 | profile_load_work.work); |
| 326 | int rc; |
| 327 | u8 buf[PROFILE_COMP_LEN], val; |
| 328 | bool tried_again = false, profiles_same = false; |
| 329 | |
| 330 | if (!chip->batt_id_avail) { |
| 331 | pr_err("batt_id not available\n"); |
| 332 | return; |
| 333 | } |
| 334 | |
| 335 | rc = fg_sram_read(chip, PROFILE_INTEGRITY_WORD, |
| 336 | PROFILE_INTEGRITY_OFFSET, &val, 1, FG_IMA_DEFAULT); |
| 337 | if (rc < 0) { |
| 338 | pr_err("failed to read profile integrity rc=%d\n", rc); |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | vote(chip->awake_votable, PROFILE_LOAD, true, 0); |
| 343 | if (val == 0x01) { |
| 344 | fg_dbg(chip, FG_STATUS, "Battery profile integrity bit is set\n"); |
| 345 | rc = fg_sram_read(chip, PROFILE_LOAD_WORD, PROFILE_LOAD_OFFSET, |
| 346 | buf, PROFILE_COMP_LEN, FG_IMA_DEFAULT); |
| 347 | if (rc < 0) { |
| 348 | pr_err("Error in reading battery profile, rc:%d\n", rc); |
| 349 | goto out; |
| 350 | } |
| 351 | profiles_same = memcmp(chip->batt_profile, buf, |
| 352 | PROFILE_COMP_LEN) == 0; |
| 353 | if (profiles_same) { |
| 354 | fg_dbg(chip, FG_STATUS, "Battery profile is same\n"); |
| 355 | goto done; |
| 356 | } |
| 357 | fg_dbg(chip, FG_STATUS, "profiles are different?\n"); |
| 358 | } |
| 359 | |
| 360 | fg_dbg(chip, FG_STATUS, "profile loading started\n"); |
| 361 | rc = fg_masked_write(chip, BATT_SOC_RESTART(chip), RESTART_GO_BIT, 0); |
| 362 | if (rc < 0) { |
| 363 | pr_err("Error in writing to %04x, rc=%d\n", |
| 364 | BATT_SOC_RESTART(chip), rc); |
| 365 | goto out; |
| 366 | } |
| 367 | |
| 368 | /* load battery profile */ |
| 369 | rc = fg_sram_write(chip, PROFILE_LOAD_WORD, PROFILE_LOAD_OFFSET, |
| 370 | chip->batt_profile, PROFILE_LEN, FG_IMA_ATOMIC); |
| 371 | if (rc < 0) { |
| 372 | pr_err("Error in writing battery profile, rc:%d\n", rc); |
| 373 | goto out; |
| 374 | } |
| 375 | |
| 376 | rc = fg_masked_write(chip, BATT_SOC_RESTART(chip), RESTART_GO_BIT, |
| 377 | RESTART_GO_BIT); |
| 378 | if (rc < 0) { |
| 379 | pr_err("Error in writing to %04x, rc=%d\n", |
| 380 | BATT_SOC_RESTART(chip), rc); |
| 381 | goto out; |
| 382 | } |
| 383 | |
| 384 | wait: |
| 385 | rc = wait_for_completion_interruptible_timeout(&chip->soc_ready, |
| 386 | msecs_to_jiffies(SOC_READY_WAIT_MS)); |
| 387 | |
| 388 | /* If we were interrupted wait again one more time. */ |
| 389 | if (rc == -ERESTARTSYS && !tried_again) { |
| 390 | tried_again = true; |
| 391 | goto wait; |
| 392 | } else if (rc <= 0) { |
| 393 | pr_err("wait for soc_ready timed out rc=%d\n", rc); |
| 394 | goto out; |
| 395 | } |
| 396 | |
| 397 | fg_dbg(chip, FG_STATUS, "SOC is ready\n"); |
| 398 | |
| 399 | /* Set the profile integrity bit */ |
| 400 | val = 0x1; |
| 401 | rc = fg_sram_write(chip, PROFILE_INTEGRITY_WORD, |
| 402 | PROFILE_INTEGRITY_OFFSET, &val, 1, FG_IMA_DEFAULT); |
| 403 | if (rc < 0) { |
| 404 | pr_err("failed to write profile integrity rc=%d\n", rc); |
| 405 | goto out; |
| 406 | } |
| 407 | |
| 408 | fg_dbg(chip, FG_STATUS, "profile loaded successfully"); |
| 409 | done: |
| 410 | rc = fg_sram_read(chip, NOM_CAP_WORD, NOM_CAP_OFFSET, buf, 2, |
| 411 | FG_IMA_DEFAULT); |
| 412 | if (rc < 0) { |
| 413 | pr_err("Error in reading %04x[%d] rc=%d\n", NOM_CAP_WORD, |
| 414 | NOM_CAP_OFFSET, rc); |
| 415 | goto out; |
| 416 | } |
| 417 | |
| 418 | chip->nom_cap_uah = (int)(buf[0] | buf[1] << 8) * 1000; |
| 419 | chip->profile_loaded = true; |
| 420 | out: |
| 421 | vote(chip->awake_votable, PROFILE_LOAD, false, 0); |
| 422 | rc = fg_masked_write(chip, BATT_SOC_RESTART(chip), RESTART_GO_BIT, 0); |
| 423 | if (rc < 0) |
| 424 | pr_err("Error in writing to %04x, rc=%d\n", |
| 425 | BATT_SOC_RESTART(chip), rc); |
| 426 | } |
| 427 | |
| 428 | /* All getters HERE */ |
| 429 | |
| 430 | static int fg_decode_value_16b(struct fg_sram_param *sp, |
| 431 | enum fg_sram_param_id id, int value) |
| 432 | { |
| 433 | sp[id].value = div_u64((u64)(u16)value * sp[id].numrtr, sp[id].denmtr); |
| 434 | pr_debug("id: %d raw value: %x decoded value: %x\n", id, value, |
| 435 | sp[id].value); |
| 436 | return sp[id].value; |
| 437 | } |
| 438 | |
| 439 | static int fg_decode_default(struct fg_sram_param *sp, |
| 440 | enum fg_sram_param_id id, int value) |
| 441 | { |
| 442 | return value; |
| 443 | } |
| 444 | |
| 445 | static int fg_decode(struct fg_sram_param *sp, enum fg_sram_param_id id, |
| 446 | int value) |
| 447 | { |
| 448 | if (!sp[id].decode) { |
| 449 | pr_err("No decoding function for parameter %d\n", id); |
| 450 | return -EINVAL; |
| 451 | } |
| 452 | |
| 453 | return sp[id].decode(sp, id, value); |
| 454 | } |
| 455 | |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 456 | static void fg_encode_voltage(struct fg_sram_param *sp, |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 457 | enum fg_sram_param_id id, int val, u8 *buf) |
| 458 | { |
| 459 | int i, mask = 0xff; |
| 460 | int64_t temp; |
| 461 | |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 462 | val += sp[id].offset; |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 463 | temp = (int64_t)div_u64((u64)val * sp[id].numrtr, sp[id].denmtr); |
| 464 | pr_debug("temp: %llx id: %d, val: %d, buf: [ ", temp, id, val); |
| 465 | for (i = 0; i < sp[id].len; i++) { |
| 466 | buf[i] = temp & mask; |
| 467 | temp >>= 8; |
| 468 | pr_debug("%x ", buf[i]); |
| 469 | } |
| 470 | pr_debug("]\n"); |
| 471 | } |
| 472 | |
| 473 | static void fg_encode_current(struct fg_sram_param *sp, |
| 474 | enum fg_sram_param_id id, int val, u8 *buf) |
| 475 | { |
| 476 | int i, mask = 0xff; |
| 477 | int64_t temp; |
| 478 | s64 current_ma; |
| 479 | |
Subbaraman Narayanamurthy | 4bf3ce2 | 2016-09-19 11:17:59 -0700 | [diff] [blame^] | 480 | current_ma = val; |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 481 | temp = (int64_t)div_s64(current_ma * sp[id].numrtr, sp[id].denmtr); |
| 482 | pr_debug("temp: %llx id: %d, val: %d, buf: [ ", temp, id, val); |
| 483 | for (i = 0; i < sp[id].len; i++) { |
| 484 | buf[i] = temp & mask; |
| 485 | temp >>= 8; |
| 486 | pr_debug("%x ", buf[i]); |
| 487 | } |
| 488 | pr_debug("]\n"); |
| 489 | } |
| 490 | |
| 491 | static void fg_encode_default(struct fg_sram_param *sp, |
| 492 | enum fg_sram_param_id id, int val, u8 *buf) |
| 493 | { |
| 494 | int i, mask = 0xff; |
| 495 | int64_t temp; |
| 496 | |
| 497 | temp = DIV_ROUND_CLOSEST(val * sp[id].numrtr, sp[id].denmtr); |
| 498 | pr_debug("temp: %llx id: %d, val: %d, buf: [ ", temp, id, val); |
| 499 | for (i = 0; i < sp[id].len; i++) { |
| 500 | buf[i] = temp & mask; |
| 501 | temp >>= 8; |
| 502 | pr_debug("%x ", buf[i]); |
| 503 | } |
| 504 | pr_debug("]\n"); |
| 505 | } |
| 506 | |
| 507 | static void fg_encode(struct fg_sram_param *sp, enum fg_sram_param_id id, |
| 508 | int val, u8 *buf) |
| 509 | { |
| 510 | if (!sp[id].encode) { |
| 511 | pr_err("No encoding function for parameter %d\n", id); |
| 512 | return; |
| 513 | } |
| 514 | |
| 515 | sp[id].encode(sp, id, val, buf); |
| 516 | } |
| 517 | |
| 518 | /* |
| 519 | * Please make sure *_sram_params table has the entry for the parameter |
| 520 | * obtained through this function. In addition to address, offset, |
| 521 | * length from where this SRAM parameter is read, a decode function |
| 522 | * need to be specified. |
| 523 | */ |
| 524 | static int fg_get_sram_prop(struct fg_chip *chip, enum fg_sram_param_id id, |
| 525 | int *val) |
| 526 | { |
| 527 | int temp, rc, i; |
| 528 | u8 buf[4]; |
| 529 | |
| 530 | if (id < 0 || id > FG_SRAM_MAX || chip->sp[id].len > sizeof(buf)) |
| 531 | return -EINVAL; |
| 532 | |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 533 | rc = fg_sram_read(chip, chip->sp[id].addr_word, chip->sp[id].addr_byte, |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 534 | buf, chip->sp[id].len, FG_IMA_DEFAULT); |
| 535 | if (rc < 0) { |
| 536 | pr_err("Error reading address 0x%04x[%d] rc=%d\n", |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 537 | chip->sp[id].addr_word, chip->sp[id].addr_byte, rc); |
Nicholas Troast | b2d7174 | 2016-08-04 14:31:41 -0700 | [diff] [blame] | 538 | return rc; |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | for (i = 0, temp = 0; i < chip->sp[id].len; i++) |
| 542 | temp |= buf[i] << (8 * i); |
| 543 | |
| 544 | *val = fg_decode(chip->sp, id, temp); |
| 545 | return 0; |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | #define BATT_TEMP_NUMR 1 |
| 549 | #define BATT_TEMP_DENR 1 |
| 550 | static int fg_get_battery_temp(struct fg_chip *chip, int *val) |
| 551 | { |
| 552 | int rc = 0; |
| 553 | u16 temp = 0; |
| 554 | u8 buf[2]; |
| 555 | |
| 556 | rc = fg_read(chip, BATT_INFO_BATT_TEMP_LSB(chip), buf, 2); |
| 557 | if (rc < 0) { |
| 558 | pr_err("failed to read addr=0x%04x, rc=%d\n", |
| 559 | BATT_INFO_BATT_TEMP_LSB(chip), rc); |
| 560 | return rc; |
| 561 | } |
| 562 | |
| 563 | temp = ((buf[1] & BATT_TEMP_MSB_MASK) << 8) | |
| 564 | (buf[0] & BATT_TEMP_LSB_MASK); |
| 565 | temp = DIV_ROUND_CLOSEST(temp, 4); |
| 566 | |
| 567 | /* Value is in Kelvin; Convert it to deciDegC */ |
| 568 | temp = (temp - 273) * 10; |
| 569 | *val = temp; |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | #define BATT_ESR_NUMR 244141 |
| 574 | #define BATT_ESR_DENR 1000 |
| 575 | static int fg_get_battery_esr(struct fg_chip *chip, int *val) |
| 576 | { |
| 577 | int rc = 0; |
| 578 | u16 temp = 0; |
| 579 | u8 buf[2]; |
| 580 | |
| 581 | rc = fg_read(chip, BATT_INFO_ESR_LSB(chip), buf, 2); |
| 582 | if (rc < 0) { |
| 583 | pr_err("failed to read addr=0x%04x, rc=%d\n", |
| 584 | BATT_INFO_ESR_LSB(chip), rc); |
| 585 | return rc; |
| 586 | } |
| 587 | |
| 588 | if (chip->pmic_rev_id->rev4 < PMICOBALT_V2P0_REV4) |
| 589 | temp = ((buf[0] & ESR_MSB_MASK) << 8) | |
| 590 | (buf[1] & ESR_LSB_MASK); |
| 591 | else |
| 592 | temp = ((buf[1] & ESR_MSB_MASK) << 8) | |
| 593 | (buf[0] & ESR_LSB_MASK); |
| 594 | |
| 595 | pr_debug("buf: %x %x temp: %x\n", buf[0], buf[1], temp); |
| 596 | *val = div_u64((u64)temp * BATT_ESR_NUMR, BATT_ESR_DENR); |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | static int fg_get_battery_resistance(struct fg_chip *chip, int *val) |
| 601 | { |
| 602 | int rc, esr, rslow; |
| 603 | |
| 604 | rc = fg_get_battery_esr(chip, &esr); |
| 605 | if (rc < 0) { |
| 606 | pr_err("failed to get ESR, rc=%d\n", rc); |
| 607 | return rc; |
| 608 | } |
| 609 | |
| 610 | rc = fg_get_sram_prop(chip, FG_SRAM_RSLOW, &rslow); |
| 611 | if (rc < 0) { |
| 612 | pr_err("failed to get Rslow, rc=%d\n", rc); |
| 613 | return rc; |
| 614 | } |
| 615 | |
| 616 | *val = esr + rslow; |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | #define BATT_CURRENT_NUMR 488281 |
| 621 | #define BATT_CURRENT_DENR 1000 |
| 622 | static int fg_get_battery_current(struct fg_chip *chip, int *val) |
| 623 | { |
| 624 | int rc = 0; |
| 625 | int64_t temp = 0; |
| 626 | u8 buf[2]; |
| 627 | |
| 628 | rc = fg_read(chip, BATT_INFO_IBATT_LSB(chip), buf, 2); |
| 629 | if (rc < 0) { |
| 630 | pr_err("failed to read addr=0x%04x, rc=%d\n", |
| 631 | BATT_INFO_IBATT_LSB(chip), rc); |
| 632 | return rc; |
| 633 | } |
| 634 | |
| 635 | if (chip->pmic_rev_id->rev4 < PMICOBALT_V2P0_REV4) |
| 636 | temp = buf[0] << 8 | buf[1]; |
| 637 | else |
| 638 | temp = buf[1] << 8 | buf[0]; |
| 639 | |
| 640 | pr_debug("buf: %x %x temp: %llx\n", buf[0], buf[1], temp); |
| 641 | /* Sign bit is bit 15 */ |
| 642 | temp = twos_compliment_extend(temp, 15); |
| 643 | *val = div_s64((s64)temp * BATT_CURRENT_NUMR, BATT_CURRENT_DENR); |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | #define BATT_VOLTAGE_NUMR 122070 |
| 648 | #define BATT_VOLTAGE_DENR 1000 |
| 649 | static int fg_get_battery_voltage(struct fg_chip *chip, int *val) |
| 650 | { |
| 651 | int rc = 0; |
| 652 | u16 temp = 0; |
| 653 | u8 buf[2]; |
| 654 | |
| 655 | rc = fg_read(chip, BATT_INFO_VBATT_LSB(chip), buf, 2); |
| 656 | if (rc < 0) { |
| 657 | pr_err("failed to read addr=0x%04x, rc=%d\n", |
| 658 | BATT_INFO_VBATT_LSB(chip), rc); |
| 659 | return rc; |
| 660 | } |
| 661 | |
| 662 | if (chip->pmic_rev_id->rev4 < PMICOBALT_V2P0_REV4) |
| 663 | temp = buf[0] << 8 | buf[1]; |
| 664 | else |
| 665 | temp = buf[1] << 8 | buf[0]; |
| 666 | |
| 667 | pr_debug("buf: %x %x temp: %x\n", buf[0], buf[1], temp); |
| 668 | *val = div_u64((u64)temp * BATT_VOLTAGE_NUMR, BATT_VOLTAGE_DENR); |
| 669 | return 0; |
| 670 | } |
| 671 | |
| 672 | #define MAX_TRIES_SOC 5 |
| 673 | static int fg_get_msoc_raw(struct fg_chip *chip, int *val) |
| 674 | { |
| 675 | u8 cap[2]; |
| 676 | int rc, tries = 0; |
| 677 | |
| 678 | while (tries < MAX_TRIES_SOC) { |
| 679 | rc = fg_read(chip, BATT_SOC_FG_MONOTONIC_SOC(chip), cap, 2); |
| 680 | if (rc < 0) { |
| 681 | pr_err("failed to read addr=0x%04x, rc=%d\n", |
| 682 | BATT_SOC_FG_MONOTONIC_SOC(chip), rc); |
| 683 | return rc; |
| 684 | } |
| 685 | |
| 686 | if (cap[0] == cap[1]) |
| 687 | break; |
| 688 | |
| 689 | tries++; |
| 690 | } |
| 691 | |
| 692 | if (tries == MAX_TRIES_SOC) { |
| 693 | pr_err("shadow registers do not match\n"); |
| 694 | return -EINVAL; |
| 695 | } |
| 696 | |
| 697 | fg_dbg(chip, FG_POWER_SUPPLY, "raw: 0x%02x\n", cap[0]); |
| 698 | |
| 699 | *val = cap[0]; |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | #define FULL_CAPACITY 100 |
| 704 | #define FULL_SOC_RAW 255 |
| 705 | static int fg_get_prop_capacity(struct fg_chip *chip, int *val) |
| 706 | { |
| 707 | int rc, msoc; |
| 708 | |
| 709 | rc = fg_get_msoc_raw(chip, &msoc); |
| 710 | if (rc < 0) |
| 711 | return rc; |
| 712 | |
| 713 | *val = DIV_ROUND_CLOSEST(msoc * FULL_CAPACITY, FULL_SOC_RAW); |
| 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | #define DEFAULT_BATT_TYPE "Unknown Battery" |
| 718 | #define MISSING_BATT_TYPE "Missing Battery" |
| 719 | #define LOADING_BATT_TYPE "Loading Battery" |
| 720 | static const char *fg_get_battery_type(struct fg_chip *chip) |
| 721 | { |
| 722 | if (chip->battery_missing) |
| 723 | return MISSING_BATT_TYPE; |
| 724 | |
| 725 | if (chip->bp.batt_type_str) { |
| 726 | if (chip->profile_loaded) |
| 727 | return chip->bp.batt_type_str; |
| 728 | else |
| 729 | return LOADING_BATT_TYPE; |
| 730 | } |
| 731 | |
| 732 | return DEFAULT_BATT_TYPE; |
| 733 | } |
| 734 | |
| 735 | static int fg_get_batt_id(struct fg_chip *chip, int *val) |
| 736 | { |
| 737 | int rc, batt_id = -EINVAL; |
| 738 | |
| 739 | if (!chip->batt_id_chan) |
| 740 | return -EINVAL; |
| 741 | |
| 742 | rc = iio_read_channel_processed(chip->batt_id_chan, &batt_id); |
| 743 | if (rc < 0) { |
| 744 | pr_err("Error in reading batt_id channel, rc:%d\n", rc); |
| 745 | return rc; |
| 746 | } |
| 747 | |
| 748 | chip->batt_id_avail = true; |
| 749 | fg_dbg(chip, FG_STATUS, "batt_id: %d\n", batt_id); |
| 750 | |
| 751 | *val = batt_id; |
| 752 | return 0; |
| 753 | } |
| 754 | |
| 755 | static int fg_get_batt_profile(struct fg_chip *chip) |
| 756 | { |
| 757 | struct device_node *node = chip->dev->of_node; |
| 758 | struct device_node *batt_node, *profile_node; |
| 759 | const char *data; |
| 760 | int rc, len, batt_id; |
| 761 | |
| 762 | rc = fg_get_batt_id(chip, &batt_id); |
| 763 | if (rc < 0) { |
| 764 | pr_err("Error in getting batt_id rc:%d\n", rc); |
| 765 | return rc; |
| 766 | } |
| 767 | |
| 768 | batt_node = of_find_node_by_name(node, "qcom,battery-data"); |
| 769 | if (!batt_node) { |
| 770 | pr_err("Batterydata not available\n"); |
| 771 | return -ENXIO; |
| 772 | } |
| 773 | |
| 774 | batt_id /= 1000; |
| 775 | profile_node = of_batterydata_get_best_profile(batt_node, batt_id, |
| 776 | NULL); |
| 777 | if (IS_ERR(profile_node)) |
| 778 | return PTR_ERR(profile_node); |
| 779 | |
| 780 | if (!profile_node) { |
| 781 | pr_err("couldn't find profile handle\n"); |
| 782 | return -ENODATA; |
| 783 | } |
| 784 | |
| 785 | rc = of_property_read_string(profile_node, "qcom,battery-type", |
| 786 | &chip->bp.batt_type_str); |
| 787 | if (rc < 0) { |
| 788 | pr_err("battery type unavailable, rc:%d\n", rc); |
| 789 | return rc; |
| 790 | } |
| 791 | |
| 792 | rc = of_property_read_u32(profile_node, "qcom,max-voltage-uv", |
| 793 | &chip->bp.float_volt_uv); |
| 794 | if (rc < 0) { |
| 795 | pr_err("battery float voltage unavailable, rc:%d\n", rc); |
| 796 | chip->bp.float_volt_uv = -EINVAL; |
| 797 | } |
| 798 | |
| 799 | rc = of_property_read_u32(profile_node, "qcom,nom-batt-capacity-mah", |
| 800 | &chip->bp.fastchg_curr_ma); |
| 801 | if (rc < 0) { |
| 802 | pr_err("battery nominal capacity unavailable, rc:%d\n", rc); |
| 803 | chip->bp.fastchg_curr_ma = -EINVAL; |
| 804 | } |
| 805 | |
| 806 | data = of_get_property(profile_node, "qcom,fg-profile-data", &len); |
| 807 | if (!data) { |
| 808 | pr_err("No profile data available\n"); |
| 809 | return -ENODATA; |
| 810 | } |
| 811 | |
| 812 | if (len != PROFILE_LEN) { |
| 813 | pr_err("battery profile incorrect size: %d\n", len); |
| 814 | return -EINVAL; |
| 815 | } |
| 816 | |
| 817 | memcpy(chip->batt_profile, data, len); |
| 818 | return 0; |
| 819 | } |
| 820 | |
| 821 | static inline void get_temp_setpoint(int threshold, u8 *val) |
| 822 | { |
| 823 | /* Resolution is 0.5C. Base is -30C. */ |
| 824 | *val = DIV_ROUND_CLOSEST((threshold + 30) * 10, 5); |
| 825 | } |
| 826 | |
Nicholas Troast | dcf8fe6 | 2016-08-04 14:30:02 -0700 | [diff] [blame] | 827 | static int fg_set_esr_timer(struct fg_chip *chip, int cycles, bool charging, |
| 828 | int flags) |
| 829 | { |
| 830 | u8 buf[2]; |
| 831 | int rc, timer_max, timer_init; |
| 832 | |
| 833 | if (charging) { |
| 834 | timer_max = FG_SRAM_ESR_TIMER_CHG_MAX; |
| 835 | timer_init = FG_SRAM_ESR_TIMER_CHG_INIT; |
| 836 | } else { |
| 837 | timer_max = FG_SRAM_ESR_TIMER_DISCHG_MAX; |
| 838 | timer_init = FG_SRAM_ESR_TIMER_DISCHG_INIT; |
| 839 | } |
| 840 | |
| 841 | fg_encode(chip->sp, timer_max, cycles, buf); |
| 842 | rc = fg_sram_write(chip, |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 843 | chip->sp[timer_max].addr_word, |
| 844 | chip->sp[timer_max].addr_byte, buf, |
Nicholas Troast | dcf8fe6 | 2016-08-04 14:30:02 -0700 | [diff] [blame] | 845 | chip->sp[timer_max].len, flags); |
| 846 | if (rc < 0) { |
| 847 | pr_err("Error in writing esr_timer_dischg_max, rc=%d\n", |
| 848 | rc); |
| 849 | return rc; |
| 850 | } |
| 851 | |
| 852 | fg_encode(chip->sp, timer_init, cycles, buf); |
| 853 | rc = fg_sram_write(chip, |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 854 | chip->sp[timer_init].addr_word, |
| 855 | chip->sp[timer_init].addr_byte, buf, |
Nicholas Troast | dcf8fe6 | 2016-08-04 14:30:02 -0700 | [diff] [blame] | 856 | chip->sp[timer_init].len, flags); |
| 857 | if (rc < 0) { |
| 858 | pr_err("Error in writing esr_timer_dischg_init, rc=%d\n", |
| 859 | rc); |
| 860 | return rc; |
| 861 | } |
| 862 | |
| 863 | return 0; |
| 864 | } |
| 865 | |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 866 | /* PSY CALLBACKS STAY HERE */ |
| 867 | |
| 868 | static int fg_psy_get_property(struct power_supply *psy, |
| 869 | enum power_supply_property psp, |
| 870 | union power_supply_propval *pval) |
| 871 | { |
| 872 | struct fg_chip *chip = power_supply_get_drvdata(psy); |
| 873 | int rc = 0; |
| 874 | |
| 875 | switch (psp) { |
| 876 | case POWER_SUPPLY_PROP_CAPACITY: |
| 877 | rc = fg_get_prop_capacity(chip, &pval->intval); |
| 878 | break; |
| 879 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 880 | rc = fg_get_battery_voltage(chip, &pval->intval); |
| 881 | break; |
| 882 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 883 | rc = fg_get_battery_current(chip, &pval->intval); |
| 884 | break; |
| 885 | case POWER_SUPPLY_PROP_TEMP: |
| 886 | rc = fg_get_battery_temp(chip, &pval->intval); |
| 887 | break; |
| 888 | case POWER_SUPPLY_PROP_RESISTANCE: |
| 889 | rc = fg_get_battery_resistance(chip, &pval->intval); |
| 890 | break; |
| 891 | case POWER_SUPPLY_PROP_VOLTAGE_OCV: |
| 892 | rc = fg_get_sram_prop(chip, FG_SRAM_OCV, &pval->intval); |
| 893 | break; |
| 894 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
| 895 | pval->intval = chip->nom_cap_uah; |
| 896 | break; |
| 897 | case POWER_SUPPLY_PROP_RESISTANCE_ID: |
| 898 | rc = fg_get_batt_id(chip, &pval->intval); |
| 899 | break; |
| 900 | case POWER_SUPPLY_PROP_BATTERY_TYPE: |
| 901 | pval->strval = fg_get_battery_type(chip); |
| 902 | break; |
Abhijeet Dharmapurikar | 3d8b226 | 2016-08-25 13:50:42 -0700 | [diff] [blame] | 903 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
| 904 | pval->intval = chip->bp.float_volt_uv; |
| 905 | break; |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 906 | default: |
| 907 | break; |
| 908 | } |
| 909 | |
| 910 | return rc; |
| 911 | } |
| 912 | |
| 913 | static int fg_psy_set_property(struct power_supply *psy, |
| 914 | enum power_supply_property psp, |
| 915 | const union power_supply_propval *pval) |
| 916 | { |
| 917 | switch (psp) { |
| 918 | default: |
| 919 | break; |
| 920 | } |
| 921 | |
| 922 | return 0; |
| 923 | } |
| 924 | |
| 925 | static int fg_property_is_writeable(struct power_supply *psy, |
| 926 | enum power_supply_property psp) |
| 927 | { |
| 928 | switch (psp) { |
| 929 | default: |
| 930 | break; |
| 931 | } |
| 932 | |
| 933 | return 0; |
| 934 | } |
| 935 | |
| 936 | static void fg_external_power_changed(struct power_supply *psy) |
| 937 | { |
| 938 | pr_debug("power supply changed\n"); |
| 939 | } |
| 940 | |
| 941 | static int fg_notifier_cb(struct notifier_block *nb, |
| 942 | unsigned long event, void *data) |
| 943 | { |
| 944 | struct power_supply *psy = data; |
| 945 | struct fg_chip *chip = container_of(nb, struct fg_chip, nb); |
| 946 | |
| 947 | if (event != PSY_EVENT_PROP_CHANGED) |
| 948 | return NOTIFY_OK; |
| 949 | |
| 950 | if ((strcmp(psy->desc->name, "battery") == 0) |
| 951 | || (strcmp(psy->desc->name, "usb") == 0)) |
| 952 | schedule_work(&chip->status_change_work); |
| 953 | |
| 954 | return NOTIFY_OK; |
| 955 | } |
| 956 | |
| 957 | static enum power_supply_property fg_psy_props[] = { |
| 958 | POWER_SUPPLY_PROP_CAPACITY, |
| 959 | POWER_SUPPLY_PROP_TEMP, |
| 960 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 961 | POWER_SUPPLY_PROP_VOLTAGE_OCV, |
| 962 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 963 | POWER_SUPPLY_PROP_RESISTANCE_ID, |
| 964 | POWER_SUPPLY_PROP_RESISTANCE, |
| 965 | POWER_SUPPLY_PROP_BATTERY_TYPE, |
| 966 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
Abhijeet Dharmapurikar | 3d8b226 | 2016-08-25 13:50:42 -0700 | [diff] [blame] | 967 | POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 968 | }; |
| 969 | |
| 970 | static const struct power_supply_desc fg_psy_desc = { |
| 971 | .name = "bms", |
| 972 | .type = POWER_SUPPLY_TYPE_BMS, |
| 973 | .properties = fg_psy_props, |
| 974 | .num_properties = ARRAY_SIZE(fg_psy_props), |
| 975 | .get_property = fg_psy_get_property, |
| 976 | .set_property = fg_psy_set_property, |
| 977 | .external_power_changed = fg_external_power_changed, |
| 978 | .property_is_writeable = fg_property_is_writeable, |
| 979 | }; |
| 980 | |
| 981 | /* INIT FUNCTIONS STAY HERE */ |
| 982 | |
| 983 | static int fg_hw_init(struct fg_chip *chip) |
| 984 | { |
| 985 | int rc; |
| 986 | u8 buf[4], val; |
| 987 | |
| 988 | fg_encode(chip->sp, FG_SRAM_CUTOFF_VOLT, chip->dt.cutoff_volt_mv, buf); |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 989 | rc = fg_sram_write(chip, chip->sp[FG_SRAM_CUTOFF_VOLT].addr_word, |
| 990 | chip->sp[FG_SRAM_CUTOFF_VOLT].addr_byte, buf, |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 991 | chip->sp[FG_SRAM_CUTOFF_VOLT].len, FG_IMA_DEFAULT); |
| 992 | if (rc < 0) { |
| 993 | pr_err("Error in writing cutoff_volt, rc=%d\n", rc); |
| 994 | return rc; |
| 995 | } |
| 996 | |
| 997 | fg_encode(chip->sp, FG_SRAM_EMPTY_VOLT, chip->dt.empty_volt_mv, buf); |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 998 | rc = fg_sram_write(chip, chip->sp[FG_SRAM_EMPTY_VOLT].addr_word, |
| 999 | chip->sp[FG_SRAM_EMPTY_VOLT].addr_byte, buf, |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 1000 | chip->sp[FG_SRAM_EMPTY_VOLT].len, FG_IMA_DEFAULT); |
| 1001 | if (rc < 0) { |
| 1002 | pr_err("Error in writing empty_volt, rc=%d\n", rc); |
| 1003 | return rc; |
| 1004 | } |
| 1005 | |
| 1006 | fg_encode(chip->sp, FG_SRAM_CHG_TERM_CURR, chip->dt.chg_term_curr_ma, |
| 1007 | buf); |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 1008 | rc = fg_sram_write(chip, chip->sp[FG_SRAM_CHG_TERM_CURR].addr_word, |
| 1009 | chip->sp[FG_SRAM_CHG_TERM_CURR].addr_byte, buf, |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 1010 | chip->sp[FG_SRAM_CHG_TERM_CURR].len, FG_IMA_DEFAULT); |
| 1011 | if (rc < 0) { |
| 1012 | pr_err("Error in writing chg_term_curr, rc=%d\n", rc); |
| 1013 | return rc; |
| 1014 | } |
| 1015 | |
| 1016 | fg_encode(chip->sp, FG_SRAM_SYS_TERM_CURR, chip->dt.sys_term_curr_ma, |
| 1017 | buf); |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 1018 | rc = fg_sram_write(chip, chip->sp[FG_SRAM_SYS_TERM_CURR].addr_word, |
| 1019 | chip->sp[FG_SRAM_SYS_TERM_CURR].addr_byte, buf, |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 1020 | chip->sp[FG_SRAM_SYS_TERM_CURR].len, FG_IMA_DEFAULT); |
| 1021 | if (rc < 0) { |
| 1022 | pr_err("Error in writing sys_term_curr, rc=%d\n", rc); |
| 1023 | return rc; |
| 1024 | } |
| 1025 | |
| 1026 | if (chip->dt.vbatt_low_thr_mv > 0) { |
| 1027 | fg_encode(chip->sp, FG_SRAM_VBATT_LOW, |
| 1028 | chip->dt.vbatt_low_thr_mv, buf); |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 1029 | rc = fg_sram_write(chip, chip->sp[FG_SRAM_VBATT_LOW].addr_word, |
| 1030 | chip->sp[FG_SRAM_VBATT_LOW].addr_byte, buf, |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 1031 | chip->sp[FG_SRAM_VBATT_LOW].len, |
| 1032 | FG_IMA_DEFAULT); |
| 1033 | if (rc < 0) { |
| 1034 | pr_err("Error in writing vbatt_low_thr, rc=%d\n", rc); |
| 1035 | return rc; |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | if (chip->dt.delta_soc_thr > 0 && chip->dt.delta_soc_thr < 100) { |
| 1040 | fg_encode(chip->sp, FG_SRAM_DELTA_SOC_THR, |
| 1041 | chip->dt.delta_soc_thr, buf); |
| 1042 | rc = fg_sram_write(chip, |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 1043 | chip->sp[FG_SRAM_DELTA_SOC_THR].addr_word, |
| 1044 | chip->sp[FG_SRAM_DELTA_SOC_THR].addr_byte, |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 1045 | buf, chip->sp[FG_SRAM_DELTA_SOC_THR].len, |
| 1046 | FG_IMA_DEFAULT); |
| 1047 | if (rc < 0) { |
| 1048 | pr_err("Error in writing delta_soc_thr, rc=%d\n", rc); |
| 1049 | return rc; |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | if (chip->dt.recharge_soc_thr > 0 && chip->dt.recharge_soc_thr < 100) { |
| 1054 | fg_encode(chip->sp, FG_SRAM_RECHARGE_SOC_THR, |
| 1055 | chip->dt.recharge_soc_thr, buf); |
| 1056 | rc = fg_sram_write(chip, |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 1057 | chip->sp[FG_SRAM_RECHARGE_SOC_THR].addr_word, |
| 1058 | chip->sp[FG_SRAM_RECHARGE_SOC_THR].addr_byte, |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 1059 | buf, chip->sp[FG_SRAM_RECHARGE_SOC_THR].len, |
| 1060 | FG_IMA_DEFAULT); |
| 1061 | if (rc < 0) { |
| 1062 | pr_err("Error in writing recharge_soc_thr, rc=%d\n", |
| 1063 | rc); |
| 1064 | return rc; |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | if (chip->dt.rsense_sel >= SRC_SEL_BATFET && |
| 1069 | chip->dt.rsense_sel < SRC_SEL_RESERVED) { |
| 1070 | rc = fg_masked_write(chip, BATT_INFO_IBATT_SENSING_CFG(chip), |
| 1071 | SOURCE_SELECT_MASK, chip->dt.rsense_sel); |
| 1072 | if (rc < 0) { |
| 1073 | pr_err("Error in writing rsense_sel, rc=%d\n", rc); |
| 1074 | return rc; |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | get_temp_setpoint(chip->dt.jeita_thresholds[JEITA_COLD], &val); |
| 1079 | rc = fg_write(chip, BATT_INFO_JEITA_TOO_COLD(chip), &val, 1); |
| 1080 | if (rc < 0) { |
| 1081 | pr_err("Error in writing jeita_cold, rc=%d\n", rc); |
| 1082 | return rc; |
| 1083 | } |
| 1084 | |
| 1085 | get_temp_setpoint(chip->dt.jeita_thresholds[JEITA_COOL], &val); |
| 1086 | rc = fg_write(chip, BATT_INFO_JEITA_COLD(chip), &val, 1); |
| 1087 | if (rc < 0) { |
| 1088 | pr_err("Error in writing jeita_cool, rc=%d\n", rc); |
| 1089 | return rc; |
| 1090 | } |
| 1091 | |
| 1092 | get_temp_setpoint(chip->dt.jeita_thresholds[JEITA_WARM], &val); |
| 1093 | rc = fg_write(chip, BATT_INFO_JEITA_HOT(chip), &val, 1); |
| 1094 | if (rc < 0) { |
| 1095 | pr_err("Error in writing jeita_warm, rc=%d\n", rc); |
| 1096 | return rc; |
| 1097 | } |
| 1098 | |
| 1099 | get_temp_setpoint(chip->dt.jeita_thresholds[JEITA_HOT], &val); |
| 1100 | rc = fg_write(chip, BATT_INFO_JEITA_TOO_HOT(chip), &val, 1); |
| 1101 | if (rc < 0) { |
| 1102 | pr_err("Error in writing jeita_hot, rc=%d\n", rc); |
| 1103 | return rc; |
| 1104 | } |
| 1105 | |
Nicholas Troast | dcf8fe6 | 2016-08-04 14:30:02 -0700 | [diff] [blame] | 1106 | if (chip->dt.esr_timer_charging > 0) { |
| 1107 | rc = fg_set_esr_timer(chip, chip->dt.esr_timer_charging, true, |
| 1108 | FG_IMA_DEFAULT); |
| 1109 | if (rc < 0) { |
| 1110 | pr_err("Error in setting ESR timer, rc=%d\n", rc); |
| 1111 | return rc; |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | if (chip->dt.esr_timer_awake > 0) { |
| 1116 | rc = fg_set_esr_timer(chip, chip->dt.esr_timer_awake, false, |
| 1117 | FG_IMA_DEFAULT); |
| 1118 | if (rc < 0) { |
| 1119 | pr_err("Error in setting ESR timer, rc=%d\n", rc); |
| 1120 | return rc; |
| 1121 | } |
| 1122 | } |
| 1123 | |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 1124 | return 0; |
| 1125 | } |
| 1126 | |
| 1127 | static int fg_memif_init(struct fg_chip *chip) |
| 1128 | { |
| 1129 | return fg_ima_init(chip); |
| 1130 | } |
| 1131 | |
| 1132 | static int fg_batt_profile_init(struct fg_chip *chip) |
| 1133 | { |
| 1134 | int rc; |
| 1135 | |
| 1136 | if (!chip->batt_profile) { |
| 1137 | chip->batt_profile = devm_kcalloc(chip->dev, PROFILE_LEN, |
| 1138 | sizeof(*chip->batt_profile), |
| 1139 | GFP_KERNEL); |
| 1140 | if (!chip->batt_profile) |
| 1141 | return -ENOMEM; |
| 1142 | } |
| 1143 | |
| 1144 | rc = fg_get_batt_profile(chip); |
| 1145 | if (rc < 0) { |
| 1146 | pr_err("Error in getting battery profile, rc:%d\n", rc); |
| 1147 | return rc; |
| 1148 | } |
| 1149 | |
| 1150 | schedule_delayed_work(&chip->profile_load_work, msecs_to_jiffies(0)); |
| 1151 | return 0; |
| 1152 | } |
| 1153 | |
| 1154 | /* INTERRUPT HANDLERS STAY HERE */ |
| 1155 | |
| 1156 | static irqreturn_t fg_vbatt_low_irq_handler(int irq, void *data) |
| 1157 | { |
| 1158 | struct fg_chip *chip = data; |
| 1159 | |
| 1160 | fg_dbg(chip, FG_IRQ, "irq %d triggered\n", irq); |
| 1161 | return IRQ_HANDLED; |
| 1162 | } |
| 1163 | |
| 1164 | static irqreturn_t fg_batt_missing_irq_handler(int irq, void *data) |
| 1165 | { |
| 1166 | struct fg_chip *chip = data; |
| 1167 | u8 status; |
| 1168 | int rc; |
| 1169 | |
| 1170 | fg_dbg(chip, FG_IRQ, "irq %d triggered\n", irq); |
| 1171 | rc = fg_read(chip, BATT_INFO_INT_RT_STS(chip), &status, 1); |
| 1172 | if (rc < 0) { |
| 1173 | pr_err("failed to read addr=0x%04x, rc=%d\n", |
| 1174 | BATT_INFO_INT_RT_STS(chip), rc); |
| 1175 | return IRQ_HANDLED; |
| 1176 | } |
| 1177 | |
| 1178 | chip->battery_missing = (status & BT_MISS_BIT); |
| 1179 | |
| 1180 | if (chip->battery_missing) { |
| 1181 | chip->batt_id_avail = false; |
| 1182 | chip->profile_loaded = false; |
| 1183 | } else { |
| 1184 | rc = fg_batt_profile_init(chip); |
| 1185 | if (rc < 0) { |
| 1186 | pr_err("Error in initializing battery profile, rc=%d\n", |
| 1187 | rc); |
| 1188 | return IRQ_HANDLED; |
| 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | return IRQ_HANDLED; |
| 1193 | } |
| 1194 | |
| 1195 | static irqreturn_t fg_delta_batt_temp_irq_handler(int irq, void *data) |
| 1196 | { |
| 1197 | struct fg_chip *chip = data; |
| 1198 | |
| 1199 | fg_dbg(chip, FG_IRQ, "irq %d triggered\n", irq); |
| 1200 | return IRQ_HANDLED; |
| 1201 | } |
| 1202 | |
| 1203 | static irqreturn_t fg_first_est_irq_handler(int irq, void *data) |
| 1204 | { |
| 1205 | struct fg_chip *chip = data; |
| 1206 | |
| 1207 | fg_dbg(chip, FG_IRQ, "irq %d triggered\n", irq); |
| 1208 | complete_all(&chip->soc_ready); |
| 1209 | return IRQ_HANDLED; |
| 1210 | } |
| 1211 | |
| 1212 | static irqreturn_t fg_soc_update_irq_handler(int irq, void *data) |
| 1213 | { |
| 1214 | struct fg_chip *chip = data; |
| 1215 | |
| 1216 | fg_dbg(chip, FG_IRQ, "irq %d triggered\n", irq); |
| 1217 | complete_all(&chip->soc_update); |
| 1218 | return IRQ_HANDLED; |
| 1219 | } |
| 1220 | |
| 1221 | static irqreturn_t fg_delta_soc_irq_handler(int irq, void *data) |
| 1222 | { |
| 1223 | struct fg_chip *chip = data; |
| 1224 | |
| 1225 | fg_dbg(chip, FG_IRQ, "irq %d triggered\n", irq); |
| 1226 | return IRQ_HANDLED; |
| 1227 | } |
| 1228 | |
| 1229 | static irqreturn_t fg_empty_soc_irq_handler(int irq, void *data) |
| 1230 | { |
| 1231 | struct fg_chip *chip = data; |
| 1232 | |
| 1233 | fg_dbg(chip, FG_IRQ, "irq %d triggered\n", irq); |
| 1234 | return IRQ_HANDLED; |
| 1235 | } |
| 1236 | |
| 1237 | static irqreturn_t fg_soc_irq_handler(int irq, void *data) |
| 1238 | { |
| 1239 | struct fg_chip *chip = data; |
| 1240 | |
| 1241 | fg_dbg(chip, FG_IRQ, "irq %d triggered\n", irq); |
| 1242 | return IRQ_HANDLED; |
| 1243 | } |
| 1244 | |
| 1245 | static irqreturn_t fg_dummy_irq_handler(int irq, void *data) |
| 1246 | { |
| 1247 | struct fg_chip *chip = data; |
| 1248 | |
| 1249 | fg_dbg(chip, FG_IRQ, "irq %d triggered\n", irq); |
| 1250 | return IRQ_HANDLED; |
| 1251 | } |
| 1252 | |
| 1253 | static struct fg_irq_info fg_irqs[FG_IRQ_MAX] = { |
| 1254 | /* BATT_SOC irqs */ |
| 1255 | [MSOC_FULL_IRQ] = { |
| 1256 | "msoc-full", fg_soc_irq_handler, true }, |
| 1257 | [MSOC_HIGH_IRQ] = { |
| 1258 | "msoc-high", fg_soc_irq_handler, true }, |
| 1259 | [MSOC_EMPTY_IRQ] = { |
| 1260 | "msoc-empty", fg_empty_soc_irq_handler, true }, |
| 1261 | [MSOC_LOW_IRQ] = { |
| 1262 | "msoc-low", fg_soc_irq_handler }, |
| 1263 | [MSOC_DELTA_IRQ] = { |
| 1264 | "msoc-delta", fg_delta_soc_irq_handler, true }, |
| 1265 | [BSOC_DELTA_IRQ] = { |
| 1266 | "bsoc-delta", fg_delta_soc_irq_handler, true }, |
| 1267 | [SOC_READY_IRQ] = { |
| 1268 | "soc-ready", fg_first_est_irq_handler, true }, |
| 1269 | [SOC_UPDATE_IRQ] = { |
| 1270 | "soc-update", fg_soc_update_irq_handler }, |
| 1271 | /* BATT_INFO irqs */ |
| 1272 | [BATT_TEMP_DELTA_IRQ] = { |
| 1273 | "batt-temp-delta", fg_delta_batt_temp_irq_handler }, |
| 1274 | [BATT_MISSING_IRQ] = { |
| 1275 | "batt-missing", fg_batt_missing_irq_handler, true }, |
| 1276 | [ESR_DELTA_IRQ] = { |
| 1277 | "esr-delta", fg_dummy_irq_handler }, |
| 1278 | [VBATT_LOW_IRQ] = { |
| 1279 | "vbatt-low", fg_vbatt_low_irq_handler, true }, |
| 1280 | [VBATT_PRED_DELTA_IRQ] = { |
| 1281 | "vbatt-pred-delta", fg_dummy_irq_handler }, |
| 1282 | /* MEM_IF irqs */ |
| 1283 | [DMA_GRANT_IRQ] = { |
| 1284 | "dma-grant", fg_dummy_irq_handler }, |
| 1285 | [MEM_XCP_IRQ] = { |
| 1286 | "mem-xcp", fg_dummy_irq_handler }, |
| 1287 | [IMA_RDY_IRQ] = { |
| 1288 | "ima-rdy", fg_dummy_irq_handler }, |
| 1289 | }; |
| 1290 | |
| 1291 | static int fg_get_irq_index_byname(const char *name) |
| 1292 | { |
| 1293 | int i; |
| 1294 | |
| 1295 | for (i = 0; i < ARRAY_SIZE(fg_irqs); i++) { |
| 1296 | if (strcmp(fg_irqs[i].name, name) == 0) |
| 1297 | return i; |
| 1298 | } |
| 1299 | |
| 1300 | pr_err("%s is not in irq list\n", name); |
| 1301 | return -ENOENT; |
| 1302 | } |
| 1303 | |
| 1304 | static int fg_register_interrupts(struct fg_chip *chip) |
| 1305 | { |
| 1306 | struct device_node *child, *node = chip->dev->of_node; |
| 1307 | struct property *prop; |
| 1308 | const char *name; |
| 1309 | int rc, irq, irq_index; |
| 1310 | |
| 1311 | for_each_available_child_of_node(node, child) { |
| 1312 | of_property_for_each_string(child, "interrupt-names", prop, |
| 1313 | name) { |
| 1314 | irq = of_irq_get_byname(child, name); |
| 1315 | if (irq < 0) { |
| 1316 | dev_err(chip->dev, "failed to get irq %s irq:%d\n", |
| 1317 | name, irq); |
| 1318 | return irq; |
| 1319 | } |
| 1320 | |
| 1321 | irq_index = fg_get_irq_index_byname(name); |
| 1322 | if (irq_index < 0) |
| 1323 | return irq_index; |
| 1324 | |
| 1325 | rc = devm_request_threaded_irq(chip->dev, irq, NULL, |
| 1326 | fg_irqs[irq_index].handler, |
| 1327 | IRQF_ONESHOT, name, chip); |
| 1328 | if (rc < 0) { |
| 1329 | dev_err(chip->dev, "failed to register irq handler for %s rc:%d\n", |
| 1330 | name, rc); |
| 1331 | return rc; |
| 1332 | } |
| 1333 | |
| 1334 | fg_irqs[irq_index].irq = irq; |
| 1335 | if (fg_irqs[irq_index].wakeable) |
| 1336 | enable_irq_wake(fg_irqs[irq_index].irq); |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | return 0; |
| 1341 | } |
| 1342 | |
| 1343 | #define DEFAULT_CUTOFF_VOLT_MV 3200 |
| 1344 | #define DEFAULT_EMPTY_VOLT_MV 3100 |
| 1345 | #define DEFAULT_CHG_TERM_CURR_MA 100 |
Subbaraman Narayanamurthy | 4bf3ce2 | 2016-09-19 11:17:59 -0700 | [diff] [blame^] | 1346 | #define DEFAULT_SYS_TERM_CURR_MA -125 |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 1347 | #define DEFAULT_DELTA_SOC_THR 1 |
| 1348 | #define DEFAULT_RECHARGE_SOC_THR 95 |
| 1349 | #define DEFAULT_BATT_TEMP_COLD 0 |
| 1350 | #define DEFAULT_BATT_TEMP_COOL 5 |
| 1351 | #define DEFAULT_BATT_TEMP_WARM 45 |
| 1352 | #define DEFAULT_BATT_TEMP_HOT 50 |
| 1353 | static int fg_parse_dt(struct fg_chip *chip) |
| 1354 | { |
| 1355 | struct device_node *child, *revid_node, *node = chip->dev->of_node; |
| 1356 | u32 base, temp; |
| 1357 | u8 subtype; |
| 1358 | int rc, len; |
| 1359 | |
| 1360 | if (!node) { |
| 1361 | dev_err(chip->dev, "device tree node missing\n"); |
| 1362 | return -ENXIO; |
| 1363 | } |
| 1364 | |
| 1365 | revid_node = of_parse_phandle(node, "qcom,pmic-revid", 0); |
| 1366 | if (!revid_node) { |
| 1367 | pr_err("Missing qcom,pmic-revid property - driver failed\n"); |
| 1368 | return -EINVAL; |
| 1369 | } |
| 1370 | |
| 1371 | chip->pmic_rev_id = get_revid_data(revid_node); |
| 1372 | if (IS_ERR_OR_NULL(chip->pmic_rev_id)) { |
| 1373 | pr_err("Unable to get pmic_revid rc=%ld\n", |
| 1374 | PTR_ERR(chip->pmic_rev_id)); |
| 1375 | /* |
| 1376 | * the revid peripheral must be registered, any failure |
| 1377 | * here only indicates that the rev-id module has not |
| 1378 | * probed yet. |
| 1379 | */ |
| 1380 | return -EPROBE_DEFER; |
| 1381 | } |
| 1382 | |
| 1383 | pr_debug("PMIC subtype %d Digital major %d\n", |
| 1384 | chip->pmic_rev_id->pmic_subtype, chip->pmic_rev_id->rev4); |
| 1385 | |
| 1386 | switch (chip->pmic_rev_id->pmic_subtype) { |
| 1387 | case PMICOBALT_SUBTYPE: |
Nicholas Troast | 69da225 | 2016-09-07 16:17:47 -0700 | [diff] [blame] | 1388 | if (chip->pmic_rev_id->rev4 < PMICOBALT_V2P0_REV4) { |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 1389 | chip->sp = pmicobalt_v1_sram_params; |
Nicholas Troast | 69da225 | 2016-09-07 16:17:47 -0700 | [diff] [blame] | 1390 | chip->alg_flags = pmicobalt_v1_alg_flags; |
| 1391 | } else if (chip->pmic_rev_id->rev4 == PMICOBALT_V2P0_REV4) { |
Nicholas Troast | a2b4037 | 2016-08-15 10:45:39 -0700 | [diff] [blame] | 1392 | chip->sp = pmicobalt_v2_sram_params; |
Nicholas Troast | 69da225 | 2016-09-07 16:17:47 -0700 | [diff] [blame] | 1393 | chip->alg_flags = pmicobalt_v2_alg_flags; |
| 1394 | } else { |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 1395 | return -EINVAL; |
Nicholas Troast | 69da225 | 2016-09-07 16:17:47 -0700 | [diff] [blame] | 1396 | } |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 1397 | break; |
| 1398 | default: |
| 1399 | return -EINVAL; |
| 1400 | } |
| 1401 | |
| 1402 | chip->batt_id_chan = iio_channel_get(chip->dev, "rradc_batt_id"); |
| 1403 | if (IS_ERR(chip->batt_id_chan)) { |
| 1404 | if (PTR_ERR(chip->batt_id_chan) != -EPROBE_DEFER) |
| 1405 | pr_err("batt_id_chan unavailable %ld\n", |
| 1406 | PTR_ERR(chip->batt_id_chan)); |
| 1407 | rc = PTR_ERR(chip->batt_id_chan); |
| 1408 | chip->batt_id_chan = NULL; |
| 1409 | return rc; |
| 1410 | } |
| 1411 | |
| 1412 | if (of_get_available_child_count(node) == 0) { |
| 1413 | dev_err(chip->dev, "No child nodes specified!\n"); |
| 1414 | return -ENXIO; |
| 1415 | } |
| 1416 | |
| 1417 | for_each_available_child_of_node(node, child) { |
| 1418 | rc = of_property_read_u32(child, "reg", &base); |
| 1419 | if (rc < 0) { |
| 1420 | dev_err(chip->dev, "reg not specified in node %s, rc=%d\n", |
| 1421 | child->full_name, rc); |
| 1422 | return rc; |
| 1423 | } |
| 1424 | |
| 1425 | rc = fg_read(chip, base + PERPH_SUBTYPE_REG, &subtype, 1); |
| 1426 | if (rc < 0) { |
| 1427 | dev_err(chip->dev, "Couldn't read subtype for base %d, rc=%d\n", |
| 1428 | base, rc); |
| 1429 | return rc; |
| 1430 | } |
| 1431 | |
| 1432 | switch (subtype) { |
| 1433 | case FG_BATT_SOC_PMICOBALT: |
| 1434 | chip->batt_soc_base = base; |
| 1435 | break; |
| 1436 | case FG_BATT_INFO_PMICOBALT: |
| 1437 | chip->batt_info_base = base; |
| 1438 | break; |
| 1439 | case FG_MEM_INFO_PMICOBALT: |
| 1440 | chip->mem_if_base = base; |
| 1441 | break; |
| 1442 | default: |
| 1443 | dev_err(chip->dev, "Invalid peripheral subtype 0x%x\n", |
| 1444 | subtype); |
| 1445 | return -ENXIO; |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | /* Read all the optional properties below */ |
| 1450 | rc = of_property_read_u32(node, "qcom,fg-cutoff-voltage", &temp); |
| 1451 | if (rc < 0) |
| 1452 | chip->dt.cutoff_volt_mv = DEFAULT_CUTOFF_VOLT_MV; |
| 1453 | else |
| 1454 | chip->dt.cutoff_volt_mv = temp; |
| 1455 | |
| 1456 | rc = of_property_read_u32(node, "qcom,fg-empty-voltage", &temp); |
| 1457 | if (rc < 0) |
| 1458 | chip->dt.empty_volt_mv = DEFAULT_EMPTY_VOLT_MV; |
| 1459 | else |
| 1460 | chip->dt.empty_volt_mv = temp; |
| 1461 | |
| 1462 | rc = of_property_read_u32(node, "qcom,fg-vbatt-low-thr", &temp); |
| 1463 | if (rc < 0) |
| 1464 | chip->dt.vbatt_low_thr_mv = -EINVAL; |
| 1465 | else |
| 1466 | chip->dt.vbatt_low_thr_mv = temp; |
| 1467 | |
| 1468 | rc = of_property_read_u32(node, "qcom,fg-chg-term-current", &temp); |
| 1469 | if (rc < 0) |
| 1470 | chip->dt.chg_term_curr_ma = DEFAULT_CHG_TERM_CURR_MA; |
| 1471 | else |
| 1472 | chip->dt.chg_term_curr_ma = temp; |
| 1473 | |
| 1474 | rc = of_property_read_u32(node, "qcom,fg-sys-term-current", &temp); |
| 1475 | if (rc < 0) |
| 1476 | chip->dt.sys_term_curr_ma = DEFAULT_SYS_TERM_CURR_MA; |
| 1477 | else |
| 1478 | chip->dt.sys_term_curr_ma = temp; |
| 1479 | |
| 1480 | rc = of_property_read_u32(node, "qcom,fg-delta-soc-thr", &temp); |
| 1481 | if (rc < 0) |
| 1482 | chip->dt.delta_soc_thr = DEFAULT_DELTA_SOC_THR; |
| 1483 | else |
| 1484 | chip->dt.delta_soc_thr = temp; |
| 1485 | |
| 1486 | rc = of_property_read_u32(node, "qcom,fg-recharge-soc-thr", &temp); |
| 1487 | if (rc < 0) |
| 1488 | chip->dt.recharge_soc_thr = DEFAULT_RECHARGE_SOC_THR; |
| 1489 | else |
| 1490 | chip->dt.recharge_soc_thr = temp; |
| 1491 | |
| 1492 | rc = of_property_read_u32(node, "qcom,fg-rsense-sel", &temp); |
| 1493 | if (rc < 0) |
| 1494 | chip->dt.rsense_sel = SRC_SEL_BATFET_SMB; |
| 1495 | else |
| 1496 | chip->dt.rsense_sel = (u8)temp & SOURCE_SELECT_MASK; |
| 1497 | |
| 1498 | chip->dt.jeita_thresholds[JEITA_COLD] = DEFAULT_BATT_TEMP_COLD; |
| 1499 | chip->dt.jeita_thresholds[JEITA_COOL] = DEFAULT_BATT_TEMP_COOL; |
| 1500 | chip->dt.jeita_thresholds[JEITA_WARM] = DEFAULT_BATT_TEMP_WARM; |
| 1501 | chip->dt.jeita_thresholds[JEITA_HOT] = DEFAULT_BATT_TEMP_HOT; |
| 1502 | if (of_find_property(node, "qcom,fg-jeita-thresholds", &len)) { |
| 1503 | if (len == NUM_JEITA_LEVELS) { |
| 1504 | rc = of_property_read_u32_array(node, |
| 1505 | "qcom,fg-jeita-thresholds", |
| 1506 | chip->dt.jeita_thresholds, len); |
| 1507 | if (rc < 0) |
| 1508 | pr_warn("Error reading Jeita thresholds, default values will be used rc:%d\n", |
| 1509 | rc); |
| 1510 | } |
| 1511 | } |
| 1512 | |
Nicholas Troast | dcf8fe6 | 2016-08-04 14:30:02 -0700 | [diff] [blame] | 1513 | rc = of_property_read_u32(node, "qcom,fg-esr-timer-charging", &temp); |
| 1514 | if (rc < 0) |
| 1515 | chip->dt.esr_timer_charging = -EINVAL; |
| 1516 | else |
| 1517 | chip->dt.esr_timer_charging = temp; |
| 1518 | |
| 1519 | rc = of_property_read_u32(node, "qcom,fg-esr-timer-awake", &temp); |
| 1520 | if (rc < 0) |
| 1521 | chip->dt.esr_timer_awake = -EINVAL; |
| 1522 | else |
| 1523 | chip->dt.esr_timer_awake = temp; |
| 1524 | |
| 1525 | rc = of_property_read_u32(node, "qcom,fg-esr-timer-asleep", &temp); |
| 1526 | if (rc < 0) |
| 1527 | chip->dt.esr_timer_asleep = -EINVAL; |
| 1528 | else |
| 1529 | chip->dt.esr_timer_asleep = temp; |
| 1530 | |
| 1531 | |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 1532 | return 0; |
| 1533 | } |
| 1534 | |
| 1535 | static void fg_cleanup(struct fg_chip *chip) |
| 1536 | { |
| 1537 | power_supply_unreg_notifier(&chip->nb); |
Nicholas Troast | 69da225 | 2016-09-07 16:17:47 -0700 | [diff] [blame] | 1538 | debugfs_remove_recursive(chip->dfs_root); |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 1539 | if (chip->awake_votable) |
| 1540 | destroy_votable(chip->awake_votable); |
| 1541 | |
| 1542 | if (chip->batt_id_chan) |
| 1543 | iio_channel_release(chip->batt_id_chan); |
| 1544 | |
| 1545 | dev_set_drvdata(chip->dev, NULL); |
| 1546 | } |
| 1547 | |
| 1548 | static int fg_gen3_probe(struct platform_device *pdev) |
| 1549 | { |
| 1550 | struct fg_chip *chip; |
| 1551 | struct power_supply_config fg_psy_cfg; |
| 1552 | int rc; |
| 1553 | |
| 1554 | chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL); |
| 1555 | if (!chip) |
| 1556 | return -ENOMEM; |
| 1557 | |
| 1558 | chip->dev = &pdev->dev; |
| 1559 | chip->debug_mask = &fg_gen3_debug_mask; |
| 1560 | chip->irqs = fg_irqs; |
| 1561 | chip->regmap = dev_get_regmap(chip->dev->parent, NULL); |
| 1562 | if (!chip->regmap) { |
| 1563 | dev_err(chip->dev, "Parent regmap is unavailable\n"); |
| 1564 | return -ENXIO; |
| 1565 | } |
| 1566 | |
| 1567 | rc = fg_parse_dt(chip); |
| 1568 | if (rc < 0) { |
| 1569 | dev_err(chip->dev, "Error in reading DT parameters, rc:%d\n", |
| 1570 | rc); |
| 1571 | return rc; |
| 1572 | } |
| 1573 | |
| 1574 | chip->awake_votable = create_votable("FG_WS", VOTE_SET_ANY, fg_awake_cb, |
| 1575 | chip); |
| 1576 | if (IS_ERR(chip->awake_votable)) { |
| 1577 | rc = PTR_ERR(chip->awake_votable); |
| 1578 | return rc; |
| 1579 | } |
| 1580 | |
| 1581 | mutex_init(&chip->bus_lock); |
| 1582 | mutex_init(&chip->sram_rw_lock); |
| 1583 | init_completion(&chip->soc_update); |
| 1584 | init_completion(&chip->soc_ready); |
| 1585 | INIT_DELAYED_WORK(&chip->profile_load_work, profile_load_work); |
| 1586 | INIT_WORK(&chip->status_change_work, status_change_work); |
| 1587 | |
| 1588 | rc = fg_memif_init(chip); |
| 1589 | if (rc < 0) { |
| 1590 | dev_err(chip->dev, "Error in initializing FG_MEMIF, rc:%d\n", |
| 1591 | rc); |
| 1592 | goto exit; |
| 1593 | } |
| 1594 | |
| 1595 | rc = fg_hw_init(chip); |
| 1596 | if (rc < 0) { |
| 1597 | dev_err(chip->dev, "Error in initializing FG hardware, rc:%d\n", |
| 1598 | rc); |
| 1599 | goto exit; |
| 1600 | } |
| 1601 | |
| 1602 | platform_set_drvdata(pdev, chip); |
| 1603 | |
| 1604 | /* Register the power supply */ |
| 1605 | fg_psy_cfg.drv_data = chip; |
| 1606 | fg_psy_cfg.of_node = NULL; |
| 1607 | fg_psy_cfg.supplied_to = NULL; |
| 1608 | fg_psy_cfg.num_supplicants = 0; |
| 1609 | chip->fg_psy = devm_power_supply_register(chip->dev, &fg_psy_desc, |
| 1610 | &fg_psy_cfg); |
| 1611 | if (IS_ERR(chip->fg_psy)) { |
| 1612 | pr_err("failed to register fg_psy rc = %ld\n", |
| 1613 | PTR_ERR(chip->fg_psy)); |
| 1614 | goto exit; |
| 1615 | } |
| 1616 | |
| 1617 | chip->nb.notifier_call = fg_notifier_cb; |
| 1618 | rc = power_supply_reg_notifier(&chip->nb); |
| 1619 | if (rc < 0) { |
| 1620 | pr_err("Couldn't register psy notifier rc = %d\n", rc); |
| 1621 | goto exit; |
| 1622 | } |
| 1623 | |
| 1624 | rc = fg_register_interrupts(chip); |
| 1625 | if (rc < 0) { |
| 1626 | dev_err(chip->dev, "Error in registering interrupts, rc:%d\n", |
| 1627 | rc); |
| 1628 | goto exit; |
| 1629 | } |
| 1630 | |
| 1631 | /* Keep SOC_UPDATE irq disabled until we require it */ |
| 1632 | if (fg_irqs[SOC_UPDATE_IRQ].irq) |
| 1633 | disable_irq_nosync(fg_irqs[SOC_UPDATE_IRQ].irq); |
| 1634 | |
Nicholas Troast | 69da225 | 2016-09-07 16:17:47 -0700 | [diff] [blame] | 1635 | rc = fg_debugfs_create(chip); |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 1636 | if (rc < 0) { |
| 1637 | dev_err(chip->dev, "Error in creating debugfs entries, rc:%d\n", |
| 1638 | rc); |
| 1639 | goto exit; |
| 1640 | } |
| 1641 | |
| 1642 | rc = fg_batt_profile_init(chip); |
| 1643 | if (rc < 0) |
| 1644 | dev_warn(chip->dev, "Error in initializing battery profile, rc:%d\n", |
| 1645 | rc); |
| 1646 | |
| 1647 | device_init_wakeup(chip->dev, true); |
| 1648 | pr_debug("FG GEN3 driver successfully probed\n"); |
| 1649 | return 0; |
| 1650 | exit: |
| 1651 | fg_cleanup(chip); |
| 1652 | return rc; |
| 1653 | } |
| 1654 | |
Nicholas Troast | dcf8fe6 | 2016-08-04 14:30:02 -0700 | [diff] [blame] | 1655 | static int fg_gen3_suspend(struct device *dev) |
| 1656 | { |
| 1657 | struct fg_chip *chip = dev_get_drvdata(dev); |
| 1658 | int rc; |
| 1659 | |
| 1660 | if (chip->dt.esr_timer_awake > 0 && chip->dt.esr_timer_asleep > 0) { |
| 1661 | rc = fg_set_esr_timer(chip, chip->dt.esr_timer_asleep, false, |
| 1662 | FG_IMA_NO_WLOCK); |
| 1663 | if (rc < 0) { |
| 1664 | pr_err("Error in setting ESR timer during suspend, rc=%d\n", |
| 1665 | rc); |
| 1666 | return rc; |
| 1667 | } |
| 1668 | } |
| 1669 | |
| 1670 | return 0; |
| 1671 | } |
| 1672 | |
| 1673 | static int fg_gen3_resume(struct device *dev) |
| 1674 | { |
| 1675 | struct fg_chip *chip = dev_get_drvdata(dev); |
| 1676 | int rc; |
| 1677 | |
| 1678 | if (chip->dt.esr_timer_awake > 0 && chip->dt.esr_timer_asleep > 0) { |
| 1679 | rc = fg_set_esr_timer(chip, chip->dt.esr_timer_awake, false, |
| 1680 | FG_IMA_DEFAULT); |
| 1681 | if (rc < 0) { |
| 1682 | pr_err("Error in setting ESR timer during resume, rc=%d\n", |
| 1683 | rc); |
| 1684 | return rc; |
| 1685 | } |
| 1686 | } |
| 1687 | |
| 1688 | return 0; |
| 1689 | } |
| 1690 | |
| 1691 | static const struct dev_pm_ops fg_gen3_pm_ops = { |
| 1692 | .suspend = fg_gen3_suspend, |
| 1693 | .resume = fg_gen3_resume, |
| 1694 | }; |
| 1695 | |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 1696 | static int fg_gen3_remove(struct platform_device *pdev) |
| 1697 | { |
| 1698 | struct fg_chip *chip = dev_get_drvdata(&pdev->dev); |
| 1699 | |
| 1700 | fg_cleanup(chip); |
| 1701 | return 0; |
| 1702 | } |
| 1703 | |
| 1704 | static const struct of_device_id fg_gen3_match_table[] = { |
| 1705 | {.compatible = FG_GEN3_DEV_NAME}, |
| 1706 | {}, |
| 1707 | }; |
| 1708 | |
| 1709 | static struct platform_driver fg_gen3_driver = { |
| 1710 | .driver = { |
| 1711 | .name = FG_GEN3_DEV_NAME, |
| 1712 | .owner = THIS_MODULE, |
| 1713 | .of_match_table = fg_gen3_match_table, |
Nicholas Troast | dcf8fe6 | 2016-08-04 14:30:02 -0700 | [diff] [blame] | 1714 | .pm = &fg_gen3_pm_ops, |
Subbaraman Narayanamurthy | 6accb26 | 2016-03-14 16:41:16 -0700 | [diff] [blame] | 1715 | }, |
| 1716 | .probe = fg_gen3_probe, |
| 1717 | .remove = fg_gen3_remove, |
| 1718 | }; |
| 1719 | |
| 1720 | static int __init fg_gen3_init(void) |
| 1721 | { |
| 1722 | return platform_driver_register(&fg_gen3_driver); |
| 1723 | } |
| 1724 | |
| 1725 | static void __exit fg_gen3_exit(void) |
| 1726 | { |
| 1727 | return platform_driver_unregister(&fg_gen3_driver); |
| 1728 | } |
| 1729 | |
| 1730 | module_init(fg_gen3_init); |
| 1731 | module_exit(fg_gen3_exit); |
| 1732 | |
| 1733 | MODULE_DESCRIPTION("QPNP Fuel gauge GEN3 driver"); |
| 1734 | MODULE_LICENSE("GPL v2"); |
| 1735 | MODULE_ALIAS("platform:" FG_GEN3_DEV_NAME); |