Harry Yang | 3b113a5 | 2016-12-08 12:37:40 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2016-2017 The Linux Foundation. All rights reserved. |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 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 | #include <linux/device.h> |
| 14 | #include <linux/regmap.h> |
Harry Yang | 360bd53 | 2016-09-26 11:03:22 -0700 | [diff] [blame] | 15 | #include <linux/delay.h> |
Harry Yang | ba874ce | 2016-08-19 14:17:01 -0700 | [diff] [blame] | 16 | #include <linux/iio/consumer.h> |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 17 | #include <linux/power_supply.h> |
| 18 | #include <linux/regulator/driver.h> |
Abhijeet Dharmapurikar | 7442e42 | 2017-02-08 13:35:14 -0800 | [diff] [blame] | 19 | #include <linux/qpnp/qpnp-revid.h> |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 20 | #include <linux/irq.h> |
Harry Yang | 0c35ff6 | 2017-04-06 00:02:30 -0700 | [diff] [blame] | 21 | #include <linux/pmic-voter.h> |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 22 | #include "smb-lib.h" |
| 23 | #include "smb-reg.h" |
Harry Yang | 0c35ff6 | 2017-04-06 00:02:30 -0700 | [diff] [blame] | 24 | #include "battery.h" |
Anirudh Ghayal | 9d0196d | 2017-07-23 23:02:48 +0530 | [diff] [blame] | 25 | #include "step-chg-jeita.h" |
Nicholas Troast | 47ae461 | 2016-08-03 09:49:36 -0700 | [diff] [blame] | 26 | #include "storm-watch.h" |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 27 | |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 28 | #define smblib_err(chg, fmt, ...) \ |
| 29 | pr_err("%s: %s: " fmt, chg->name, \ |
| 30 | __func__, ##__VA_ARGS__) \ |
| 31 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 32 | #define smblib_dbg(chg, reason, fmt, ...) \ |
| 33 | do { \ |
| 34 | if (*chg->debug_mask & (reason)) \ |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 35 | pr_info("%s: %s: " fmt, chg->name, \ |
| 36 | __func__, ##__VA_ARGS__); \ |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 37 | else \ |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 38 | pr_debug("%s: %s: " fmt, chg->name, \ |
| 39 | __func__, ##__VA_ARGS__); \ |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 40 | } while (0) |
| 41 | |
| 42 | static bool is_secure(struct smb_charger *chg, int addr) |
| 43 | { |
Anirudh Ghayal | 4da3df9 | 2017-01-25 18:55:57 +0530 | [diff] [blame] | 44 | if (addr == SHIP_MODE_REG || addr == FREQ_CLK_DIV_REG) |
Fenglin Wu | edd7079 | 2016-11-22 13:16:19 +0800 | [diff] [blame] | 45 | return true; |
Harry Yang | 47bd385 | 2016-10-17 10:37:12 -0700 | [diff] [blame] | 46 | /* assume everything above 0xA0 is secure */ |
| 47 | return (bool)((addr & 0xFF) >= 0xA0); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | int smblib_read(struct smb_charger *chg, u16 addr, u8 *val) |
| 51 | { |
| 52 | unsigned int temp; |
| 53 | int rc = 0; |
| 54 | |
| 55 | rc = regmap_read(chg->regmap, addr, &temp); |
| 56 | if (rc >= 0) |
| 57 | *val = (u8)temp; |
| 58 | |
| 59 | return rc; |
| 60 | } |
| 61 | |
Ashay Jaiswal | 6d308da | 2017-02-18 09:59:23 +0530 | [diff] [blame] | 62 | int smblib_multibyte_read(struct smb_charger *chg, u16 addr, u8 *val, |
| 63 | int count) |
| 64 | { |
| 65 | return regmap_bulk_read(chg->regmap, addr, val, count); |
| 66 | } |
| 67 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 68 | int smblib_masked_write(struct smb_charger *chg, u16 addr, u8 mask, u8 val) |
| 69 | { |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 70 | int rc = 0; |
| 71 | |
Harry Yang | bacd222 | 2016-05-11 16:43:51 -0700 | [diff] [blame] | 72 | mutex_lock(&chg->write_lock); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 73 | if (is_secure(chg, addr)) { |
| 74 | rc = regmap_write(chg->regmap, (addr & 0xFF00) | 0xD0, 0xA5); |
| 75 | if (rc < 0) |
| 76 | goto unlock; |
| 77 | } |
| 78 | |
| 79 | rc = regmap_update_bits(chg->regmap, addr, mask, val); |
| 80 | |
| 81 | unlock: |
Harry Yang | bacd222 | 2016-05-11 16:43:51 -0700 | [diff] [blame] | 82 | mutex_unlock(&chg->write_lock); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 83 | return rc; |
| 84 | } |
| 85 | |
| 86 | int smblib_write(struct smb_charger *chg, u16 addr, u8 val) |
| 87 | { |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 88 | int rc = 0; |
| 89 | |
Harry Yang | bacd222 | 2016-05-11 16:43:51 -0700 | [diff] [blame] | 90 | mutex_lock(&chg->write_lock); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 91 | |
| 92 | if (is_secure(chg, addr)) { |
| 93 | rc = regmap_write(chg->regmap, (addr & ~(0xFF)) | 0xD0, 0xA5); |
| 94 | if (rc < 0) |
| 95 | goto unlock; |
| 96 | } |
| 97 | |
| 98 | rc = regmap_write(chg->regmap, addr, val); |
| 99 | |
| 100 | unlock: |
Harry Yang | bacd222 | 2016-05-11 16:43:51 -0700 | [diff] [blame] | 101 | mutex_unlock(&chg->write_lock); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 102 | return rc; |
| 103 | } |
| 104 | |
Abhijeet Dharmapurikar | c0b0f59 | 2016-10-14 10:55:42 -0700 | [diff] [blame] | 105 | static int smblib_get_jeita_cc_delta(struct smb_charger *chg, int *cc_delta_ua) |
| 106 | { |
| 107 | int rc, cc_minus_ua; |
| 108 | u8 stat; |
Harry Yang | fe91384 | 2016-08-10 12:27:28 -0700 | [diff] [blame] | 109 | |
Abhijeet Dharmapurikar | c0b0f59 | 2016-10-14 10:55:42 -0700 | [diff] [blame] | 110 | rc = smblib_read(chg, BATTERY_CHARGER_STATUS_2_REG, &stat); |
| 111 | if (rc < 0) { |
| 112 | smblib_err(chg, "Couldn't read BATTERY_CHARGER_STATUS_2 rc=%d\n", |
| 113 | rc); |
| 114 | return rc; |
| 115 | } |
| 116 | |
| 117 | if (!(stat & BAT_TEMP_STATUS_SOFT_LIMIT_MASK)) { |
| 118 | *cc_delta_ua = 0; |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | rc = smblib_get_charge_param(chg, &chg->param.jeita_cc_comp, |
Anirudh Ghayal | 9d0196d | 2017-07-23 23:02:48 +0530 | [diff] [blame] | 123 | &cc_minus_ua); |
Abhijeet Dharmapurikar | c0b0f59 | 2016-10-14 10:55:42 -0700 | [diff] [blame] | 124 | if (rc < 0) { |
| 125 | smblib_err(chg, "Couldn't get jeita cc minus rc=%d\n", rc); |
| 126 | return rc; |
| 127 | } |
| 128 | |
| 129 | *cc_delta_ua = -cc_minus_ua; |
| 130 | return 0; |
| 131 | } |
| 132 | |
Abhijeet Dharmapurikar | 74021fc | 2017-02-06 18:39:19 -0800 | [diff] [blame] | 133 | int smblib_icl_override(struct smb_charger *chg, bool override) |
| 134 | { |
| 135 | int rc; |
Abhijeet Dharmapurikar | 74021fc | 2017-02-06 18:39:19 -0800 | [diff] [blame] | 136 | |
Nicholas Troast | 11af51b | 2017-03-15 10:45:28 -0700 | [diff] [blame] | 137 | rc = smblib_masked_write(chg, USBIN_LOAD_CFG_REG, |
| 138 | ICL_OVERRIDE_AFTER_APSD_BIT, |
| 139 | override ? ICL_OVERRIDE_AFTER_APSD_BIT : 0); |
| 140 | if (rc < 0) |
| 141 | smblib_err(chg, "Couldn't override ICL rc=%d\n", rc); |
Abhijeet Dharmapurikar | 7442e42 | 2017-02-08 13:35:14 -0800 | [diff] [blame] | 142 | |
Nicholas Troast | 11af51b | 2017-03-15 10:45:28 -0700 | [diff] [blame] | 143 | return rc; |
Abhijeet Dharmapurikar | 74021fc | 2017-02-06 18:39:19 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Harry Yang | 166b15d | 2017-08-28 13:00:40 -0700 | [diff] [blame^] | 146 | int smblib_stat_sw_override_cfg(struct smb_charger *chg, bool override) |
| 147 | { |
| 148 | int rc; |
| 149 | |
| 150 | /* override = 1, SW STAT override; override = 0, HW auto mode */ |
| 151 | rc = smblib_masked_write(chg, STAT_CFG_REG, |
| 152 | STAT_SW_OVERRIDE_CFG_BIT, |
| 153 | override ? STAT_SW_OVERRIDE_CFG_BIT : 0); |
| 154 | if (rc < 0) { |
| 155 | dev_err(chg->dev, "Couldn't configure SW STAT override rc=%d\n", |
| 156 | rc); |
| 157 | return rc; |
| 158 | } |
| 159 | |
| 160 | return rc; |
| 161 | } |
| 162 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 163 | /******************** |
| 164 | * REGISTER GETTERS * |
| 165 | ********************/ |
| 166 | |
Nicholas Troast | 4c31049 | 2016-05-12 17:56:35 -0700 | [diff] [blame] | 167 | int smblib_get_charge_param(struct smb_charger *chg, |
| 168 | struct smb_chg_param *param, int *val_u) |
| 169 | { |
| 170 | int rc = 0; |
| 171 | u8 val_raw; |
| 172 | |
| 173 | rc = smblib_read(chg, param->reg, &val_raw); |
| 174 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 175 | smblib_err(chg, "%s: Couldn't read from 0x%04x rc=%d\n", |
Nicholas Troast | 4c31049 | 2016-05-12 17:56:35 -0700 | [diff] [blame] | 176 | param->name, param->reg, rc); |
| 177 | return rc; |
| 178 | } |
| 179 | |
Harry Yang | f8b4125 | 2016-08-10 14:21:10 -0700 | [diff] [blame] | 180 | if (param->get_proc) |
| 181 | *val_u = param->get_proc(param, val_raw); |
| 182 | else |
| 183 | *val_u = val_raw * param->step_u + param->min_u; |
Nicholas Troast | 4c31049 | 2016-05-12 17:56:35 -0700 | [diff] [blame] | 184 | smblib_dbg(chg, PR_REGISTER, "%s = %d (0x%02x)\n", |
| 185 | param->name, *val_u, val_raw); |
| 186 | |
| 187 | return rc; |
| 188 | } |
| 189 | |
| 190 | int smblib_get_usb_suspend(struct smb_charger *chg, int *suspend) |
| 191 | { |
| 192 | int rc = 0; |
| 193 | u8 temp; |
| 194 | |
| 195 | rc = smblib_read(chg, USBIN_CMD_IL_REG, &temp); |
| 196 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 197 | smblib_err(chg, "Couldn't read USBIN_CMD_IL rc=%d\n", rc); |
Nicholas Troast | 4c31049 | 2016-05-12 17:56:35 -0700 | [diff] [blame] | 198 | return rc; |
| 199 | } |
| 200 | *suspend = temp & USBIN_SUSPEND_BIT; |
| 201 | |
| 202 | return rc; |
| 203 | } |
| 204 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 205 | struct apsd_result { |
| 206 | const char * const name; |
| 207 | const u8 bit; |
| 208 | const enum power_supply_type pst; |
| 209 | }; |
| 210 | |
Abhijeet Dharmapurikar | f0b0a04 | 2016-10-10 16:43:43 -0700 | [diff] [blame] | 211 | enum { |
| 212 | UNKNOWN, |
| 213 | SDP, |
| 214 | CDP, |
| 215 | DCP, |
| 216 | OCP, |
| 217 | FLOAT, |
| 218 | HVDCP2, |
| 219 | HVDCP3, |
| 220 | MAX_TYPES |
| 221 | }; |
| 222 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 223 | static const struct apsd_result const smblib_apsd_results[] = { |
Abhijeet Dharmapurikar | f0b0a04 | 2016-10-10 16:43:43 -0700 | [diff] [blame] | 224 | [UNKNOWN] = { |
| 225 | .name = "UNKNOWN", |
| 226 | .bit = 0, |
| 227 | .pst = POWER_SUPPLY_TYPE_UNKNOWN |
| 228 | }, |
| 229 | [SDP] = { |
| 230 | .name = "SDP", |
| 231 | .bit = SDP_CHARGER_BIT, |
| 232 | .pst = POWER_SUPPLY_TYPE_USB |
| 233 | }, |
| 234 | [CDP] = { |
| 235 | .name = "CDP", |
| 236 | .bit = CDP_CHARGER_BIT, |
| 237 | .pst = POWER_SUPPLY_TYPE_USB_CDP |
| 238 | }, |
| 239 | [DCP] = { |
| 240 | .name = "DCP", |
| 241 | .bit = DCP_CHARGER_BIT, |
| 242 | .pst = POWER_SUPPLY_TYPE_USB_DCP |
| 243 | }, |
| 244 | [OCP] = { |
| 245 | .name = "OCP", |
| 246 | .bit = OCP_CHARGER_BIT, |
| 247 | .pst = POWER_SUPPLY_TYPE_USB_DCP |
| 248 | }, |
| 249 | [FLOAT] = { |
| 250 | .name = "FLOAT", |
| 251 | .bit = FLOAT_CHARGER_BIT, |
Ashay Jaiswal | b9b2d2f | 2017-06-21 12:08:38 +0530 | [diff] [blame] | 252 | .pst = POWER_SUPPLY_TYPE_USB_FLOAT |
Abhijeet Dharmapurikar | f0b0a04 | 2016-10-10 16:43:43 -0700 | [diff] [blame] | 253 | }, |
| 254 | [HVDCP2] = { |
| 255 | .name = "HVDCP2", |
| 256 | .bit = DCP_CHARGER_BIT | QC_2P0_BIT, |
| 257 | .pst = POWER_SUPPLY_TYPE_USB_HVDCP |
| 258 | }, |
| 259 | [HVDCP3] = { |
| 260 | .name = "HVDCP3", |
| 261 | .bit = DCP_CHARGER_BIT | QC_3P0_BIT, |
| 262 | .pst = POWER_SUPPLY_TYPE_USB_HVDCP_3, |
| 263 | }, |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 264 | }; |
| 265 | |
| 266 | static const struct apsd_result *smblib_get_apsd_result(struct smb_charger *chg) |
| 267 | { |
| 268 | int rc, i; |
Abhijeet Dharmapurikar | f0b0a04 | 2016-10-10 16:43:43 -0700 | [diff] [blame] | 269 | u8 apsd_stat, stat; |
| 270 | const struct apsd_result *result = &smblib_apsd_results[UNKNOWN]; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 271 | |
Abhijeet Dharmapurikar | f0b0a04 | 2016-10-10 16:43:43 -0700 | [diff] [blame] | 272 | rc = smblib_read(chg, APSD_STATUS_REG, &apsd_stat); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 273 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 274 | smblib_err(chg, "Couldn't read APSD_STATUS rc=%d\n", rc); |
Abhijeet Dharmapurikar | f0b0a04 | 2016-10-10 16:43:43 -0700 | [diff] [blame] | 275 | return result; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 276 | } |
Abhijeet Dharmapurikar | f0b0a04 | 2016-10-10 16:43:43 -0700 | [diff] [blame] | 277 | smblib_dbg(chg, PR_REGISTER, "APSD_STATUS = 0x%02x\n", apsd_stat); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 278 | |
Abhijeet Dharmapurikar | f0b0a04 | 2016-10-10 16:43:43 -0700 | [diff] [blame] | 279 | if (!(apsd_stat & APSD_DTC_STATUS_DONE_BIT)) |
| 280 | return result; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 281 | |
| 282 | rc = smblib_read(chg, APSD_RESULT_STATUS_REG, &stat); |
| 283 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 284 | smblib_err(chg, "Couldn't read APSD_RESULT_STATUS rc=%d\n", |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 285 | rc); |
Abhijeet Dharmapurikar | f0b0a04 | 2016-10-10 16:43:43 -0700 | [diff] [blame] | 286 | return result; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 287 | } |
| 288 | stat &= APSD_RESULT_STATUS_MASK; |
| 289 | |
| 290 | for (i = 0; i < ARRAY_SIZE(smblib_apsd_results); i++) { |
| 291 | if (smblib_apsd_results[i].bit == stat) |
Abhijeet Dharmapurikar | f0b0a04 | 2016-10-10 16:43:43 -0700 | [diff] [blame] | 292 | result = &smblib_apsd_results[i]; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Abhijeet Dharmapurikar | f0b0a04 | 2016-10-10 16:43:43 -0700 | [diff] [blame] | 295 | if (apsd_stat & QC_CHARGER_BIT) { |
| 296 | /* since its a qc_charger, either return HVDCP3 or HVDCP2 */ |
| 297 | if (result != &smblib_apsd_results[HVDCP3]) |
| 298 | result = &smblib_apsd_results[HVDCP2]; |
| 299 | } |
| 300 | |
| 301 | return result; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 304 | /******************** |
| 305 | * REGISTER SETTERS * |
| 306 | ********************/ |
| 307 | |
Anirudh Ghayal | 4da3df9 | 2017-01-25 18:55:57 +0530 | [diff] [blame] | 308 | static int chg_freq_list[] = { |
| 309 | 9600, 9600, 6400, 4800, 3800, 3200, 2700, 2400, 2100, 1900, 1700, |
| 310 | 1600, 1500, 1400, 1300, 1200, |
| 311 | }; |
| 312 | |
| 313 | int smblib_set_chg_freq(struct smb_chg_param *param, |
| 314 | int val_u, u8 *val_raw) |
| 315 | { |
| 316 | u8 i; |
| 317 | |
| 318 | if (val_u > param->max_u || val_u < param->min_u) |
| 319 | return -EINVAL; |
| 320 | |
| 321 | /* Charger FSW is the configured freqency / 2 */ |
| 322 | val_u *= 2; |
| 323 | for (i = 0; i < ARRAY_SIZE(chg_freq_list); i++) { |
| 324 | if (chg_freq_list[i] == val_u) |
| 325 | break; |
| 326 | } |
| 327 | if (i == ARRAY_SIZE(chg_freq_list)) { |
| 328 | pr_err("Invalid frequency %d Hz\n", val_u / 2); |
| 329 | return -EINVAL; |
| 330 | } |
| 331 | |
| 332 | *val_raw = i; |
| 333 | |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | static int smblib_set_opt_freq_buck(struct smb_charger *chg, int fsw_khz) |
| 338 | { |
| 339 | union power_supply_propval pval = {0, }; |
| 340 | int rc = 0; |
| 341 | |
| 342 | rc = smblib_set_charge_param(chg, &chg->param.freq_buck, fsw_khz); |
| 343 | if (rc < 0) |
| 344 | dev_err(chg->dev, "Error in setting freq_buck rc=%d\n", rc); |
| 345 | |
| 346 | if (chg->mode == PARALLEL_MASTER && chg->pl.psy) { |
| 347 | pval.intval = fsw_khz; |
Abhijeet Dharmapurikar | e9fd08d | 2017-02-27 11:05:28 -0800 | [diff] [blame] | 348 | /* |
| 349 | * Some parallel charging implementations may not have |
| 350 | * PROP_BUCK_FREQ property - they could be running |
| 351 | * with a fixed frequency |
| 352 | */ |
| 353 | power_supply_set_property(chg->pl.psy, |
Anirudh Ghayal | 4da3df9 | 2017-01-25 18:55:57 +0530 | [diff] [blame] | 354 | POWER_SUPPLY_PROP_BUCK_FREQ, &pval); |
Anirudh Ghayal | 4da3df9 | 2017-01-25 18:55:57 +0530 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | return rc; |
| 358 | } |
| 359 | |
Nicholas Troast | 4c31049 | 2016-05-12 17:56:35 -0700 | [diff] [blame] | 360 | int smblib_set_charge_param(struct smb_charger *chg, |
| 361 | struct smb_chg_param *param, int val_u) |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 362 | { |
| 363 | int rc = 0; |
| 364 | u8 val_raw; |
| 365 | |
Harry Yang | f8b4125 | 2016-08-10 14:21:10 -0700 | [diff] [blame] | 366 | if (param->set_proc) { |
| 367 | rc = param->set_proc(param, val_u, &val_raw); |
| 368 | if (rc < 0) |
| 369 | return -EINVAL; |
| 370 | } else { |
| 371 | if (val_u > param->max_u || val_u < param->min_u) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 372 | smblib_err(chg, "%s: %d is out of range [%d, %d]\n", |
Harry Yang | f8b4125 | 2016-08-10 14:21:10 -0700 | [diff] [blame] | 373 | param->name, val_u, param->min_u, param->max_u); |
| 374 | return -EINVAL; |
| 375 | } |
| 376 | |
| 377 | val_raw = (val_u - param->min_u) / param->step_u; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 380 | rc = smblib_write(chg, param->reg, val_raw); |
| 381 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 382 | smblib_err(chg, "%s: Couldn't write 0x%02x to 0x%04x rc=%d\n", |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 383 | param->name, val_raw, param->reg, rc); |
| 384 | return rc; |
| 385 | } |
| 386 | |
| 387 | smblib_dbg(chg, PR_REGISTER, "%s = %d (0x%02x)\n", |
| 388 | param->name, val_u, val_raw); |
| 389 | |
| 390 | return rc; |
| 391 | } |
| 392 | |
Nicholas Troast | 4c31049 | 2016-05-12 17:56:35 -0700 | [diff] [blame] | 393 | int smblib_set_usb_suspend(struct smb_charger *chg, bool suspend) |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 394 | { |
| 395 | int rc = 0; |
Abhijeet Dharmapurikar | 109802f71 | 2017-07-07 18:30:54 -0700 | [diff] [blame] | 396 | int irq = chg->irq_info[USBIN_ICL_CHANGE_IRQ].irq; |
| 397 | |
| 398 | if (suspend && irq) { |
| 399 | if (chg->usb_icl_change_irq_enabled) { |
| 400 | disable_irq_nosync(irq); |
| 401 | chg->usb_icl_change_irq_enabled = false; |
| 402 | } |
| 403 | } |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 404 | |
| 405 | rc = smblib_masked_write(chg, USBIN_CMD_IL_REG, USBIN_SUSPEND_BIT, |
| 406 | suspend ? USBIN_SUSPEND_BIT : 0); |
| 407 | if (rc < 0) |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 408 | smblib_err(chg, "Couldn't write %s to USBIN_SUSPEND_BIT rc=%d\n", |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 409 | suspend ? "suspend" : "resume", rc); |
| 410 | |
Abhijeet Dharmapurikar | 109802f71 | 2017-07-07 18:30:54 -0700 | [diff] [blame] | 411 | if (!suspend && irq) { |
| 412 | if (!chg->usb_icl_change_irq_enabled) { |
| 413 | enable_irq(irq); |
| 414 | chg->usb_icl_change_irq_enabled = true; |
| 415 | } |
| 416 | } |
| 417 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 418 | return rc; |
| 419 | } |
| 420 | |
Nicholas Troast | 4c31049 | 2016-05-12 17:56:35 -0700 | [diff] [blame] | 421 | int smblib_set_dc_suspend(struct smb_charger *chg, bool suspend) |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 422 | { |
| 423 | int rc = 0; |
| 424 | |
| 425 | rc = smblib_masked_write(chg, DCIN_CMD_IL_REG, DCIN_SUSPEND_BIT, |
| 426 | suspend ? DCIN_SUSPEND_BIT : 0); |
| 427 | if (rc < 0) |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 428 | smblib_err(chg, "Couldn't write %s to DCIN_SUSPEND_BIT rc=%d\n", |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 429 | suspend ? "suspend" : "resume", rc); |
| 430 | |
| 431 | return rc; |
| 432 | } |
| 433 | |
Ashay Jaiswal | b310101 | 2017-03-01 10:18:04 +0530 | [diff] [blame] | 434 | static int smblib_set_adapter_allowance(struct smb_charger *chg, |
| 435 | u8 allowed_voltage) |
| 436 | { |
| 437 | int rc = 0; |
| 438 | |
Nicholas Troast | 7f55c92 | 2017-07-25 13:18:03 -0700 | [diff] [blame] | 439 | /* PM660 only support max. 9V */ |
| 440 | if (chg->smb_version == PM660_SUBTYPE) { |
| 441 | switch (allowed_voltage) { |
| 442 | case USBIN_ADAPTER_ALLOW_12V: |
| 443 | case USBIN_ADAPTER_ALLOW_9V_TO_12V: |
| 444 | allowed_voltage = USBIN_ADAPTER_ALLOW_9V; |
| 445 | break; |
| 446 | case USBIN_ADAPTER_ALLOW_5V_OR_12V: |
| 447 | case USBIN_ADAPTER_ALLOW_5V_OR_9V_TO_12V: |
| 448 | allowed_voltage = USBIN_ADAPTER_ALLOW_5V_OR_9V; |
| 449 | break; |
| 450 | case USBIN_ADAPTER_ALLOW_5V_TO_12V: |
Ashay Jaiswal | b310101 | 2017-03-01 10:18:04 +0530 | [diff] [blame] | 451 | allowed_voltage = USBIN_ADAPTER_ALLOW_5V_TO_9V; |
Nicholas Troast | 7f55c92 | 2017-07-25 13:18:03 -0700 | [diff] [blame] | 452 | break; |
Ashay Jaiswal | b310101 | 2017-03-01 10:18:04 +0530 | [diff] [blame] | 453 | } |
Ashay Jaiswal | b310101 | 2017-03-01 10:18:04 +0530 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | rc = smblib_write(chg, USBIN_ADAPTER_ALLOW_CFG_REG, allowed_voltage); |
| 457 | if (rc < 0) { |
| 458 | smblib_err(chg, "Couldn't write 0x%02x to USBIN_ADAPTER_ALLOW_CFG rc=%d\n", |
| 459 | allowed_voltage, rc); |
| 460 | return rc; |
| 461 | } |
| 462 | |
| 463 | return rc; |
| 464 | } |
| 465 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 466 | #define MICRO_5V 5000000 |
| 467 | #define MICRO_9V 9000000 |
| 468 | #define MICRO_12V 12000000 |
| 469 | static int smblib_set_usb_pd_allowed_voltage(struct smb_charger *chg, |
| 470 | int min_allowed_uv, int max_allowed_uv) |
| 471 | { |
| 472 | int rc; |
| 473 | u8 allowed_voltage; |
| 474 | |
| 475 | if (min_allowed_uv == MICRO_5V && max_allowed_uv == MICRO_5V) { |
| 476 | allowed_voltage = USBIN_ADAPTER_ALLOW_5V; |
Anirudh Ghayal | 4da3df9 | 2017-01-25 18:55:57 +0530 | [diff] [blame] | 477 | smblib_set_opt_freq_buck(chg, chg->chg_freq.freq_5V); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 478 | } else if (min_allowed_uv == MICRO_9V && max_allowed_uv == MICRO_9V) { |
| 479 | allowed_voltage = USBIN_ADAPTER_ALLOW_9V; |
Anirudh Ghayal | 4da3df9 | 2017-01-25 18:55:57 +0530 | [diff] [blame] | 480 | smblib_set_opt_freq_buck(chg, chg->chg_freq.freq_9V); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 481 | } else if (min_allowed_uv == MICRO_12V && max_allowed_uv == MICRO_12V) { |
| 482 | allowed_voltage = USBIN_ADAPTER_ALLOW_12V; |
Anirudh Ghayal | 4da3df9 | 2017-01-25 18:55:57 +0530 | [diff] [blame] | 483 | smblib_set_opt_freq_buck(chg, chg->chg_freq.freq_12V); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 484 | } else if (min_allowed_uv < MICRO_9V && max_allowed_uv <= MICRO_9V) { |
| 485 | allowed_voltage = USBIN_ADAPTER_ALLOW_5V_TO_9V; |
| 486 | } else if (min_allowed_uv < MICRO_9V && max_allowed_uv <= MICRO_12V) { |
| 487 | allowed_voltage = USBIN_ADAPTER_ALLOW_5V_TO_12V; |
| 488 | } else if (min_allowed_uv < MICRO_12V && max_allowed_uv <= MICRO_12V) { |
| 489 | allowed_voltage = USBIN_ADAPTER_ALLOW_9V_TO_12V; |
| 490 | } else { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 491 | smblib_err(chg, "invalid allowed voltage [%d, %d]\n", |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 492 | min_allowed_uv, max_allowed_uv); |
| 493 | return -EINVAL; |
| 494 | } |
| 495 | |
Ashay Jaiswal | b310101 | 2017-03-01 10:18:04 +0530 | [diff] [blame] | 496 | rc = smblib_set_adapter_allowance(chg, allowed_voltage); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 497 | if (rc < 0) { |
Ashay Jaiswal | b310101 | 2017-03-01 10:18:04 +0530 | [diff] [blame] | 498 | smblib_err(chg, "Couldn't configure adapter allowance rc=%d\n", |
| 499 | rc); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 500 | return rc; |
| 501 | } |
| 502 | |
| 503 | return rc; |
| 504 | } |
| 505 | |
| 506 | /******************** |
| 507 | * HELPER FUNCTIONS * |
| 508 | ********************/ |
Ashay Jaiswal | 13a1b81 | 2017-07-17 14:49:05 +0530 | [diff] [blame] | 509 | static int smblib_request_dpdm(struct smb_charger *chg, bool enable) |
| 510 | { |
| 511 | int rc = 0; |
| 512 | |
| 513 | /* fetch the DPDM regulator */ |
| 514 | if (!chg->dpdm_reg && of_get_property(chg->dev->of_node, |
| 515 | "dpdm-supply", NULL)) { |
| 516 | chg->dpdm_reg = devm_regulator_get(chg->dev, "dpdm"); |
| 517 | if (IS_ERR(chg->dpdm_reg)) { |
| 518 | rc = PTR_ERR(chg->dpdm_reg); |
| 519 | smblib_err(chg, "Couldn't get dpdm regulator rc=%d\n", |
| 520 | rc); |
| 521 | chg->dpdm_reg = NULL; |
| 522 | return rc; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | if (enable) { |
| 527 | if (chg->dpdm_reg && !regulator_is_enabled(chg->dpdm_reg)) { |
| 528 | smblib_dbg(chg, PR_MISC, "enabling DPDM regulator\n"); |
| 529 | rc = regulator_enable(chg->dpdm_reg); |
| 530 | if (rc < 0) |
| 531 | smblib_err(chg, |
| 532 | "Couldn't enable dpdm regulator rc=%d\n", |
| 533 | rc); |
| 534 | } |
| 535 | } else { |
| 536 | if (chg->dpdm_reg && regulator_is_enabled(chg->dpdm_reg)) { |
| 537 | smblib_dbg(chg, PR_MISC, "disabling DPDM regulator\n"); |
| 538 | rc = regulator_disable(chg->dpdm_reg); |
| 539 | if (rc < 0) |
| 540 | smblib_err(chg, |
| 541 | "Couldn't disable dpdm regulator rc=%d\n", |
| 542 | rc); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | return rc; |
| 547 | } |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 548 | |
Nicholas Troast | 93fb0f0 | 2017-03-01 10:17:13 -0800 | [diff] [blame] | 549 | static void smblib_rerun_apsd(struct smb_charger *chg) |
| 550 | { |
| 551 | int rc; |
| 552 | |
| 553 | smblib_dbg(chg, PR_MISC, "re-running APSD\n"); |
| 554 | if (chg->wa_flags & QC_AUTH_INTERRUPT_WA_BIT) { |
| 555 | rc = smblib_masked_write(chg, |
| 556 | USBIN_SOURCE_CHANGE_INTRPT_ENB_REG, |
| 557 | AUTH_IRQ_EN_CFG_BIT, AUTH_IRQ_EN_CFG_BIT); |
| 558 | if (rc < 0) |
| 559 | smblib_err(chg, "Couldn't enable HVDCP auth IRQ rc=%d\n", |
| 560 | rc); |
| 561 | } |
| 562 | |
| 563 | rc = smblib_masked_write(chg, CMD_APSD_REG, |
| 564 | APSD_RERUN_BIT, APSD_RERUN_BIT); |
| 565 | if (rc < 0) |
| 566 | smblib_err(chg, "Couldn't re-run APSD rc=%d\n", rc); |
| 567 | } |
| 568 | |
Abhijeet Dharmapurikar | b959887 | 2016-10-17 16:58:58 -0700 | [diff] [blame] | 569 | static const struct apsd_result *smblib_update_usb_type(struct smb_charger *chg) |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 570 | { |
Abhijeet Dharmapurikar | eda0822 | 2016-11-01 11:35:29 -0700 | [diff] [blame] | 571 | const struct apsd_result *apsd_result = smblib_get_apsd_result(chg); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 572 | |
Jack Pham | 9696e25 | 2016-05-23 11:15:15 -0700 | [diff] [blame] | 573 | /* if PD is active, APSD is disabled so won't have a valid result */ |
Anirudh Ghayal | 1121f5d | 2017-07-26 20:26:20 +0530 | [diff] [blame] | 574 | if (chg->pd_active) { |
Fenglin Wu | 80826e0 | 2017-04-25 21:45:08 +0800 | [diff] [blame] | 575 | chg->real_charger_type = POWER_SUPPLY_TYPE_USB_PD; |
Anirudh Ghayal | 1121f5d | 2017-07-26 20:26:20 +0530 | [diff] [blame] | 576 | } else { |
| 577 | /* |
| 578 | * Update real charger type only if its not FLOAT |
| 579 | * detected as as SDP |
| 580 | */ |
| 581 | if (!(apsd_result->pst == POWER_SUPPLY_TYPE_USB_FLOAT && |
| 582 | chg->real_charger_type == POWER_SUPPLY_TYPE_USB)) |
Fenglin Wu | 80826e0 | 2017-04-25 21:45:08 +0800 | [diff] [blame] | 583 | chg->real_charger_type = apsd_result->pst; |
Anirudh Ghayal | 1121f5d | 2017-07-26 20:26:20 +0530 | [diff] [blame] | 584 | } |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 585 | |
Nicholas Troast | 93fb0f0 | 2017-03-01 10:17:13 -0800 | [diff] [blame] | 586 | smblib_dbg(chg, PR_MISC, "APSD=%s PD=%d\n", |
| 587 | apsd_result->name, chg->pd_active); |
Abhijeet Dharmapurikar | b959887 | 2016-10-17 16:58:58 -0700 | [diff] [blame] | 588 | return apsd_result; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 589 | } |
| 590 | |
Harry Yang | 5e1a522 | 2016-07-26 15:16:04 -0700 | [diff] [blame] | 591 | static int smblib_notifier_call(struct notifier_block *nb, |
Harry Yang | 58a9e7a | 2016-06-23 14:54:43 -0700 | [diff] [blame] | 592 | unsigned long ev, void *v) |
Harry Yang | 1d1034c | 2016-06-15 12:09:42 -0700 | [diff] [blame] | 593 | { |
Harry Yang | 58a9e7a | 2016-06-23 14:54:43 -0700 | [diff] [blame] | 594 | struct power_supply *psy = v; |
Harry Yang | 5e1a522 | 2016-07-26 15:16:04 -0700 | [diff] [blame] | 595 | struct smb_charger *chg = container_of(nb, struct smb_charger, nb); |
Harry Yang | 1d1034c | 2016-06-15 12:09:42 -0700 | [diff] [blame] | 596 | |
Harry Yang | 5e1a522 | 2016-07-26 15:16:04 -0700 | [diff] [blame] | 597 | if (!strcmp(psy->desc->name, "bms")) { |
| 598 | if (!chg->bms_psy) |
| 599 | chg->bms_psy = psy; |
Ashay Jaiswal | 2fb369d | 2017-01-12 21:38:29 +0530 | [diff] [blame] | 600 | if (ev == PSY_EVENT_PROP_CHANGED) |
Harry Yang | 5e1a522 | 2016-07-26 15:16:04 -0700 | [diff] [blame] | 601 | schedule_work(&chg->bms_update_work); |
| 602 | } |
| 603 | |
Harry Yang | 166b15d | 2017-08-28 13:00:40 -0700 | [diff] [blame^] | 604 | if (!chg->pl.psy && !strcmp(psy->desc->name, "parallel")) { |
Harry Yang | 58a9e7a | 2016-06-23 14:54:43 -0700 | [diff] [blame] | 605 | chg->pl.psy = psy; |
Harry Yang | 166b15d | 2017-08-28 13:00:40 -0700 | [diff] [blame^] | 606 | schedule_work(&chg->pl_update_work); |
| 607 | } |
Harry Yang | 1d1034c | 2016-06-15 12:09:42 -0700 | [diff] [blame] | 608 | |
Harry Yang | 58a9e7a | 2016-06-23 14:54:43 -0700 | [diff] [blame] | 609 | return NOTIFY_OK; |
| 610 | } |
| 611 | |
Harry Yang | 5e1a522 | 2016-07-26 15:16:04 -0700 | [diff] [blame] | 612 | static int smblib_register_notifier(struct smb_charger *chg) |
Harry Yang | 58a9e7a | 2016-06-23 14:54:43 -0700 | [diff] [blame] | 613 | { |
| 614 | int rc; |
| 615 | |
Harry Yang | 5e1a522 | 2016-07-26 15:16:04 -0700 | [diff] [blame] | 616 | chg->nb.notifier_call = smblib_notifier_call; |
| 617 | rc = power_supply_reg_notifier(&chg->nb); |
Harry Yang | 58a9e7a | 2016-06-23 14:54:43 -0700 | [diff] [blame] | 618 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 619 | smblib_err(chg, "Couldn't register psy notifier rc = %d\n", rc); |
Harry Yang | 58a9e7a | 2016-06-23 14:54:43 -0700 | [diff] [blame] | 620 | return rc; |
| 621 | } |
| 622 | |
| 623 | return 0; |
Harry Yang | 1d1034c | 2016-06-15 12:09:42 -0700 | [diff] [blame] | 624 | } |
| 625 | |
Harry Yang | fe91384 | 2016-08-10 12:27:28 -0700 | [diff] [blame] | 626 | int smblib_mapping_soc_from_field_value(struct smb_chg_param *param, |
| 627 | int val_u, u8 *val_raw) |
| 628 | { |
| 629 | if (val_u > param->max_u || val_u < param->min_u) |
| 630 | return -EINVAL; |
| 631 | |
| 632 | *val_raw = val_u << 1; |
| 633 | |
| 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | int smblib_mapping_cc_delta_to_field_value(struct smb_chg_param *param, |
| 638 | u8 val_raw) |
| 639 | { |
| 640 | int val_u = val_raw * param->step_u + param->min_u; |
| 641 | |
| 642 | if (val_u > param->max_u) |
| 643 | val_u -= param->max_u * 2; |
| 644 | |
| 645 | return val_u; |
| 646 | } |
| 647 | |
| 648 | int smblib_mapping_cc_delta_from_field_value(struct smb_chg_param *param, |
| 649 | int val_u, u8 *val_raw) |
| 650 | { |
| 651 | if (val_u > param->max_u || val_u < param->min_u - param->max_u) |
| 652 | return -EINVAL; |
| 653 | |
| 654 | val_u += param->max_u * 2 - param->min_u; |
| 655 | val_u %= param->max_u * 2; |
| 656 | *val_raw = val_u / param->step_u; |
| 657 | |
| 658 | return 0; |
| 659 | } |
| 660 | |
Ashay Jaiswal | 4aa2c0c | 2016-12-07 19:39:38 +0530 | [diff] [blame] | 661 | static void smblib_uusb_removal(struct smb_charger *chg) |
| 662 | { |
| 663 | int rc; |
Ashay Jaiswal | 2f3b0c1 | 2017-06-14 16:04:45 +0530 | [diff] [blame] | 664 | struct smb_irq_data *data; |
| 665 | struct storm_watch *wdata; |
Ashay Jaiswal | 4aa2c0c | 2016-12-07 19:39:38 +0530 | [diff] [blame] | 666 | |
Ashay Jaiswal | c036167 | 2017-03-21 12:24:16 +0530 | [diff] [blame] | 667 | cancel_delayed_work_sync(&chg->pl_enable_work); |
Anirudh Ghayal | 36feefe | 2017-05-26 09:41:25 +0530 | [diff] [blame] | 668 | |
Ashay Jaiswal | 13a1b81 | 2017-07-17 14:49:05 +0530 | [diff] [blame] | 669 | rc = smblib_request_dpdm(chg, false); |
| 670 | if (rc < 0) |
| 671 | smblib_err(chg, "Couldn't to disable DPDM rc=%d\n", rc); |
Anirudh Ghayal | 36feefe | 2017-05-26 09:41:25 +0530 | [diff] [blame] | 672 | |
Ashay Jaiswal | 2f3b0c1 | 2017-06-14 16:04:45 +0530 | [diff] [blame] | 673 | if (chg->wa_flags & BOOST_BACK_WA) { |
| 674 | data = chg->irq_info[SWITCH_POWER_OK_IRQ].irq_data; |
| 675 | if (data) { |
| 676 | wdata = &data->storm_data; |
| 677 | update_storm_count(wdata, WEAK_CHG_STORM_COUNT); |
| 678 | vote(chg->usb_icl_votable, BOOST_BACK_VOTER, false, 0); |
| 679 | vote(chg->usb_icl_votable, WEAK_CHARGER_VOTER, |
| 680 | false, 0); |
| 681 | } |
| 682 | } |
Ashay Jaiswal | c036167 | 2017-03-21 12:24:16 +0530 | [diff] [blame] | 683 | vote(chg->pl_disable_votable, PL_DELAY_VOTER, true, 0); |
| 684 | vote(chg->awake_votable, PL_DELAY_VOTER, false, 0); |
| 685 | |
Ashay Jaiswal | 4aa2c0c | 2016-12-07 19:39:38 +0530 | [diff] [blame] | 686 | /* reset both usbin current and voltage votes */ |
| 687 | vote(chg->pl_enable_votable_indirect, USBIN_I_VOTER, false, 0); |
| 688 | vote(chg->pl_enable_votable_indirect, USBIN_V_VOTER, false, 0); |
Ashay Jaiswal | ae23a04 | 2017-05-18 15:28:31 +0530 | [diff] [blame] | 689 | vote(chg->usb_icl_votable, SW_QC3_VOTER, false, 0); |
Ashay Jaiswal | 4aa2c0c | 2016-12-07 19:39:38 +0530 | [diff] [blame] | 690 | |
| 691 | cancel_delayed_work_sync(&chg->hvdcp_detect_work); |
| 692 | |
Ashay Jaiswal | 67ec707 | 2017-02-16 14:14:58 +0530 | [diff] [blame] | 693 | if (chg->wa_flags & QC_AUTH_INTERRUPT_WA_BIT) { |
| 694 | /* re-enable AUTH_IRQ_EN_CFG_BIT */ |
| 695 | rc = smblib_masked_write(chg, |
| 696 | USBIN_SOURCE_CHANGE_INTRPT_ENB_REG, |
| 697 | AUTH_IRQ_EN_CFG_BIT, AUTH_IRQ_EN_CFG_BIT); |
| 698 | if (rc < 0) |
| 699 | smblib_err(chg, |
| 700 | "Couldn't enable QC auth setting rc=%d\n", rc); |
| 701 | } |
Ashay Jaiswal | 4aa2c0c | 2016-12-07 19:39:38 +0530 | [diff] [blame] | 702 | |
| 703 | /* reconfigure allowed voltage for HVDCP */ |
Ashay Jaiswal | b310101 | 2017-03-01 10:18:04 +0530 | [diff] [blame] | 704 | rc = smblib_set_adapter_allowance(chg, |
| 705 | USBIN_ADAPTER_ALLOW_5V_OR_9V_TO_12V); |
Ashay Jaiswal | 4aa2c0c | 2016-12-07 19:39:38 +0530 | [diff] [blame] | 706 | if (rc < 0) |
| 707 | smblib_err(chg, "Couldn't set USBIN_ADAPTER_ALLOW_5V_OR_9V_TO_12V rc=%d\n", |
| 708 | rc); |
| 709 | |
| 710 | chg->voltage_min_uv = MICRO_5V; |
| 711 | chg->voltage_max_uv = MICRO_5V; |
Ashay Jaiswal | 6d308da | 2017-02-18 09:59:23 +0530 | [diff] [blame] | 712 | chg->usb_icl_delta_ua = 0; |
| 713 | chg->pulse_cnt = 0; |
Ashay Jaiswal | 8507aa5 | 2017-04-14 09:42:32 +0530 | [diff] [blame] | 714 | chg->uusb_apsd_rerun_done = false; |
Ashay Jaiswal | 4aa2c0c | 2016-12-07 19:39:38 +0530 | [diff] [blame] | 715 | |
| 716 | /* clear USB ICL vote for USB_PSY_VOTER */ |
| 717 | rc = vote(chg->usb_icl_votable, USB_PSY_VOTER, false, 0); |
| 718 | if (rc < 0) |
| 719 | smblib_err(chg, "Couldn't un-vote for USB ICL rc=%d\n", rc); |
Abhijeet Dharmapurikar | 7442e42 | 2017-02-08 13:35:14 -0800 | [diff] [blame] | 720 | |
| 721 | /* clear USB ICL vote for DCP_VOTER */ |
| 722 | rc = vote(chg->usb_icl_votable, DCP_VOTER, false, 0); |
| 723 | if (rc < 0) |
| 724 | smblib_err(chg, |
| 725 | "Couldn't un-vote DCP from USB ICL rc=%d\n", rc); |
Ashay Jaiswal | 4aa2c0c | 2016-12-07 19:39:38 +0530 | [diff] [blame] | 726 | } |
| 727 | |
Ashay Jaiswal | 2fb369d | 2017-01-12 21:38:29 +0530 | [diff] [blame] | 728 | void smblib_suspend_on_debug_battery(struct smb_charger *chg) |
| 729 | { |
| 730 | int rc; |
| 731 | union power_supply_propval val; |
| 732 | |
Ashay Jaiswal | da8669b | 2017-02-10 23:24:23 +0530 | [diff] [blame] | 733 | if (!chg->suspend_input_on_debug_batt) |
| 734 | return; |
| 735 | |
Ashay Jaiswal | 2fb369d | 2017-01-12 21:38:29 +0530 | [diff] [blame] | 736 | rc = power_supply_get_property(chg->bms_psy, |
| 737 | POWER_SUPPLY_PROP_DEBUG_BATTERY, &val); |
| 738 | if (rc < 0) { |
| 739 | smblib_err(chg, "Couldn't get debug battery prop rc=%d\n", rc); |
| 740 | return; |
| 741 | } |
| 742 | |
Abhijeet Dharmapurikar | 5e0bfbe | 2017-02-12 19:16:15 -0800 | [diff] [blame] | 743 | vote(chg->usb_icl_votable, DEBUG_BOARD_VOTER, val.intval, 0); |
Ashay Jaiswal | 2fb369d | 2017-01-12 21:38:29 +0530 | [diff] [blame] | 744 | vote(chg->dc_suspend_votable, DEBUG_BOARD_VOTER, val.intval, 0); |
| 745 | if (val.intval) |
| 746 | pr_info("Input suspended: Fake battery\n"); |
| 747 | } |
| 748 | |
Abhijeet Dharmapurikar | 9dd6129 | 2017-01-25 16:40:08 -0800 | [diff] [blame] | 749 | int smblib_rerun_apsd_if_required(struct smb_charger *chg) |
| 750 | { |
Abhijeet Dharmapurikar | 9dd6129 | 2017-01-25 16:40:08 -0800 | [diff] [blame] | 751 | union power_supply_propval val; |
| 752 | int rc; |
| 753 | |
| 754 | rc = smblib_get_prop_usb_present(chg, &val); |
| 755 | if (rc < 0) { |
| 756 | smblib_err(chg, "Couldn't get usb present rc = %d\n", rc); |
| 757 | return rc; |
| 758 | } |
| 759 | |
| 760 | if (!val.intval) |
| 761 | return 0; |
| 762 | |
Ashay Jaiswal | 13a1b81 | 2017-07-17 14:49:05 +0530 | [diff] [blame] | 763 | rc = smblib_request_dpdm(chg, true); |
| 764 | if (rc < 0) |
| 765 | smblib_err(chg, "Couldn't to enable DPDM rc=%d\n", rc); |
Abhijeet Dharmapurikar | 025e63a | 2017-02-24 14:06:22 -0800 | [diff] [blame] | 766 | |
Ashay Jaiswal | 8507aa5 | 2017-04-14 09:42:32 +0530 | [diff] [blame] | 767 | chg->uusb_apsd_rerun_done = true; |
Abhijeet Dharmapurikar | 025e63a | 2017-02-24 14:06:22 -0800 | [diff] [blame] | 768 | smblib_rerun_apsd(chg); |
| 769 | |
Abhijeet Dharmapurikar | 9dd6129 | 2017-01-25 16:40:08 -0800 | [diff] [blame] | 770 | return 0; |
| 771 | } |
| 772 | |
Ashay Jaiswal | cda0d1c | 2017-05-15 17:15:29 +0530 | [diff] [blame] | 773 | static int smblib_get_hw_pulse_cnt(struct smb_charger *chg, int *count) |
Ashay Jaiswal | 6d308da | 2017-02-18 09:59:23 +0530 | [diff] [blame] | 774 | { |
| 775 | int rc; |
| 776 | u8 val[2]; |
| 777 | |
| 778 | switch (chg->smb_version) { |
| 779 | case PMI8998_SUBTYPE: |
| 780 | rc = smblib_read(chg, QC_PULSE_COUNT_STATUS_REG, val); |
| 781 | if (rc) { |
| 782 | pr_err("failed to read QC_PULSE_COUNT_STATUS_REG rc=%d\n", |
| 783 | rc); |
| 784 | return rc; |
| 785 | } |
| 786 | *count = val[0] & QC_PULSE_COUNT_MASK; |
| 787 | break; |
| 788 | case PM660_SUBTYPE: |
| 789 | rc = smblib_multibyte_read(chg, |
| 790 | QC_PULSE_COUNT_STATUS_1_REG, val, 2); |
| 791 | if (rc) { |
| 792 | pr_err("failed to read QC_PULSE_COUNT_STATUS_1_REG rc=%d\n", |
| 793 | rc); |
| 794 | return rc; |
| 795 | } |
| 796 | *count = (val[1] << 8) | val[0]; |
| 797 | break; |
| 798 | default: |
| 799 | smblib_dbg(chg, PR_PARALLEL, "unknown SMB chip %d\n", |
| 800 | chg->smb_version); |
| 801 | return -EINVAL; |
| 802 | } |
| 803 | |
| 804 | return 0; |
| 805 | } |
| 806 | |
Ashay Jaiswal | cda0d1c | 2017-05-15 17:15:29 +0530 | [diff] [blame] | 807 | static int smblib_get_pulse_cnt(struct smb_charger *chg, int *count) |
| 808 | { |
| 809 | int rc; |
| 810 | |
| 811 | /* Use software based pulse count if HW INOV is disabled */ |
| 812 | if (get_effective_result(chg->hvdcp_hw_inov_dis_votable) > 0) { |
| 813 | *count = chg->pulse_cnt; |
| 814 | return 0; |
| 815 | } |
| 816 | |
| 817 | /* Use h/w pulse count if autonomous mode is enabled */ |
| 818 | rc = smblib_get_hw_pulse_cnt(chg, count); |
| 819 | if (rc < 0) |
| 820 | smblib_err(chg, "failed to read h/w pulse count rc=%d\n", rc); |
| 821 | |
| 822 | return rc; |
| 823 | } |
| 824 | |
Nicholas Troast | bb76a14 | 2016-09-23 11:23:23 -0700 | [diff] [blame] | 825 | #define USBIN_25MA 25000 |
| 826 | #define USBIN_100MA 100000 |
| 827 | #define USBIN_150MA 150000 |
| 828 | #define USBIN_500MA 500000 |
| 829 | #define USBIN_900MA 900000 |
Abhijeet Dharmapurikar | 5e0bfbe | 2017-02-12 19:16:15 -0800 | [diff] [blame] | 830 | |
Abhijeet Dharmapurikar | 5e0bfbe | 2017-02-12 19:16:15 -0800 | [diff] [blame] | 831 | static int set_sdp_current(struct smb_charger *chg, int icl_ua) |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 832 | { |
Abhijeet Dharmapurikar | 5e0bfbe | 2017-02-12 19:16:15 -0800 | [diff] [blame] | 833 | int rc; |
| 834 | u8 icl_options; |
Anirudh Ghayal | 1121f5d | 2017-07-26 20:26:20 +0530 | [diff] [blame] | 835 | const struct apsd_result *apsd_result = smblib_get_apsd_result(chg); |
Abhijeet Dharmapurikar | c0ff65a | 2016-06-06 16:45:34 -0700 | [diff] [blame] | 836 | |
Nicholas Troast | bb76a14 | 2016-09-23 11:23:23 -0700 | [diff] [blame] | 837 | /* power source is SDP */ |
| 838 | switch (icl_ua) { |
| 839 | case USBIN_100MA: |
| 840 | /* USB 2.0 100mA */ |
| 841 | icl_options = 0; |
| 842 | break; |
| 843 | case USBIN_150MA: |
| 844 | /* USB 3.0 150mA */ |
| 845 | icl_options = CFG_USB3P0_SEL_BIT; |
| 846 | break; |
| 847 | case USBIN_500MA: |
| 848 | /* USB 2.0 500mA */ |
| 849 | icl_options = USB51_MODE_BIT; |
| 850 | break; |
| 851 | case USBIN_900MA: |
| 852 | /* USB 3.0 900mA */ |
| 853 | icl_options = CFG_USB3P0_SEL_BIT | USB51_MODE_BIT; |
| 854 | break; |
| 855 | default: |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 856 | smblib_err(chg, "ICL %duA isn't supported for SDP\n", icl_ua); |
Abhijeet Dharmapurikar | 5e0bfbe | 2017-02-12 19:16:15 -0800 | [diff] [blame] | 857 | return -EINVAL; |
Nicholas Troast | bb76a14 | 2016-09-23 11:23:23 -0700 | [diff] [blame] | 858 | } |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 859 | |
Anirudh Ghayal | 1121f5d | 2017-07-26 20:26:20 +0530 | [diff] [blame] | 860 | if (chg->real_charger_type == POWER_SUPPLY_TYPE_USB && |
| 861 | apsd_result->pst == POWER_SUPPLY_TYPE_USB_FLOAT) { |
| 862 | /* |
| 863 | * change the float charger configuration to SDP, if this |
| 864 | * is the case of SDP being detected as FLOAT |
| 865 | */ |
| 866 | rc = smblib_masked_write(chg, USBIN_OPTIONS_2_CFG_REG, |
| 867 | FORCE_FLOAT_SDP_CFG_BIT, FORCE_FLOAT_SDP_CFG_BIT); |
| 868 | if (rc < 0) { |
| 869 | smblib_err(chg, "Couldn't set float ICL options rc=%d\n", |
| 870 | rc); |
| 871 | return rc; |
| 872 | } |
| 873 | } |
| 874 | |
Nicholas Troast | bb76a14 | 2016-09-23 11:23:23 -0700 | [diff] [blame] | 875 | rc = smblib_masked_write(chg, USBIN_ICL_OPTIONS_REG, |
Abhijeet Dharmapurikar | 5e0bfbe | 2017-02-12 19:16:15 -0800 | [diff] [blame] | 876 | CFG_USB3P0_SEL_BIT | USB51_MODE_BIT, icl_options); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 877 | if (rc < 0) { |
Abhijeet Dharmapurikar | 74021fc | 2017-02-06 18:39:19 -0800 | [diff] [blame] | 878 | smblib_err(chg, "Couldn't set ICL options rc=%d\n", rc); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 879 | return rc; |
| 880 | } |
| 881 | |
Abhijeet Dharmapurikar | 5e0bfbe | 2017-02-12 19:16:15 -0800 | [diff] [blame] | 882 | return rc; |
| 883 | } |
| 884 | |
Abhijeet Dharmapurikar | eecf558 | 2017-04-24 13:33:07 -0700 | [diff] [blame] | 885 | static int get_sdp_current(struct smb_charger *chg, int *icl_ua) |
| 886 | { |
| 887 | int rc; |
| 888 | u8 icl_options; |
| 889 | bool usb3 = false; |
| 890 | |
| 891 | rc = smblib_read(chg, USBIN_ICL_OPTIONS_REG, &icl_options); |
| 892 | if (rc < 0) { |
| 893 | smblib_err(chg, "Couldn't get ICL options rc=%d\n", rc); |
| 894 | return rc; |
| 895 | } |
| 896 | |
| 897 | usb3 = (icl_options & CFG_USB3P0_SEL_BIT); |
| 898 | |
| 899 | if (icl_options & USB51_MODE_BIT) |
| 900 | *icl_ua = usb3 ? USBIN_900MA : USBIN_500MA; |
| 901 | else |
| 902 | *icl_ua = usb3 ? USBIN_150MA : USBIN_100MA; |
| 903 | |
| 904 | return rc; |
| 905 | } |
| 906 | |
Ashay Jaiswal | ae1586d | 2017-03-22 23:18:51 +0530 | [diff] [blame] | 907 | int smblib_set_icl_current(struct smb_charger *chg, int icl_ua) |
Abhijeet Dharmapurikar | 5e0bfbe | 2017-02-12 19:16:15 -0800 | [diff] [blame] | 908 | { |
Abhijeet Dharmapurikar | 5e0bfbe | 2017-02-12 19:16:15 -0800 | [diff] [blame] | 909 | int rc = 0; |
| 910 | bool override; |
Abhijeet Dharmapurikar | 5e0bfbe | 2017-02-12 19:16:15 -0800 | [diff] [blame] | 911 | |
| 912 | /* suspend and return if 25mA or less is requested */ |
Harry Yang | 379484a | 2017-08-29 10:26:14 -0700 | [diff] [blame] | 913 | if (icl_ua <= USBIN_25MA) |
Abhijeet Dharmapurikar | 5e0bfbe | 2017-02-12 19:16:15 -0800 | [diff] [blame] | 914 | return smblib_set_usb_suspend(chg, true); |
| 915 | |
Ashay Jaiswal | ae1586d | 2017-03-22 23:18:51 +0530 | [diff] [blame] | 916 | if (icl_ua == INT_MAX) |
Abhijeet Dharmapurikar | 5e0bfbe | 2017-02-12 19:16:15 -0800 | [diff] [blame] | 917 | goto override_suspend_config; |
| 918 | |
Abhijeet Dharmapurikar | 5e0bfbe | 2017-02-12 19:16:15 -0800 | [diff] [blame] | 919 | /* configure current */ |
Nicholas Troast | e1932e4 | 2017-04-12 12:38:18 -0700 | [diff] [blame] | 920 | if (chg->typec_mode == POWER_SUPPLY_TYPEC_SOURCE_DEFAULT |
Fenglin Wu | 80826e0 | 2017-04-25 21:45:08 +0800 | [diff] [blame] | 921 | && (chg->real_charger_type == POWER_SUPPLY_TYPE_USB)) { |
Abhijeet Dharmapurikar | 5e0bfbe | 2017-02-12 19:16:15 -0800 | [diff] [blame] | 922 | rc = set_sdp_current(chg, icl_ua); |
| 923 | if (rc < 0) { |
| 924 | smblib_err(chg, "Couldn't set SDP ICL rc=%d\n", rc); |
| 925 | goto enable_icl_changed_interrupt; |
| 926 | } |
| 927 | } else { |
Abhijeet Dharmapurikar | 949d67e | 2017-05-25 15:10:56 -0700 | [diff] [blame] | 928 | set_sdp_current(chg, 100000); |
Abhijeet Dharmapurikar | f59c835 | 2017-03-23 14:04:05 -0700 | [diff] [blame] | 929 | rc = smblib_set_charge_param(chg, &chg->param.usb_icl, icl_ua); |
Abhijeet Dharmapurikar | 5e0bfbe | 2017-02-12 19:16:15 -0800 | [diff] [blame] | 930 | if (rc < 0) { |
| 931 | smblib_err(chg, "Couldn't set HC ICL rc=%d\n", rc); |
| 932 | goto enable_icl_changed_interrupt; |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | override_suspend_config: |
| 937 | /* determine if override needs to be enforced */ |
| 938 | override = true; |
Ashay Jaiswal | ae1586d | 2017-03-22 23:18:51 +0530 | [diff] [blame] | 939 | if (icl_ua == INT_MAX) { |
Abhijeet Dharmapurikar | 5e0bfbe | 2017-02-12 19:16:15 -0800 | [diff] [blame] | 940 | /* remove override if no voters - hw defaults is desired */ |
| 941 | override = false; |
Nicholas Troast | e1932e4 | 2017-04-12 12:38:18 -0700 | [diff] [blame] | 942 | } else if (chg->typec_mode == POWER_SUPPLY_TYPEC_SOURCE_DEFAULT) { |
Fenglin Wu | 80826e0 | 2017-04-25 21:45:08 +0800 | [diff] [blame] | 943 | if (chg->real_charger_type == POWER_SUPPLY_TYPE_USB) |
Abhijeet Dharmapurikar | 5e0bfbe | 2017-02-12 19:16:15 -0800 | [diff] [blame] | 944 | /* For std cable with type = SDP never override */ |
| 945 | override = false; |
Fenglin Wu | 80826e0 | 2017-04-25 21:45:08 +0800 | [diff] [blame] | 946 | else if (chg->real_charger_type == POWER_SUPPLY_TYPE_USB_CDP |
Abhijeet Dharmapurikar | f59c835 | 2017-03-23 14:04:05 -0700 | [diff] [blame] | 947 | && icl_ua == 1500000) |
Abhijeet Dharmapurikar | 5e0bfbe | 2017-02-12 19:16:15 -0800 | [diff] [blame] | 948 | /* |
| 949 | * For std cable with type = CDP override only if |
| 950 | * current is not 1500mA |
| 951 | */ |
| 952 | override = false; |
| 953 | } |
| 954 | |
| 955 | /* enforce override */ |
| 956 | rc = smblib_masked_write(chg, USBIN_ICL_OPTIONS_REG, |
| 957 | USBIN_MODE_CHG_BIT, override ? USBIN_MODE_CHG_BIT : 0); |
| 958 | |
Abhijeet Dharmapurikar | 74021fc | 2017-02-06 18:39:19 -0800 | [diff] [blame] | 959 | rc = smblib_icl_override(chg, override); |
| 960 | if (rc < 0) { |
| 961 | smblib_err(chg, "Couldn't set ICL override rc=%d\n", rc); |
Abhijeet Dharmapurikar | 5e0bfbe | 2017-02-12 19:16:15 -0800 | [diff] [blame] | 962 | goto enable_icl_changed_interrupt; |
Abhijeet Dharmapurikar | 74021fc | 2017-02-06 18:39:19 -0800 | [diff] [blame] | 963 | } |
| 964 | |
Abhijeet Dharmapurikar | 5e0bfbe | 2017-02-12 19:16:15 -0800 | [diff] [blame] | 965 | /* unsuspend after configuring current and override */ |
| 966 | rc = smblib_set_usb_suspend(chg, false); |
| 967 | if (rc < 0) { |
| 968 | smblib_err(chg, "Couldn't resume input rc=%d\n", rc); |
| 969 | goto enable_icl_changed_interrupt; |
| 970 | } |
| 971 | |
| 972 | enable_icl_changed_interrupt: |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 973 | return rc; |
| 974 | } |
| 975 | |
Abhijeet Dharmapurikar | eecf558 | 2017-04-24 13:33:07 -0700 | [diff] [blame] | 976 | int smblib_get_icl_current(struct smb_charger *chg, int *icl_ua) |
| 977 | { |
| 978 | int rc = 0; |
| 979 | u8 load_cfg; |
| 980 | bool override; |
Abhijeet Dharmapurikar | eecf558 | 2017-04-24 13:33:07 -0700 | [diff] [blame] | 981 | |
Nicholas Troast | e1932e4 | 2017-04-12 12:38:18 -0700 | [diff] [blame] | 982 | if ((chg->typec_mode == POWER_SUPPLY_TYPEC_SOURCE_DEFAULT |
Abhijeet Dharmapurikar | eecf558 | 2017-04-24 13:33:07 -0700 | [diff] [blame] | 983 | || chg->micro_usb_mode) |
| 984 | && (chg->usb_psy_desc.type == POWER_SUPPLY_TYPE_USB)) { |
| 985 | rc = get_sdp_current(chg, icl_ua); |
| 986 | if (rc < 0) { |
| 987 | smblib_err(chg, "Couldn't get SDP ICL rc=%d\n", rc); |
| 988 | return rc; |
| 989 | } |
| 990 | } else { |
| 991 | rc = smblib_read(chg, USBIN_LOAD_CFG_REG, &load_cfg); |
| 992 | if (rc < 0) { |
| 993 | smblib_err(chg, "Couldn't get load cfg rc=%d\n", rc); |
| 994 | return rc; |
| 995 | } |
| 996 | override = load_cfg & ICL_OVERRIDE_AFTER_APSD_BIT; |
| 997 | if (!override) |
| 998 | return INT_MAX; |
| 999 | |
| 1000 | /* override is set */ |
| 1001 | rc = smblib_get_charge_param(chg, &chg->param.usb_icl, icl_ua); |
| 1002 | if (rc < 0) { |
| 1003 | smblib_err(chg, "Couldn't get HC ICL rc=%d\n", rc); |
| 1004 | return rc; |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | return 0; |
| 1009 | } |
| 1010 | |
Ashay Jaiswal | ae1586d | 2017-03-22 23:18:51 +0530 | [diff] [blame] | 1011 | /********************* |
| 1012 | * VOTABLE CALLBACKS * |
| 1013 | *********************/ |
| 1014 | |
| 1015 | static int smblib_dc_suspend_vote_callback(struct votable *votable, void *data, |
| 1016 | int suspend, const char *client) |
| 1017 | { |
| 1018 | struct smb_charger *chg = data; |
| 1019 | |
| 1020 | /* resume input if suspend is invalid */ |
| 1021 | if (suspend < 0) |
| 1022 | suspend = 0; |
| 1023 | |
| 1024 | return smblib_set_dc_suspend(chg, (bool)suspend); |
| 1025 | } |
| 1026 | |
Abhijeet Dharmapurikar | 8e9e757 | 2016-06-06 16:13:14 -0700 | [diff] [blame] | 1027 | static int smblib_dc_icl_vote_callback(struct votable *votable, void *data, |
Abhijeet Dharmapurikar | ebb5e5c | 2016-05-17 20:09:16 -0700 | [diff] [blame] | 1028 | int icl_ua, const char *client) |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1029 | { |
Abhijeet Dharmapurikar | 8e9e757 | 2016-06-06 16:13:14 -0700 | [diff] [blame] | 1030 | struct smb_charger *chg = data; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1031 | int rc = 0; |
Abhijeet Dharmapurikar | c0ff65a | 2016-06-06 16:45:34 -0700 | [diff] [blame] | 1032 | bool suspend; |
| 1033 | |
| 1034 | if (icl_ua < 0) { |
| 1035 | smblib_dbg(chg, PR_MISC, "No Voter hence suspending\n"); |
| 1036 | icl_ua = 0; |
| 1037 | } |
| 1038 | |
Harry Yang | 379484a | 2017-08-29 10:26:14 -0700 | [diff] [blame] | 1039 | suspend = (icl_ua <= USBIN_25MA); |
Abhijeet Dharmapurikar | c0ff65a | 2016-06-06 16:45:34 -0700 | [diff] [blame] | 1040 | if (suspend) |
| 1041 | goto suspend; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1042 | |
| 1043 | rc = smblib_set_charge_param(chg, &chg->param.dc_icl, icl_ua); |
Abhijeet Dharmapurikar | c0ff65a | 2016-06-06 16:45:34 -0700 | [diff] [blame] | 1044 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 1045 | smblib_err(chg, "Couldn't set DC input current limit rc=%d\n", |
Abhijeet Dharmapurikar | c0ff65a | 2016-06-06 16:45:34 -0700 | [diff] [blame] | 1046 | rc); |
| 1047 | return rc; |
| 1048 | } |
| 1049 | |
| 1050 | suspend: |
| 1051 | rc = vote(chg->dc_suspend_votable, USER_VOTER, suspend, 0); |
| 1052 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 1053 | smblib_err(chg, "Couldn't vote to %s DC rc=%d\n", |
Abhijeet Dharmapurikar | c0ff65a | 2016-06-06 16:45:34 -0700 | [diff] [blame] | 1054 | suspend ? "suspend" : "resume", rc); |
| 1055 | return rc; |
| 1056 | } |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1057 | return rc; |
| 1058 | } |
| 1059 | |
Abhijeet Dharmapurikar | 4b13c6e | 2016-10-05 15:15:10 -0700 | [diff] [blame] | 1060 | static int smblib_pd_disallowed_votable_indirect_callback( |
| 1061 | struct votable *votable, void *data, int disallowed, const char *client) |
| 1062 | { |
| 1063 | struct smb_charger *chg = data; |
| 1064 | int rc; |
| 1065 | |
| 1066 | rc = vote(chg->pd_allowed_votable, PD_DISALLOWED_INDIRECT_VOTER, |
| 1067 | !disallowed, 0); |
| 1068 | |
| 1069 | return rc; |
| 1070 | } |
| 1071 | |
Harry Yang | 223c628 | 2016-06-14 15:48:36 -0700 | [diff] [blame] | 1072 | static int smblib_awake_vote_callback(struct votable *votable, void *data, |
| 1073 | int awake, const char *client) |
| 1074 | { |
| 1075 | struct smb_charger *chg = data; |
| 1076 | |
| 1077 | if (awake) |
| 1078 | pm_stay_awake(chg->dev); |
| 1079 | else |
| 1080 | pm_relax(chg->dev); |
| 1081 | |
| 1082 | return 0; |
| 1083 | } |
| 1084 | |
Abhijeet Dharmapurikar | ee54de0 | 2016-07-08 14:51:47 -0700 | [diff] [blame] | 1085 | static int smblib_chg_disable_vote_callback(struct votable *votable, void *data, |
| 1086 | int chg_disable, const char *client) |
| 1087 | { |
| 1088 | struct smb_charger *chg = data; |
| 1089 | int rc; |
| 1090 | |
| 1091 | rc = smblib_masked_write(chg, CHARGING_ENABLE_CMD_REG, |
| 1092 | CHARGING_ENABLE_CMD_BIT, |
| 1093 | chg_disable ? 0 : CHARGING_ENABLE_CMD_BIT); |
| 1094 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 1095 | smblib_err(chg, "Couldn't %s charging rc=%d\n", |
Abhijeet Dharmapurikar | ee54de0 | 2016-07-08 14:51:47 -0700 | [diff] [blame] | 1096 | chg_disable ? "disable" : "enable", rc); |
| 1097 | return rc; |
| 1098 | } |
| 1099 | |
| 1100 | return 0; |
| 1101 | } |
Harry Yang | aba1f5f | 2016-09-28 10:47:29 -0700 | [diff] [blame] | 1102 | |
Ashay Jaiswal | 4aa2c0c | 2016-12-07 19:39:38 +0530 | [diff] [blame] | 1103 | static int smblib_hvdcp_enable_vote_callback(struct votable *votable, |
Abhijeet Dharmapurikar | f0b0a04 | 2016-10-10 16:43:43 -0700 | [diff] [blame] | 1104 | void *data, |
Ashay Jaiswal | 4aa2c0c | 2016-12-07 19:39:38 +0530 | [diff] [blame] | 1105 | int hvdcp_enable, const char *client) |
Abhijeet Dharmapurikar | f0b0a04 | 2016-10-10 16:43:43 -0700 | [diff] [blame] | 1106 | { |
| 1107 | struct smb_charger *chg = data; |
| 1108 | int rc; |
Ashay Jaiswal | 6d308da | 2017-02-18 09:59:23 +0530 | [diff] [blame] | 1109 | u8 val = HVDCP_AUTH_ALG_EN_CFG_BIT | HVDCP_EN_BIT; |
Abhijeet Dharmapurikar | 3c93db3 | 2017-04-17 17:36:14 -0700 | [diff] [blame] | 1110 | u8 stat; |
Ashay Jaiswal | 6d308da | 2017-02-18 09:59:23 +0530 | [diff] [blame] | 1111 | |
| 1112 | /* vote to enable/disable HW autonomous INOV */ |
| 1113 | vote(chg->hvdcp_hw_inov_dis_votable, client, !hvdcp_enable, 0); |
Abhijeet Dharmapurikar | f0b0a04 | 2016-10-10 16:43:43 -0700 | [diff] [blame] | 1114 | |
| 1115 | /* |
| 1116 | * Disable the autonomous bit and auth bit for disabling hvdcp. |
| 1117 | * This ensures only qc 2.0 detection runs but no vbus |
| 1118 | * negotiation happens. |
| 1119 | */ |
Ashay Jaiswal | 4aa2c0c | 2016-12-07 19:39:38 +0530 | [diff] [blame] | 1120 | if (!hvdcp_enable) |
Abhijeet Dharmapurikar | f0b0a04 | 2016-10-10 16:43:43 -0700 | [diff] [blame] | 1121 | val = HVDCP_EN_BIT; |
| 1122 | |
| 1123 | rc = smblib_masked_write(chg, USBIN_OPTIONS_1_CFG_REG, |
Ashay Jaiswal | 6d308da | 2017-02-18 09:59:23 +0530 | [diff] [blame] | 1124 | HVDCP_EN_BIT | HVDCP_AUTH_ALG_EN_CFG_BIT, |
Abhijeet Dharmapurikar | f0b0a04 | 2016-10-10 16:43:43 -0700 | [diff] [blame] | 1125 | val); |
| 1126 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 1127 | smblib_err(chg, "Couldn't %s hvdcp rc=%d\n", |
Ashay Jaiswal | 4aa2c0c | 2016-12-07 19:39:38 +0530 | [diff] [blame] | 1128 | hvdcp_enable ? "enable" : "disable", rc); |
Abhijeet Dharmapurikar | f0b0a04 | 2016-10-10 16:43:43 -0700 | [diff] [blame] | 1129 | return rc; |
| 1130 | } |
| 1131 | |
Abhijeet Dharmapurikar | 3c93db3 | 2017-04-17 17:36:14 -0700 | [diff] [blame] | 1132 | rc = smblib_read(chg, APSD_STATUS_REG, &stat); |
| 1133 | if (rc < 0) { |
| 1134 | smblib_err(chg, "Couldn't read APSD status rc=%d\n", rc); |
| 1135 | return rc; |
| 1136 | } |
| 1137 | |
| 1138 | /* re-run APSD if HVDCP was detected */ |
| 1139 | if (stat & QC_CHARGER_BIT) |
| 1140 | smblib_rerun_apsd(chg); |
| 1141 | |
Abhijeet Dharmapurikar | f0b0a04 | 2016-10-10 16:43:43 -0700 | [diff] [blame] | 1142 | return 0; |
| 1143 | } |
| 1144 | |
Ashay Jaiswal | 4aa2c0c | 2016-12-07 19:39:38 +0530 | [diff] [blame] | 1145 | static int smblib_hvdcp_disable_indirect_vote_callback(struct votable *votable, |
| 1146 | void *data, int hvdcp_disable, const char *client) |
| 1147 | { |
| 1148 | struct smb_charger *chg = data; |
| 1149 | |
| 1150 | vote(chg->hvdcp_enable_votable, HVDCP_INDIRECT_VOTER, |
| 1151 | !hvdcp_disable, 0); |
| 1152 | |
| 1153 | return 0; |
| 1154 | } |
| 1155 | |
Abhijeet Dharmapurikar | f8a7a4a | 2016-10-07 18:46:45 -0700 | [diff] [blame] | 1156 | static int smblib_apsd_disable_vote_callback(struct votable *votable, |
| 1157 | void *data, |
| 1158 | int apsd_disable, const char *client) |
| 1159 | { |
| 1160 | struct smb_charger *chg = data; |
| 1161 | int rc; |
| 1162 | |
Nicholas Troast | ec4703c | 2017-01-30 14:52:33 -0800 | [diff] [blame] | 1163 | if (apsd_disable) { |
Nicholas Troast | ec4703c | 2017-01-30 14:52:33 -0800 | [diff] [blame] | 1164 | rc = smblib_masked_write(chg, USBIN_OPTIONS_1_CFG_REG, |
| 1165 | AUTO_SRC_DETECT_BIT, |
| 1166 | 0); |
| 1167 | if (rc < 0) { |
| 1168 | smblib_err(chg, "Couldn't disable APSD rc=%d\n", rc); |
| 1169 | return rc; |
| 1170 | } |
| 1171 | } else { |
| 1172 | rc = smblib_masked_write(chg, USBIN_OPTIONS_1_CFG_REG, |
| 1173 | AUTO_SRC_DETECT_BIT, |
| 1174 | AUTO_SRC_DETECT_BIT); |
| 1175 | if (rc < 0) { |
| 1176 | smblib_err(chg, "Couldn't enable APSD rc=%d\n", rc); |
| 1177 | return rc; |
| 1178 | } |
Abhijeet Dharmapurikar | f8a7a4a | 2016-10-07 18:46:45 -0700 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | return 0; |
| 1182 | } |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 1183 | |
Ashay Jaiswal | 6d308da | 2017-02-18 09:59:23 +0530 | [diff] [blame] | 1184 | static int smblib_hvdcp_hw_inov_dis_vote_callback(struct votable *votable, |
| 1185 | void *data, int disable, const char *client) |
| 1186 | { |
| 1187 | struct smb_charger *chg = data; |
| 1188 | int rc; |
| 1189 | |
| 1190 | if (disable) { |
| 1191 | /* |
| 1192 | * the pulse count register get zeroed when autonomous mode is |
| 1193 | * disabled. Track that in variables before disabling |
| 1194 | */ |
Ashay Jaiswal | cda0d1c | 2017-05-15 17:15:29 +0530 | [diff] [blame] | 1195 | rc = smblib_get_hw_pulse_cnt(chg, &chg->pulse_cnt); |
Ashay Jaiswal | 6d308da | 2017-02-18 09:59:23 +0530 | [diff] [blame] | 1196 | if (rc < 0) { |
| 1197 | pr_err("failed to read QC_PULSE_COUNT_STATUS_REG rc=%d\n", |
| 1198 | rc); |
| 1199 | return rc; |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | rc = smblib_masked_write(chg, USBIN_OPTIONS_1_CFG_REG, |
| 1204 | HVDCP_AUTONOMOUS_MODE_EN_CFG_BIT, |
| 1205 | disable ? 0 : HVDCP_AUTONOMOUS_MODE_EN_CFG_BIT); |
| 1206 | if (rc < 0) { |
| 1207 | smblib_err(chg, "Couldn't %s hvdcp rc=%d\n", |
| 1208 | disable ? "disable" : "enable", rc); |
| 1209 | return rc; |
| 1210 | } |
| 1211 | |
| 1212 | return rc; |
| 1213 | } |
| 1214 | |
Harry Yang | 4bf7d96 | 2017-03-13 16:51:43 -0700 | [diff] [blame] | 1215 | static int smblib_usb_irq_enable_vote_callback(struct votable *votable, |
| 1216 | void *data, int enable, const char *client) |
| 1217 | { |
| 1218 | struct smb_charger *chg = data; |
| 1219 | |
| 1220 | if (!chg->irq_info[INPUT_CURRENT_LIMIT_IRQ].irq || |
| 1221 | !chg->irq_info[HIGH_DUTY_CYCLE_IRQ].irq) |
| 1222 | return 0; |
| 1223 | |
| 1224 | if (enable) { |
| 1225 | enable_irq(chg->irq_info[INPUT_CURRENT_LIMIT_IRQ].irq); |
| 1226 | enable_irq(chg->irq_info[HIGH_DUTY_CYCLE_IRQ].irq); |
| 1227 | } else { |
| 1228 | disable_irq(chg->irq_info[INPUT_CURRENT_LIMIT_IRQ].irq); |
| 1229 | disable_irq(chg->irq_info[HIGH_DUTY_CYCLE_IRQ].irq); |
| 1230 | } |
| 1231 | |
| 1232 | return 0; |
| 1233 | } |
| 1234 | |
Abhijeet Dharmapurikar | 3c93db3 | 2017-04-17 17:36:14 -0700 | [diff] [blame] | 1235 | static int smblib_typec_irq_disable_vote_callback(struct votable *votable, |
| 1236 | void *data, int disable, const char *client) |
| 1237 | { |
| 1238 | struct smb_charger *chg = data; |
| 1239 | |
| 1240 | if (!chg->irq_info[TYPE_C_CHANGE_IRQ].irq) |
| 1241 | return 0; |
| 1242 | |
| 1243 | if (disable) |
| 1244 | disable_irq_nosync(chg->irq_info[TYPE_C_CHANGE_IRQ].irq); |
| 1245 | else |
| 1246 | enable_irq(chg->irq_info[TYPE_C_CHANGE_IRQ].irq); |
| 1247 | |
| 1248 | return 0; |
| 1249 | } |
| 1250 | |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 1251 | /******************* |
| 1252 | * VCONN REGULATOR * |
| 1253 | * *****************/ |
| 1254 | |
Nicholas Troast | b11015f | 2017-01-17 17:56:45 -0800 | [diff] [blame] | 1255 | #define MAX_OTG_SS_TRIES 2 |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 1256 | static int _smblib_vconn_regulator_enable(struct regulator_dev *rdev) |
| 1257 | { |
| 1258 | struct smb_charger *chg = rdev_get_drvdata(rdev); |
Nicholas Troast | 85d3a5a | 2017-05-03 10:19:43 -0700 | [diff] [blame] | 1259 | int rc = 0; |
| 1260 | u8 val; |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 1261 | |
| 1262 | /* |
Nicholas Troast | 85d3a5a | 2017-05-03 10:19:43 -0700 | [diff] [blame] | 1263 | * When enabling VCONN using the command register the CC pin must be |
| 1264 | * selected. VCONN should be supplied to the inactive CC pin hence using |
| 1265 | * the opposite of the CC_ORIENTATION_BIT. |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 1266 | */ |
Nicholas Troast | b11015f | 2017-01-17 17:56:45 -0800 | [diff] [blame] | 1267 | smblib_dbg(chg, PR_OTG, "enabling VCONN\n"); |
Abhijeet Dharmapurikar | b0403c4 | 2017-04-17 12:26:26 -0700 | [diff] [blame] | 1268 | val = chg->typec_status[3] & |
| 1269 | CC_ORIENTATION_BIT ? 0 : VCONN_EN_ORIENTATION_BIT; |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 1270 | rc = smblib_masked_write(chg, TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG, |
| 1271 | VCONN_EN_VALUE_BIT | VCONN_EN_ORIENTATION_BIT, |
Abhijeet Dharmapurikar | b0403c4 | 2017-04-17 12:26:26 -0700 | [diff] [blame] | 1272 | VCONN_EN_VALUE_BIT | val); |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 1273 | if (rc < 0) { |
| 1274 | smblib_err(chg, "Couldn't enable vconn setting rc=%d\n", rc); |
| 1275 | return rc; |
| 1276 | } |
| 1277 | |
| 1278 | return rc; |
| 1279 | } |
| 1280 | |
| 1281 | int smblib_vconn_regulator_enable(struct regulator_dev *rdev) |
| 1282 | { |
| 1283 | struct smb_charger *chg = rdev_get_drvdata(rdev); |
| 1284 | int rc = 0; |
| 1285 | |
Nicholas Troast | 85d3a5a | 2017-05-03 10:19:43 -0700 | [diff] [blame] | 1286 | mutex_lock(&chg->vconn_oc_lock); |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 1287 | if (chg->vconn_en) |
| 1288 | goto unlock; |
| 1289 | |
| 1290 | rc = _smblib_vconn_regulator_enable(rdev); |
| 1291 | if (rc >= 0) |
| 1292 | chg->vconn_en = true; |
| 1293 | |
| 1294 | unlock: |
Nicholas Troast | 85d3a5a | 2017-05-03 10:19:43 -0700 | [diff] [blame] | 1295 | mutex_unlock(&chg->vconn_oc_lock); |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 1296 | return rc; |
| 1297 | } |
| 1298 | |
| 1299 | static int _smblib_vconn_regulator_disable(struct regulator_dev *rdev) |
| 1300 | { |
| 1301 | struct smb_charger *chg = rdev_get_drvdata(rdev); |
| 1302 | int rc = 0; |
| 1303 | |
Nicholas Troast | b11015f | 2017-01-17 17:56:45 -0800 | [diff] [blame] | 1304 | smblib_dbg(chg, PR_OTG, "disabling VCONN\n"); |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 1305 | rc = smblib_masked_write(chg, TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG, |
| 1306 | VCONN_EN_VALUE_BIT, 0); |
| 1307 | if (rc < 0) |
| 1308 | smblib_err(chg, "Couldn't disable vconn regulator rc=%d\n", rc); |
| 1309 | |
| 1310 | return rc; |
| 1311 | } |
| 1312 | |
| 1313 | int smblib_vconn_regulator_disable(struct regulator_dev *rdev) |
| 1314 | { |
| 1315 | struct smb_charger *chg = rdev_get_drvdata(rdev); |
| 1316 | int rc = 0; |
| 1317 | |
Nicholas Troast | 85d3a5a | 2017-05-03 10:19:43 -0700 | [diff] [blame] | 1318 | mutex_lock(&chg->vconn_oc_lock); |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 1319 | if (!chg->vconn_en) |
| 1320 | goto unlock; |
| 1321 | |
| 1322 | rc = _smblib_vconn_regulator_disable(rdev); |
| 1323 | if (rc >= 0) |
| 1324 | chg->vconn_en = false; |
| 1325 | |
| 1326 | unlock: |
Nicholas Troast | 85d3a5a | 2017-05-03 10:19:43 -0700 | [diff] [blame] | 1327 | mutex_unlock(&chg->vconn_oc_lock); |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 1328 | return rc; |
| 1329 | } |
| 1330 | |
| 1331 | int smblib_vconn_regulator_is_enabled(struct regulator_dev *rdev) |
| 1332 | { |
| 1333 | struct smb_charger *chg = rdev_get_drvdata(rdev); |
| 1334 | int ret; |
| 1335 | |
Nicholas Troast | 85d3a5a | 2017-05-03 10:19:43 -0700 | [diff] [blame] | 1336 | mutex_lock(&chg->vconn_oc_lock); |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 1337 | ret = chg->vconn_en; |
Nicholas Troast | 85d3a5a | 2017-05-03 10:19:43 -0700 | [diff] [blame] | 1338 | mutex_unlock(&chg->vconn_oc_lock); |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 1339 | return ret; |
| 1340 | } |
| 1341 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1342 | /***************** |
| 1343 | * OTG REGULATOR * |
| 1344 | *****************/ |
Ashay Jaiswal | 7c24138 | 2017-03-06 15:26:38 +0530 | [diff] [blame] | 1345 | #define MAX_RETRY 15 |
| 1346 | #define MIN_DELAY_US 2000 |
| 1347 | #define MAX_DELAY_US 9000 |
Anirudh Ghayal | adf20da | 2017-07-03 10:52:29 +0530 | [diff] [blame] | 1348 | static int otg_current[] = {250000, 500000, 1000000, 1500000}; |
| 1349 | static int smblib_enable_otg_wa(struct smb_charger *chg) |
| 1350 | { |
| 1351 | u8 stat; |
| 1352 | int rc, i, retry_count = 0, min_delay = MIN_DELAY_US; |
| 1353 | |
| 1354 | for (i = 0; i < ARRAY_SIZE(otg_current); i++) { |
| 1355 | smblib_dbg(chg, PR_OTG, "enabling OTG with %duA\n", |
| 1356 | otg_current[i]); |
| 1357 | rc = smblib_set_charge_param(chg, &chg->param.otg_cl, |
| 1358 | otg_current[i]); |
| 1359 | if (rc < 0) { |
| 1360 | smblib_err(chg, "Couldn't set otg limit rc=%d\n", rc); |
| 1361 | return rc; |
| 1362 | } |
| 1363 | |
| 1364 | rc = smblib_write(chg, CMD_OTG_REG, OTG_EN_BIT); |
| 1365 | if (rc < 0) { |
| 1366 | smblib_err(chg, "Couldn't enable OTG rc=%d\n", rc); |
| 1367 | return rc; |
| 1368 | } |
| 1369 | |
| 1370 | retry_count = 0; |
| 1371 | min_delay = MIN_DELAY_US; |
| 1372 | do { |
| 1373 | usleep_range(min_delay, min_delay + 100); |
| 1374 | rc = smblib_read(chg, OTG_STATUS_REG, &stat); |
| 1375 | if (rc < 0) { |
| 1376 | smblib_err(chg, "Couldn't read OTG status rc=%d\n", |
| 1377 | rc); |
| 1378 | goto out; |
| 1379 | } |
| 1380 | |
| 1381 | if (stat & BOOST_SOFTSTART_DONE_BIT) { |
| 1382 | rc = smblib_set_charge_param(chg, |
| 1383 | &chg->param.otg_cl, chg->otg_cl_ua); |
| 1384 | if (rc < 0) { |
| 1385 | smblib_err(chg, "Couldn't set otg limit rc=%d\n", |
| 1386 | rc); |
| 1387 | goto out; |
| 1388 | } |
| 1389 | break; |
| 1390 | } |
| 1391 | /* increase the delay for following iterations */ |
| 1392 | if (retry_count > 5) |
| 1393 | min_delay = MAX_DELAY_US; |
| 1394 | |
| 1395 | } while (retry_count++ < MAX_RETRY); |
| 1396 | |
| 1397 | if (retry_count >= MAX_RETRY) { |
| 1398 | smblib_dbg(chg, PR_OTG, "OTG enable failed with %duA\n", |
| 1399 | otg_current[i]); |
| 1400 | rc = smblib_write(chg, CMD_OTG_REG, 0); |
| 1401 | if (rc < 0) { |
| 1402 | smblib_err(chg, "disable OTG rc=%d\n", rc); |
| 1403 | goto out; |
| 1404 | } |
| 1405 | } else { |
| 1406 | smblib_dbg(chg, PR_OTG, "OTG enabled\n"); |
| 1407 | return 0; |
| 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | if (i == ARRAY_SIZE(otg_current)) { |
| 1412 | rc = -EINVAL; |
| 1413 | goto out; |
| 1414 | } |
| 1415 | |
| 1416 | return 0; |
| 1417 | out: |
| 1418 | smblib_write(chg, CMD_OTG_REG, 0); |
| 1419 | return rc; |
| 1420 | } |
| 1421 | |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 1422 | static int _smblib_vbus_regulator_enable(struct regulator_dev *rdev) |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1423 | { |
| 1424 | struct smb_charger *chg = rdev_get_drvdata(rdev); |
Anirudh Ghayal | adf20da | 2017-07-03 10:52:29 +0530 | [diff] [blame] | 1425 | int rc; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1426 | |
Nicholas Troast | b11015f | 2017-01-17 17:56:45 -0800 | [diff] [blame] | 1427 | smblib_dbg(chg, PR_OTG, "halt 1 in 8 mode\n"); |
Harry Yang | 360bd53 | 2016-09-26 11:03:22 -0700 | [diff] [blame] | 1428 | rc = smblib_masked_write(chg, OTG_ENG_OTG_CFG_REG, |
| 1429 | ENG_BUCKBOOST_HALT1_8_MODE_BIT, |
| 1430 | ENG_BUCKBOOST_HALT1_8_MODE_BIT); |
| 1431 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 1432 | smblib_err(chg, "Couldn't set OTG_ENG_OTG_CFG_REG rc=%d\n", |
Harry Yang | 360bd53 | 2016-09-26 11:03:22 -0700 | [diff] [blame] | 1433 | rc); |
| 1434 | return rc; |
| 1435 | } |
| 1436 | |
Nicholas Troast | b11015f | 2017-01-17 17:56:45 -0800 | [diff] [blame] | 1437 | smblib_dbg(chg, PR_OTG, "enabling OTG\n"); |
Harry Yang | 360bd53 | 2016-09-26 11:03:22 -0700 | [diff] [blame] | 1438 | |
Ashay Jaiswal | 7c24138 | 2017-03-06 15:26:38 +0530 | [diff] [blame] | 1439 | if (chg->wa_flags & OTG_WA) { |
Anirudh Ghayal | adf20da | 2017-07-03 10:52:29 +0530 | [diff] [blame] | 1440 | rc = smblib_enable_otg_wa(chg); |
| 1441 | if (rc < 0) |
| 1442 | smblib_err(chg, "Couldn't enable OTG rc=%d\n", rc); |
| 1443 | } else { |
| 1444 | rc = smblib_write(chg, CMD_OTG_REG, OTG_EN_BIT); |
| 1445 | if (rc < 0) |
| 1446 | smblib_err(chg, "Couldn't enable OTG rc=%d\n", rc); |
Ashay Jaiswal | 7c24138 | 2017-03-06 15:26:38 +0530 | [diff] [blame] | 1447 | } |
| 1448 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1449 | return rc; |
| 1450 | } |
| 1451 | |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 1452 | int smblib_vbus_regulator_enable(struct regulator_dev *rdev) |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1453 | { |
| 1454 | struct smb_charger *chg = rdev_get_drvdata(rdev); |
| 1455 | int rc = 0; |
| 1456 | |
Nicholas Troast | b11015f | 2017-01-17 17:56:45 -0800 | [diff] [blame] | 1457 | mutex_lock(&chg->otg_oc_lock); |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 1458 | if (chg->otg_en) |
| 1459 | goto unlock; |
| 1460 | |
Harry Yang | a2fb0e3 | 2017-03-22 22:45:25 -0700 | [diff] [blame] | 1461 | if (!chg->usb_icl_votable) { |
| 1462 | chg->usb_icl_votable = find_votable("USB_ICL"); |
| 1463 | |
| 1464 | if (!chg->usb_icl_votable) |
| 1465 | return -EINVAL; |
| 1466 | } |
| 1467 | vote(chg->usb_icl_votable, USBIN_USBIN_BOOST_VOTER, true, 0); |
| 1468 | |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 1469 | rc = _smblib_vbus_regulator_enable(rdev); |
| 1470 | if (rc >= 0) |
| 1471 | chg->otg_en = true; |
| 1472 | |
| 1473 | unlock: |
Nicholas Troast | b11015f | 2017-01-17 17:56:45 -0800 | [diff] [blame] | 1474 | mutex_unlock(&chg->otg_oc_lock); |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 1475 | return rc; |
| 1476 | } |
| 1477 | |
| 1478 | static int _smblib_vbus_regulator_disable(struct regulator_dev *rdev) |
| 1479 | { |
| 1480 | struct smb_charger *chg = rdev_get_drvdata(rdev); |
| 1481 | int rc; |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 1482 | |
Ashay Jaiswal | 7c24138 | 2017-03-06 15:26:38 +0530 | [diff] [blame] | 1483 | if (chg->wa_flags & OTG_WA) { |
| 1484 | /* set OTG current limit to minimum value */ |
| 1485 | rc = smblib_set_charge_param(chg, &chg->param.otg_cl, |
| 1486 | chg->param.otg_cl.min_u); |
| 1487 | if (rc < 0) { |
| 1488 | smblib_err(chg, |
| 1489 | "Couldn't set otg current limit rc=%d\n", rc); |
| 1490 | return rc; |
| 1491 | } |
| 1492 | } |
| 1493 | |
Nicholas Troast | b11015f | 2017-01-17 17:56:45 -0800 | [diff] [blame] | 1494 | smblib_dbg(chg, PR_OTG, "disabling OTG\n"); |
Harry Yang | 360bd53 | 2016-09-26 11:03:22 -0700 | [diff] [blame] | 1495 | rc = smblib_write(chg, CMD_OTG_REG, 0); |
| 1496 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 1497 | smblib_err(chg, "Couldn't disable OTG regulator rc=%d\n", rc); |
Harry Yang | 360bd53 | 2016-09-26 11:03:22 -0700 | [diff] [blame] | 1498 | return rc; |
| 1499 | } |
| 1500 | |
Nicholas Troast | b11015f | 2017-01-17 17:56:45 -0800 | [diff] [blame] | 1501 | smblib_dbg(chg, PR_OTG, "start 1 in 8 mode\n"); |
Harry Yang | 360bd53 | 2016-09-26 11:03:22 -0700 | [diff] [blame] | 1502 | rc = smblib_masked_write(chg, OTG_ENG_OTG_CFG_REG, |
| 1503 | ENG_BUCKBOOST_HALT1_8_MODE_BIT, 0); |
| 1504 | if (rc < 0) { |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 1505 | smblib_err(chg, "Couldn't set OTG_ENG_OTG_CFG_REG rc=%d\n", rc); |
Harry Yang | 360bd53 | 2016-09-26 11:03:22 -0700 | [diff] [blame] | 1506 | return rc; |
| 1507 | } |
| 1508 | |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 1509 | return 0; |
| 1510 | } |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1511 | |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 1512 | int smblib_vbus_regulator_disable(struct regulator_dev *rdev) |
| 1513 | { |
| 1514 | struct smb_charger *chg = rdev_get_drvdata(rdev); |
| 1515 | int rc = 0; |
| 1516 | |
Nicholas Troast | b11015f | 2017-01-17 17:56:45 -0800 | [diff] [blame] | 1517 | mutex_lock(&chg->otg_oc_lock); |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 1518 | if (!chg->otg_en) |
| 1519 | goto unlock; |
| 1520 | |
| 1521 | rc = _smblib_vbus_regulator_disable(rdev); |
| 1522 | if (rc >= 0) |
| 1523 | chg->otg_en = false; |
| 1524 | |
Harry Yang | a2fb0e3 | 2017-03-22 22:45:25 -0700 | [diff] [blame] | 1525 | if (chg->usb_icl_votable) |
| 1526 | vote(chg->usb_icl_votable, USBIN_USBIN_BOOST_VOTER, false, 0); |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 1527 | unlock: |
Nicholas Troast | b11015f | 2017-01-17 17:56:45 -0800 | [diff] [blame] | 1528 | mutex_unlock(&chg->otg_oc_lock); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1529 | return rc; |
| 1530 | } |
| 1531 | |
| 1532 | int smblib_vbus_regulator_is_enabled(struct regulator_dev *rdev) |
| 1533 | { |
| 1534 | struct smb_charger *chg = rdev_get_drvdata(rdev); |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 1535 | int ret; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1536 | |
Nicholas Troast | b11015f | 2017-01-17 17:56:45 -0800 | [diff] [blame] | 1537 | mutex_lock(&chg->otg_oc_lock); |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 1538 | ret = chg->otg_en; |
Nicholas Troast | b11015f | 2017-01-17 17:56:45 -0800 | [diff] [blame] | 1539 | mutex_unlock(&chg->otg_oc_lock); |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 1540 | return ret; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | /******************** |
| 1544 | * BATT PSY GETTERS * |
| 1545 | ********************/ |
| 1546 | |
| 1547 | int smblib_get_prop_input_suspend(struct smb_charger *chg, |
| 1548 | union power_supply_propval *val) |
| 1549 | { |
Abhijeet Dharmapurikar | 5e0bfbe | 2017-02-12 19:16:15 -0800 | [diff] [blame] | 1550 | val->intval |
| 1551 | = (get_client_vote(chg->usb_icl_votable, USER_VOTER) == 0) |
| 1552 | && get_client_vote(chg->dc_suspend_votable, USER_VOTER); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1553 | return 0; |
| 1554 | } |
| 1555 | |
| 1556 | int smblib_get_prop_batt_present(struct smb_charger *chg, |
| 1557 | union power_supply_propval *val) |
| 1558 | { |
| 1559 | int rc; |
| 1560 | u8 stat; |
| 1561 | |
| 1562 | rc = smblib_read(chg, BATIF_BASE + INT_RT_STS_OFFSET, &stat); |
| 1563 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 1564 | smblib_err(chg, "Couldn't read BATIF_INT_RT_STS rc=%d\n", rc); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1565 | return rc; |
| 1566 | } |
| 1567 | |
| 1568 | val->intval = !(stat & (BAT_THERM_OR_ID_MISSING_RT_STS_BIT |
| 1569 | | BAT_TERMINAL_MISSING_RT_STS_BIT)); |
| 1570 | |
| 1571 | return rc; |
| 1572 | } |
| 1573 | |
| 1574 | int smblib_get_prop_batt_capacity(struct smb_charger *chg, |
| 1575 | union power_supply_propval *val) |
| 1576 | { |
Harry Yang | 5e1a522 | 2016-07-26 15:16:04 -0700 | [diff] [blame] | 1577 | int rc = -EINVAL; |
| 1578 | |
Abhijeet Dharmapurikar | 9e7e48c | 2016-08-23 12:55:49 -0700 | [diff] [blame] | 1579 | if (chg->fake_capacity >= 0) { |
| 1580 | val->intval = chg->fake_capacity; |
| 1581 | return 0; |
| 1582 | } |
| 1583 | |
Harry Yang | 5e1a522 | 2016-07-26 15:16:04 -0700 | [diff] [blame] | 1584 | if (chg->bms_psy) |
| 1585 | rc = power_supply_get_property(chg->bms_psy, |
| 1586 | POWER_SUPPLY_PROP_CAPACITY, val); |
| 1587 | return rc; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | int smblib_get_prop_batt_status(struct smb_charger *chg, |
| 1591 | union power_supply_propval *val) |
| 1592 | { |
Abhijeet Dharmapurikar | ec43bb4 | 2017-01-17 20:00:08 -0800 | [diff] [blame] | 1593 | union power_supply_propval pval = {0, }; |
Abhijeet Dharmapurikar | d97b2bf | 2017-07-06 17:03:00 -0700 | [diff] [blame] | 1594 | bool usb_online, dc_online, qnovo_en; |
| 1595 | u8 stat, pt_en_cmd; |
Nicholas Troast | 8cb7755 | 2016-09-23 11:50:18 -0700 | [diff] [blame] | 1596 | int rc; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1597 | |
Abhijeet Dharmapurikar | ec43bb4 | 2017-01-17 20:00:08 -0800 | [diff] [blame] | 1598 | rc = smblib_get_prop_usb_online(chg, &pval); |
| 1599 | if (rc < 0) { |
| 1600 | smblib_err(chg, "Couldn't get usb online property rc=%d\n", |
| 1601 | rc); |
| 1602 | return rc; |
| 1603 | } |
| 1604 | usb_online = (bool)pval.intval; |
| 1605 | |
| 1606 | rc = smblib_get_prop_dc_online(chg, &pval); |
| 1607 | if (rc < 0) { |
| 1608 | smblib_err(chg, "Couldn't get dc online property rc=%d\n", |
| 1609 | rc); |
| 1610 | return rc; |
| 1611 | } |
| 1612 | dc_online = (bool)pval.intval; |
| 1613 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1614 | rc = smblib_read(chg, BATTERY_CHARGER_STATUS_1_REG, &stat); |
| 1615 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 1616 | smblib_err(chg, "Couldn't read BATTERY_CHARGER_STATUS_1 rc=%d\n", |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1617 | rc); |
| 1618 | return rc; |
| 1619 | } |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1620 | stat = stat & BATTERY_CHARGER_STATUS_MASK; |
Abhijeet Dharmapurikar | ec43bb4 | 2017-01-17 20:00:08 -0800 | [diff] [blame] | 1621 | |
| 1622 | if (!usb_online && !dc_online) { |
| 1623 | switch (stat) { |
| 1624 | case TERMINATE_CHARGE: |
| 1625 | case INHIBIT_CHARGE: |
| 1626 | val->intval = POWER_SUPPLY_STATUS_FULL; |
| 1627 | break; |
| 1628 | default: |
| 1629 | val->intval = POWER_SUPPLY_STATUS_DISCHARGING; |
| 1630 | break; |
| 1631 | } |
| 1632 | return rc; |
| 1633 | } |
| 1634 | |
Nicholas Troast | 8cb7755 | 2016-09-23 11:50:18 -0700 | [diff] [blame] | 1635 | switch (stat) { |
| 1636 | case TRICKLE_CHARGE: |
| 1637 | case PRE_CHARGE: |
| 1638 | case FAST_CHARGE: |
| 1639 | case FULLON_CHARGE: |
| 1640 | case TAPER_CHARGE: |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1641 | val->intval = POWER_SUPPLY_STATUS_CHARGING; |
Nicholas Troast | 8cb7755 | 2016-09-23 11:50:18 -0700 | [diff] [blame] | 1642 | break; |
| 1643 | case TERMINATE_CHARGE: |
| 1644 | case INHIBIT_CHARGE: |
| 1645 | val->intval = POWER_SUPPLY_STATUS_FULL; |
| 1646 | break; |
| 1647 | case DISABLE_CHARGE: |
| 1648 | val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 1649 | break; |
| 1650 | default: |
| 1651 | val->intval = POWER_SUPPLY_STATUS_UNKNOWN; |
| 1652 | break; |
| 1653 | } |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1654 | |
Harry Yang | 7ecc1a1 | 2017-04-06 12:24:56 -0700 | [diff] [blame] | 1655 | if (val->intval != POWER_SUPPLY_STATUS_CHARGING) |
| 1656 | return 0; |
| 1657 | |
Harry Yang | 589dd42 | 2017-07-28 18:41:42 -0700 | [diff] [blame] | 1658 | if (!usb_online && dc_online |
| 1659 | && chg->fake_batt_status == POWER_SUPPLY_STATUS_FULL) { |
| 1660 | val->intval = POWER_SUPPLY_STATUS_FULL; |
| 1661 | return 0; |
| 1662 | } |
| 1663 | |
Harry Yang | c3c28d1 | 2017-04-17 16:41:19 -0700 | [diff] [blame] | 1664 | rc = smblib_read(chg, BATTERY_CHARGER_STATUS_7_REG, &stat); |
Harry Yang | 7ecc1a1 | 2017-04-06 12:24:56 -0700 | [diff] [blame] | 1665 | if (rc < 0) { |
| 1666 | smblib_err(chg, "Couldn't read BATTERY_CHARGER_STATUS_2 rc=%d\n", |
| 1667 | rc); |
| 1668 | return rc; |
Abhijeet Dharmapurikar | d97b2bf | 2017-07-06 17:03:00 -0700 | [diff] [blame] | 1669 | } |
Harry Yang | 7ecc1a1 | 2017-04-06 12:24:56 -0700 | [diff] [blame] | 1670 | |
Harry Yang | c3c28d1 | 2017-04-17 16:41:19 -0700 | [diff] [blame] | 1671 | stat &= ENABLE_TRICKLE_BIT | ENABLE_PRE_CHARGING_BIT | |
| 1672 | ENABLE_FAST_CHARGING_BIT | ENABLE_FULLON_MODE_BIT; |
Abhijeet Dharmapurikar | d97b2bf | 2017-07-06 17:03:00 -0700 | [diff] [blame] | 1673 | |
| 1674 | rc = smblib_read(chg, QNOVO_PT_ENABLE_CMD_REG, &pt_en_cmd); |
| 1675 | if (rc < 0) { |
| 1676 | smblib_err(chg, "Couldn't read QNOVO_PT_ENABLE_CMD_REG rc=%d\n", |
| 1677 | rc); |
| 1678 | return rc; |
| 1679 | } |
| 1680 | |
| 1681 | qnovo_en = (bool)(pt_en_cmd & QNOVO_PT_ENABLE_CMD_BIT); |
| 1682 | |
| 1683 | /* ignore stat7 when qnovo is enabled */ |
| 1684 | if (!qnovo_en && !stat) |
Harry Yang | 7ecc1a1 | 2017-04-06 12:24:56 -0700 | [diff] [blame] | 1685 | val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 1686 | |
Nicholas Troast | 8cb7755 | 2016-09-23 11:50:18 -0700 | [diff] [blame] | 1687 | return 0; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1688 | } |
| 1689 | |
| 1690 | int smblib_get_prop_batt_charge_type(struct smb_charger *chg, |
| 1691 | union power_supply_propval *val) |
| 1692 | { |
| 1693 | int rc; |
| 1694 | u8 stat; |
| 1695 | |
| 1696 | rc = smblib_read(chg, BATTERY_CHARGER_STATUS_1_REG, &stat); |
| 1697 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 1698 | smblib_err(chg, "Couldn't read BATTERY_CHARGER_STATUS_1 rc=%d\n", |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1699 | rc); |
| 1700 | return rc; |
| 1701 | } |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1702 | |
| 1703 | switch (stat & BATTERY_CHARGER_STATUS_MASK) { |
| 1704 | case TRICKLE_CHARGE: |
| 1705 | case PRE_CHARGE: |
| 1706 | val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE; |
| 1707 | break; |
| 1708 | case FAST_CHARGE: |
| 1709 | case FULLON_CHARGE: |
| 1710 | val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST; |
| 1711 | break; |
| 1712 | case TAPER_CHARGE: |
| 1713 | val->intval = POWER_SUPPLY_CHARGE_TYPE_TAPER; |
| 1714 | break; |
| 1715 | default: |
| 1716 | val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE; |
| 1717 | } |
| 1718 | |
| 1719 | return rc; |
| 1720 | } |
| 1721 | |
| 1722 | int smblib_get_prop_batt_health(struct smb_charger *chg, |
| 1723 | union power_supply_propval *val) |
| 1724 | { |
Subbaraman Narayanamurthy | a379c4f | 2016-10-21 17:30:46 -0700 | [diff] [blame] | 1725 | union power_supply_propval pval; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1726 | int rc; |
Abhijeet Dharmapurikar | 33d1846 | 2017-05-15 13:38:53 -0700 | [diff] [blame] | 1727 | int effective_fv_uv; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1728 | u8 stat; |
| 1729 | |
| 1730 | rc = smblib_read(chg, BATTERY_CHARGER_STATUS_2_REG, &stat); |
| 1731 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 1732 | smblib_err(chg, "Couldn't read BATTERY_CHARGER_STATUS_2 rc=%d\n", |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1733 | rc); |
| 1734 | return rc; |
| 1735 | } |
| 1736 | smblib_dbg(chg, PR_REGISTER, "BATTERY_CHARGER_STATUS_2 = 0x%02x\n", |
| 1737 | stat); |
| 1738 | |
| 1739 | if (stat & CHARGER_ERROR_STATUS_BAT_OV_BIT) { |
Subbaraman Narayanamurthy | a379c4f | 2016-10-21 17:30:46 -0700 | [diff] [blame] | 1740 | rc = smblib_get_prop_batt_voltage_now(chg, &pval); |
| 1741 | if (!rc) { |
| 1742 | /* |
| 1743 | * If Vbatt is within 40mV above Vfloat, then don't |
| 1744 | * treat it as overvoltage. |
| 1745 | */ |
Abhijeet Dharmapurikar | 33d1846 | 2017-05-15 13:38:53 -0700 | [diff] [blame] | 1746 | effective_fv_uv = get_effective_result(chg->fv_votable); |
| 1747 | if (pval.intval >= effective_fv_uv + 40000) { |
Subbaraman Narayanamurthy | a379c4f | 2016-10-21 17:30:46 -0700 | [diff] [blame] | 1748 | val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE; |
Abhijeet Dharmapurikar | 33d1846 | 2017-05-15 13:38:53 -0700 | [diff] [blame] | 1749 | smblib_err(chg, "battery over-voltage vbat_fg = %duV, fv = %duV\n", |
| 1750 | pval.intval, effective_fv_uv); |
Subbaraman Narayanamurthy | a379c4f | 2016-10-21 17:30:46 -0700 | [diff] [blame] | 1751 | goto done; |
| 1752 | } |
| 1753 | } |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1754 | } |
| 1755 | |
Harry Yang | 668fc5e | 2016-07-12 16:51:47 -0700 | [diff] [blame] | 1756 | if (stat & BAT_TEMP_STATUS_TOO_COLD_BIT) |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1757 | val->intval = POWER_SUPPLY_HEALTH_COLD; |
Harry Yang | 668fc5e | 2016-07-12 16:51:47 -0700 | [diff] [blame] | 1758 | else if (stat & BAT_TEMP_STATUS_TOO_HOT_BIT) |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1759 | val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; |
Harry Yang | 668fc5e | 2016-07-12 16:51:47 -0700 | [diff] [blame] | 1760 | else if (stat & BAT_TEMP_STATUS_COLD_SOFT_LIMIT_BIT) |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1761 | val->intval = POWER_SUPPLY_HEALTH_COOL; |
Harry Yang | 668fc5e | 2016-07-12 16:51:47 -0700 | [diff] [blame] | 1762 | else if (stat & BAT_TEMP_STATUS_HOT_SOFT_LIMIT_BIT) |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1763 | val->intval = POWER_SUPPLY_HEALTH_WARM; |
Harry Yang | 668fc5e | 2016-07-12 16:51:47 -0700 | [diff] [blame] | 1764 | else |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1765 | val->intval = POWER_SUPPLY_HEALTH_GOOD; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1766 | |
| 1767 | done: |
| 1768 | return rc; |
| 1769 | } |
| 1770 | |
Abhijeet Dharmapurikar | 99fb894 | 2016-07-08 11:39:23 -0700 | [diff] [blame] | 1771 | int smblib_get_prop_system_temp_level(struct smb_charger *chg, |
| 1772 | union power_supply_propval *val) |
| 1773 | { |
| 1774 | val->intval = chg->system_temp_level; |
| 1775 | return 0; |
| 1776 | } |
| 1777 | |
Abhijeet Dharmapurikar | acf3200 | 2017-05-11 11:54:17 -0700 | [diff] [blame] | 1778 | int smblib_get_prop_system_temp_level_max(struct smb_charger *chg, |
| 1779 | union power_supply_propval *val) |
| 1780 | { |
| 1781 | val->intval = chg->thermal_levels; |
| 1782 | return 0; |
| 1783 | } |
| 1784 | |
Abhijeet Dharmapurikar | 0e36900 | 2016-09-07 17:25:36 -0700 | [diff] [blame] | 1785 | int smblib_get_prop_input_current_limited(struct smb_charger *chg, |
| 1786 | union power_supply_propval *val) |
| 1787 | { |
| 1788 | u8 stat; |
| 1789 | int rc; |
| 1790 | |
Abhijeet Dharmapurikar | 2ec2a79 | 2017-05-01 20:00:25 -0700 | [diff] [blame] | 1791 | if (chg->fake_input_current_limited >= 0) { |
| 1792 | val->intval = chg->fake_input_current_limited; |
| 1793 | return 0; |
| 1794 | } |
| 1795 | |
Abhijeet Dharmapurikar | 0e36900 | 2016-09-07 17:25:36 -0700 | [diff] [blame] | 1796 | rc = smblib_read(chg, AICL_STATUS_REG, &stat); |
| 1797 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 1798 | smblib_err(chg, "Couldn't read AICL_STATUS rc=%d\n", rc); |
Abhijeet Dharmapurikar | 0e36900 | 2016-09-07 17:25:36 -0700 | [diff] [blame] | 1799 | return rc; |
| 1800 | } |
| 1801 | val->intval = (stat & SOFT_ILIMIT_BIT) || chg->is_hdc; |
| 1802 | return 0; |
| 1803 | } |
| 1804 | |
Nicholas Troast | 66b21d7 | 2016-09-20 15:33:20 -0700 | [diff] [blame] | 1805 | int smblib_get_prop_batt_voltage_now(struct smb_charger *chg, |
| 1806 | union power_supply_propval *val) |
| 1807 | { |
| 1808 | int rc; |
| 1809 | |
| 1810 | if (!chg->bms_psy) |
| 1811 | return -EINVAL; |
| 1812 | |
| 1813 | rc = power_supply_get_property(chg->bms_psy, |
| 1814 | POWER_SUPPLY_PROP_VOLTAGE_NOW, val); |
| 1815 | return rc; |
| 1816 | } |
| 1817 | |
| 1818 | int smblib_get_prop_batt_current_now(struct smb_charger *chg, |
| 1819 | union power_supply_propval *val) |
| 1820 | { |
| 1821 | int rc; |
| 1822 | |
| 1823 | if (!chg->bms_psy) |
| 1824 | return -EINVAL; |
| 1825 | |
| 1826 | rc = power_supply_get_property(chg->bms_psy, |
| 1827 | POWER_SUPPLY_PROP_CURRENT_NOW, val); |
| 1828 | return rc; |
| 1829 | } |
| 1830 | |
| 1831 | int smblib_get_prop_batt_temp(struct smb_charger *chg, |
| 1832 | union power_supply_propval *val) |
| 1833 | { |
| 1834 | int rc; |
| 1835 | |
| 1836 | if (!chg->bms_psy) |
| 1837 | return -EINVAL; |
| 1838 | |
| 1839 | rc = power_supply_get_property(chg->bms_psy, |
| 1840 | POWER_SUPPLY_PROP_TEMP, val); |
| 1841 | return rc; |
| 1842 | } |
| 1843 | |
Subbaraman Narayanamurthy | b491d02 | 2016-10-10 20:22:48 -0700 | [diff] [blame] | 1844 | int smblib_get_prop_batt_charge_done(struct smb_charger *chg, |
| 1845 | union power_supply_propval *val) |
| 1846 | { |
| 1847 | int rc; |
| 1848 | u8 stat; |
| 1849 | |
| 1850 | rc = smblib_read(chg, BATTERY_CHARGER_STATUS_1_REG, &stat); |
| 1851 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 1852 | smblib_err(chg, "Couldn't read BATTERY_CHARGER_STATUS_1 rc=%d\n", |
Subbaraman Narayanamurthy | b491d02 | 2016-10-10 20:22:48 -0700 | [diff] [blame] | 1853 | rc); |
| 1854 | return rc; |
| 1855 | } |
| 1856 | |
| 1857 | stat = stat & BATTERY_CHARGER_STATUS_MASK; |
| 1858 | val->intval = (stat == TERMINATE_CHARGE); |
| 1859 | return 0; |
| 1860 | } |
| 1861 | |
Harry Yang | 40192cb | 2017-02-25 23:25:17 -0800 | [diff] [blame] | 1862 | int smblib_get_prop_charge_qnovo_enable(struct smb_charger *chg, |
| 1863 | union power_supply_propval *val) |
| 1864 | { |
| 1865 | int rc; |
| 1866 | u8 stat; |
| 1867 | |
| 1868 | rc = smblib_read(chg, QNOVO_PT_ENABLE_CMD_REG, &stat); |
| 1869 | if (rc < 0) { |
| 1870 | smblib_err(chg, "Couldn't read QNOVO_PT_ENABLE_CMD rc=%d\n", |
| 1871 | rc); |
| 1872 | return rc; |
| 1873 | } |
| 1874 | |
| 1875 | val->intval = (bool)(stat & QNOVO_PT_ENABLE_CMD_BIT); |
| 1876 | return 0; |
| 1877 | } |
| 1878 | |
Nicholas Troast | 4fe68d0 | 2017-07-26 14:17:34 -0700 | [diff] [blame] | 1879 | int smblib_get_prop_batt_charge_counter(struct smb_charger *chg, |
| 1880 | union power_supply_propval *val) |
| 1881 | { |
| 1882 | int rc; |
| 1883 | |
| 1884 | if (!chg->bms_psy) |
| 1885 | return -EINVAL; |
| 1886 | |
| 1887 | rc = power_supply_get_property(chg->bms_psy, |
| 1888 | POWER_SUPPLY_PROP_CHARGE_COUNTER, val); |
| 1889 | return rc; |
| 1890 | } |
| 1891 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1892 | /*********************** |
| 1893 | * BATTERY PSY SETTERS * |
| 1894 | ***********************/ |
| 1895 | |
| 1896 | int smblib_set_prop_input_suspend(struct smb_charger *chg, |
| 1897 | const union power_supply_propval *val) |
| 1898 | { |
| 1899 | int rc; |
| 1900 | |
Abhijeet Dharmapurikar | 5e0bfbe | 2017-02-12 19:16:15 -0800 | [diff] [blame] | 1901 | /* vote 0mA when suspended */ |
| 1902 | rc = vote(chg->usb_icl_votable, USER_VOTER, (bool)val->intval, 0); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1903 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 1904 | smblib_err(chg, "Couldn't vote to %s USB rc=%d\n", |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1905 | (bool)val->intval ? "suspend" : "resume", rc); |
| 1906 | return rc; |
| 1907 | } |
| 1908 | |
| 1909 | rc = vote(chg->dc_suspend_votable, USER_VOTER, (bool)val->intval, 0); |
| 1910 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 1911 | smblib_err(chg, "Couldn't vote to %s DC rc=%d\n", |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1912 | (bool)val->intval ? "suspend" : "resume", rc); |
| 1913 | return rc; |
| 1914 | } |
| 1915 | |
Nicholas Troast | 61ff40f | 2016-07-08 10:59:22 -0700 | [diff] [blame] | 1916 | power_supply_changed(chg->batt_psy); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1917 | return rc; |
| 1918 | } |
| 1919 | |
Abhijeet Dharmapurikar | 9e7e48c | 2016-08-23 12:55:49 -0700 | [diff] [blame] | 1920 | int smblib_set_prop_batt_capacity(struct smb_charger *chg, |
| 1921 | const union power_supply_propval *val) |
| 1922 | { |
| 1923 | chg->fake_capacity = val->intval; |
| 1924 | |
| 1925 | power_supply_changed(chg->batt_psy); |
| 1926 | |
| 1927 | return 0; |
| 1928 | } |
| 1929 | |
Harry Yang | 589dd42 | 2017-07-28 18:41:42 -0700 | [diff] [blame] | 1930 | int smblib_set_prop_batt_status(struct smb_charger *chg, |
| 1931 | const union power_supply_propval *val) |
| 1932 | { |
| 1933 | /* Faking battery full */ |
| 1934 | if (val->intval == POWER_SUPPLY_STATUS_FULL) |
| 1935 | chg->fake_batt_status = val->intval; |
| 1936 | else |
| 1937 | chg->fake_batt_status = -EINVAL; |
| 1938 | |
| 1939 | power_supply_changed(chg->batt_psy); |
| 1940 | |
| 1941 | return 0; |
| 1942 | } |
| 1943 | |
Abhijeet Dharmapurikar | 99fb894 | 2016-07-08 11:39:23 -0700 | [diff] [blame] | 1944 | int smblib_set_prop_system_temp_level(struct smb_charger *chg, |
| 1945 | const union power_supply_propval *val) |
| 1946 | { |
| 1947 | if (val->intval < 0) |
| 1948 | return -EINVAL; |
| 1949 | |
| 1950 | if (chg->thermal_levels <= 0) |
| 1951 | return -EINVAL; |
| 1952 | |
| 1953 | if (val->intval > chg->thermal_levels) |
| 1954 | return -EINVAL; |
| 1955 | |
| 1956 | chg->system_temp_level = val->intval; |
Ashay Jaiswal | 147a6c3 | 2017-03-28 17:19:47 +0530 | [diff] [blame] | 1957 | /* disable parallel charge in case of system temp level */ |
| 1958 | vote(chg->pl_disable_votable, THERMAL_DAEMON_VOTER, |
| 1959 | chg->system_temp_level ? true : false, 0); |
| 1960 | |
Abhijeet Dharmapurikar | 99fb894 | 2016-07-08 11:39:23 -0700 | [diff] [blame] | 1961 | if (chg->system_temp_level == chg->thermal_levels) |
Harry Yang | aba1f5f | 2016-09-28 10:47:29 -0700 | [diff] [blame] | 1962 | return vote(chg->chg_disable_votable, |
| 1963 | THERMAL_DAEMON_VOTER, true, 0); |
Abhijeet Dharmapurikar | 99fb894 | 2016-07-08 11:39:23 -0700 | [diff] [blame] | 1964 | |
Harry Yang | aba1f5f | 2016-09-28 10:47:29 -0700 | [diff] [blame] | 1965 | vote(chg->chg_disable_votable, THERMAL_DAEMON_VOTER, false, 0); |
Abhijeet Dharmapurikar | 99fb894 | 2016-07-08 11:39:23 -0700 | [diff] [blame] | 1966 | if (chg->system_temp_level == 0) |
Harry Yang | aba1f5f | 2016-09-28 10:47:29 -0700 | [diff] [blame] | 1967 | return vote(chg->fcc_votable, THERMAL_DAEMON_VOTER, false, 0); |
Abhijeet Dharmapurikar | 99fb894 | 2016-07-08 11:39:23 -0700 | [diff] [blame] | 1968 | |
Harry Yang | aba1f5f | 2016-09-28 10:47:29 -0700 | [diff] [blame] | 1969 | vote(chg->fcc_votable, THERMAL_DAEMON_VOTER, true, |
Abhijeet Dharmapurikar | 99fb894 | 2016-07-08 11:39:23 -0700 | [diff] [blame] | 1970 | chg->thermal_mitigation[chg->system_temp_level]); |
| 1971 | return 0; |
| 1972 | } |
| 1973 | |
Harry Yang | 40192cb | 2017-02-25 23:25:17 -0800 | [diff] [blame] | 1974 | int smblib_set_prop_charge_qnovo_enable(struct smb_charger *chg, |
| 1975 | const union power_supply_propval *val) |
| 1976 | { |
| 1977 | int rc = 0; |
| 1978 | |
| 1979 | rc = smblib_masked_write(chg, QNOVO_PT_ENABLE_CMD_REG, |
| 1980 | QNOVO_PT_ENABLE_CMD_BIT, |
| 1981 | val->intval ? QNOVO_PT_ENABLE_CMD_BIT : 0); |
| 1982 | if (rc < 0) { |
| 1983 | dev_err(chg->dev, "Couldn't enable qnovo rc=%d\n", rc); |
| 1984 | return rc; |
| 1985 | } |
| 1986 | |
| 1987 | return rc; |
| 1988 | } |
| 1989 | |
Abhijeet Dharmapurikar | 2ec2a79 | 2017-05-01 20:00:25 -0700 | [diff] [blame] | 1990 | int smblib_set_prop_input_current_limited(struct smb_charger *chg, |
| 1991 | const union power_supply_propval *val) |
| 1992 | { |
| 1993 | chg->fake_input_current_limited = val->intval; |
| 1994 | return 0; |
| 1995 | } |
| 1996 | |
Ashay Jaiswal | 6d308da | 2017-02-18 09:59:23 +0530 | [diff] [blame] | 1997 | int smblib_rerun_aicl(struct smb_charger *chg) |
| 1998 | { |
Nicholas Troast | 20ae191 | 2017-02-15 10:15:32 -0800 | [diff] [blame] | 1999 | int rc, settled_icl_ua; |
| 2000 | u8 stat; |
Ashay Jaiswal | 6d308da | 2017-02-18 09:59:23 +0530 | [diff] [blame] | 2001 | |
Nicholas Troast | 20ae191 | 2017-02-15 10:15:32 -0800 | [diff] [blame] | 2002 | rc = smblib_read(chg, POWER_PATH_STATUS_REG, &stat); |
| 2003 | if (rc < 0) { |
| 2004 | smblib_err(chg, "Couldn't read POWER_PATH_STATUS rc=%d\n", |
| 2005 | rc); |
| 2006 | return rc; |
| 2007 | } |
| 2008 | |
| 2009 | /* USB is suspended so skip re-running AICL */ |
| 2010 | if (stat & USBIN_SUSPEND_STS_BIT) |
| 2011 | return rc; |
| 2012 | |
| 2013 | smblib_dbg(chg, PR_MISC, "re-running AICL\n"); |
Ashay Jaiswal | 59a1412 | 2017-05-16 13:38:52 +0530 | [diff] [blame] | 2014 | rc = smblib_get_charge_param(chg, &chg->param.icl_stat, |
| 2015 | &settled_icl_ua); |
| 2016 | if (rc < 0) { |
| 2017 | smblib_err(chg, "Couldn't get settled ICL rc=%d\n", rc); |
| 2018 | return rc; |
Ashay Jaiswal | 6d308da | 2017-02-18 09:59:23 +0530 | [diff] [blame] | 2019 | } |
Ashay Jaiswal | 6d308da | 2017-02-18 09:59:23 +0530 | [diff] [blame] | 2020 | |
Ashay Jaiswal | 59a1412 | 2017-05-16 13:38:52 +0530 | [diff] [blame] | 2021 | vote(chg->usb_icl_votable, AICL_RERUN_VOTER, true, |
| 2022 | max(settled_icl_ua - chg->param.usb_icl.step_u, |
| 2023 | chg->param.usb_icl.step_u)); |
| 2024 | vote(chg->usb_icl_votable, AICL_RERUN_VOTER, false, 0); |
| 2025 | |
Nicholas Troast | 20ae191 | 2017-02-15 10:15:32 -0800 | [diff] [blame] | 2026 | return 0; |
Ashay Jaiswal | 6d308da | 2017-02-18 09:59:23 +0530 | [diff] [blame] | 2027 | } |
| 2028 | |
| 2029 | static int smblib_dp_pulse(struct smb_charger *chg) |
| 2030 | { |
| 2031 | int rc; |
| 2032 | |
| 2033 | /* QC 3.0 increment */ |
| 2034 | rc = smblib_masked_write(chg, CMD_HVDCP_2_REG, SINGLE_INCREMENT_BIT, |
| 2035 | SINGLE_INCREMENT_BIT); |
| 2036 | if (rc < 0) |
| 2037 | smblib_err(chg, "Couldn't write to CMD_HVDCP_2_REG rc=%d\n", |
| 2038 | rc); |
| 2039 | |
| 2040 | return rc; |
| 2041 | } |
| 2042 | |
| 2043 | static int smblib_dm_pulse(struct smb_charger *chg) |
| 2044 | { |
| 2045 | int rc; |
| 2046 | |
| 2047 | /* QC 3.0 decrement */ |
| 2048 | rc = smblib_masked_write(chg, CMD_HVDCP_2_REG, SINGLE_DECREMENT_BIT, |
| 2049 | SINGLE_DECREMENT_BIT); |
| 2050 | if (rc < 0) |
| 2051 | smblib_err(chg, "Couldn't write to CMD_HVDCP_2_REG rc=%d\n", |
| 2052 | rc); |
| 2053 | |
| 2054 | return rc; |
| 2055 | } |
| 2056 | |
| 2057 | int smblib_dp_dm(struct smb_charger *chg, int val) |
| 2058 | { |
| 2059 | int target_icl_ua, rc = 0; |
Ashay Jaiswal | ae23a04 | 2017-05-18 15:28:31 +0530 | [diff] [blame] | 2060 | union power_supply_propval pval; |
Ashay Jaiswal | 6d308da | 2017-02-18 09:59:23 +0530 | [diff] [blame] | 2061 | |
| 2062 | switch (val) { |
| 2063 | case POWER_SUPPLY_DP_DM_DP_PULSE: |
| 2064 | rc = smblib_dp_pulse(chg); |
| 2065 | if (!rc) |
| 2066 | chg->pulse_cnt++; |
| 2067 | smblib_dbg(chg, PR_PARALLEL, "DP_DM_DP_PULSE rc=%d cnt=%d\n", |
| 2068 | rc, chg->pulse_cnt); |
| 2069 | break; |
| 2070 | case POWER_SUPPLY_DP_DM_DM_PULSE: |
| 2071 | rc = smblib_dm_pulse(chg); |
| 2072 | if (!rc && chg->pulse_cnt) |
| 2073 | chg->pulse_cnt--; |
| 2074 | smblib_dbg(chg, PR_PARALLEL, "DP_DM_DM_PULSE rc=%d cnt=%d\n", |
| 2075 | rc, chg->pulse_cnt); |
| 2076 | break; |
| 2077 | case POWER_SUPPLY_DP_DM_ICL_DOWN: |
Ashay Jaiswal | 6d308da | 2017-02-18 09:59:23 +0530 | [diff] [blame] | 2078 | target_icl_ua = get_effective_result(chg->usb_icl_votable); |
Ashay Jaiswal | ae23a04 | 2017-05-18 15:28:31 +0530 | [diff] [blame] | 2079 | if (target_icl_ua < 0) { |
| 2080 | /* no client vote, get the ICL from charger */ |
| 2081 | rc = power_supply_get_property(chg->usb_psy, |
| 2082 | POWER_SUPPLY_PROP_HW_CURRENT_MAX, |
| 2083 | &pval); |
| 2084 | if (rc < 0) { |
| 2085 | smblib_err(chg, |
| 2086 | "Couldn't get max current rc=%d\n", |
| 2087 | rc); |
| 2088 | return rc; |
| 2089 | } |
| 2090 | target_icl_ua = pval.intval; |
| 2091 | } |
| 2092 | |
| 2093 | /* |
| 2094 | * Check if any other voter voted on USB_ICL in case of |
| 2095 | * voter other than SW_QC3_VOTER reset and restart reduction |
| 2096 | * again. |
| 2097 | */ |
| 2098 | if (target_icl_ua != get_client_vote(chg->usb_icl_votable, |
| 2099 | SW_QC3_VOTER)) |
| 2100 | chg->usb_icl_delta_ua = 0; |
| 2101 | |
| 2102 | chg->usb_icl_delta_ua += 100000; |
Ashay Jaiswal | 6d308da | 2017-02-18 09:59:23 +0530 | [diff] [blame] | 2103 | vote(chg->usb_icl_votable, SW_QC3_VOTER, true, |
Ashay Jaiswal | ae23a04 | 2017-05-18 15:28:31 +0530 | [diff] [blame] | 2104 | target_icl_ua - 100000); |
| 2105 | smblib_dbg(chg, PR_PARALLEL, "ICL DOWN ICL=%d reduction=%d\n", |
| 2106 | target_icl_ua, chg->usb_icl_delta_ua); |
Ashay Jaiswal | 6d308da | 2017-02-18 09:59:23 +0530 | [diff] [blame] | 2107 | break; |
| 2108 | case POWER_SUPPLY_DP_DM_ICL_UP: |
| 2109 | default: |
| 2110 | break; |
| 2111 | } |
| 2112 | |
| 2113 | return rc; |
| 2114 | } |
| 2115 | |
Abhijeet Dharmapurikar | 3a58004 | 2017-07-24 09:43:00 -0700 | [diff] [blame] | 2116 | int smblib_disable_hw_jeita(struct smb_charger *chg, bool disable) |
| 2117 | { |
| 2118 | int rc; |
| 2119 | u8 mask; |
| 2120 | |
| 2121 | /* |
| 2122 | * Disable h/w base JEITA compensation if s/w JEITA is enabled |
| 2123 | */ |
| 2124 | mask = JEITA_EN_COLD_SL_FCV_BIT |
| 2125 | | JEITA_EN_HOT_SL_FCV_BIT |
| 2126 | | JEITA_EN_HOT_SL_CCC_BIT |
| 2127 | | JEITA_EN_COLD_SL_CCC_BIT, |
| 2128 | rc = smblib_masked_write(chg, JEITA_EN_CFG_REG, mask, |
| 2129 | disable ? 0 : mask); |
| 2130 | if (rc < 0) { |
| 2131 | dev_err(chg->dev, |
| 2132 | "Couldn't configure s/w jeita rc=%d\n", |
| 2133 | rc); |
| 2134 | return rc; |
| 2135 | } |
| 2136 | return 0; |
| 2137 | } |
| 2138 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2139 | /******************* |
Harry Yang | f302359 | 2016-07-20 14:56:41 -0700 | [diff] [blame] | 2140 | * DC PSY GETTERS * |
| 2141 | *******************/ |
| 2142 | |
| 2143 | int smblib_get_prop_dc_present(struct smb_charger *chg, |
| 2144 | union power_supply_propval *val) |
| 2145 | { |
Nicholas Troast | deae99c | 2016-11-14 09:13:01 -0800 | [diff] [blame] | 2146 | int rc; |
Harry Yang | f302359 | 2016-07-20 14:56:41 -0700 | [diff] [blame] | 2147 | u8 stat; |
| 2148 | |
Nicholas Troast | deae99c | 2016-11-14 09:13:01 -0800 | [diff] [blame] | 2149 | rc = smblib_read(chg, DCIN_BASE + INT_RT_STS_OFFSET, &stat); |
Harry Yang | f302359 | 2016-07-20 14:56:41 -0700 | [diff] [blame] | 2150 | if (rc < 0) { |
Nicholas Troast | deae99c | 2016-11-14 09:13:01 -0800 | [diff] [blame] | 2151 | smblib_err(chg, "Couldn't read DCIN_RT_STS rc=%d\n", rc); |
Harry Yang | f302359 | 2016-07-20 14:56:41 -0700 | [diff] [blame] | 2152 | return rc; |
| 2153 | } |
Harry Yang | f302359 | 2016-07-20 14:56:41 -0700 | [diff] [blame] | 2154 | |
| 2155 | val->intval = (bool)(stat & DCIN_PLUGIN_RT_STS_BIT); |
Nicholas Troast | deae99c | 2016-11-14 09:13:01 -0800 | [diff] [blame] | 2156 | return 0; |
Harry Yang | f302359 | 2016-07-20 14:56:41 -0700 | [diff] [blame] | 2157 | } |
| 2158 | |
| 2159 | int smblib_get_prop_dc_online(struct smb_charger *chg, |
| 2160 | union power_supply_propval *val) |
| 2161 | { |
| 2162 | int rc = 0; |
| 2163 | u8 stat; |
| 2164 | |
| 2165 | if (get_client_vote(chg->dc_suspend_votable, USER_VOTER)) { |
| 2166 | val->intval = false; |
| 2167 | return rc; |
| 2168 | } |
| 2169 | |
| 2170 | rc = smblib_read(chg, POWER_PATH_STATUS_REG, &stat); |
| 2171 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 2172 | smblib_err(chg, "Couldn't read POWER_PATH_STATUS rc=%d\n", |
Harry Yang | f302359 | 2016-07-20 14:56:41 -0700 | [diff] [blame] | 2173 | rc); |
| 2174 | return rc; |
| 2175 | } |
| 2176 | smblib_dbg(chg, PR_REGISTER, "POWER_PATH_STATUS = 0x%02x\n", |
| 2177 | stat); |
| 2178 | |
| 2179 | val->intval = (stat & USE_DCIN_BIT) && |
Vic Wei | 7ade284 | 2016-12-14 18:47:49 -0800 | [diff] [blame] | 2180 | (stat & VALID_INPUT_POWER_SOURCE_STS_BIT); |
Harry Yang | f302359 | 2016-07-20 14:56:41 -0700 | [diff] [blame] | 2181 | |
| 2182 | return rc; |
| 2183 | } |
| 2184 | |
| 2185 | int smblib_get_prop_dc_current_max(struct smb_charger *chg, |
| 2186 | union power_supply_propval *val) |
| 2187 | { |
| 2188 | val->intval = get_effective_result_locked(chg->dc_icl_votable); |
| 2189 | return 0; |
| 2190 | } |
| 2191 | |
| 2192 | /******************* |
Harry Yang | d89ff1f | 2016-12-05 14:59:11 -0800 | [diff] [blame] | 2193 | * DC PSY SETTERS * |
Harry Yang | f302359 | 2016-07-20 14:56:41 -0700 | [diff] [blame] | 2194 | * *****************/ |
| 2195 | |
| 2196 | int smblib_set_prop_dc_current_max(struct smb_charger *chg, |
| 2197 | const union power_supply_propval *val) |
| 2198 | { |
| 2199 | int rc; |
| 2200 | |
| 2201 | rc = vote(chg->dc_icl_votable, USER_VOTER, true, val->intval); |
| 2202 | return rc; |
| 2203 | } |
| 2204 | |
| 2205 | /******************* |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2206 | * USB PSY GETTERS * |
| 2207 | *******************/ |
| 2208 | |
| 2209 | int smblib_get_prop_usb_present(struct smb_charger *chg, |
| 2210 | union power_supply_propval *val) |
| 2211 | { |
Nicholas Troast | deae99c | 2016-11-14 09:13:01 -0800 | [diff] [blame] | 2212 | int rc; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2213 | u8 stat; |
| 2214 | |
Nicholas Troast | deae99c | 2016-11-14 09:13:01 -0800 | [diff] [blame] | 2215 | rc = smblib_read(chg, USBIN_BASE + INT_RT_STS_OFFSET, &stat); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2216 | if (rc < 0) { |
Nicholas Troast | deae99c | 2016-11-14 09:13:01 -0800 | [diff] [blame] | 2217 | smblib_err(chg, "Couldn't read USBIN_RT_STS rc=%d\n", rc); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2218 | return rc; |
| 2219 | } |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2220 | |
Nicholas Troast | deae99c | 2016-11-14 09:13:01 -0800 | [diff] [blame] | 2221 | val->intval = (bool)(stat & USBIN_PLUGIN_RT_STS_BIT); |
| 2222 | return 0; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2223 | } |
| 2224 | |
| 2225 | int smblib_get_prop_usb_online(struct smb_charger *chg, |
| 2226 | union power_supply_propval *val) |
| 2227 | { |
| 2228 | int rc = 0; |
| 2229 | u8 stat; |
| 2230 | |
Abhijeet Dharmapurikar | 84923af | 2017-03-23 14:07:07 -0700 | [diff] [blame] | 2231 | if (get_client_vote_locked(chg->usb_icl_votable, USER_VOTER) == 0) { |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2232 | val->intval = false; |
| 2233 | return rc; |
| 2234 | } |
| 2235 | |
| 2236 | rc = smblib_read(chg, POWER_PATH_STATUS_REG, &stat); |
| 2237 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 2238 | smblib_err(chg, "Couldn't read POWER_PATH_STATUS rc=%d\n", |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2239 | rc); |
| 2240 | return rc; |
| 2241 | } |
| 2242 | smblib_dbg(chg, PR_REGISTER, "POWER_PATH_STATUS = 0x%02x\n", |
| 2243 | stat); |
| 2244 | |
| 2245 | val->intval = (stat & USE_USBIN_BIT) && |
Vic Wei | 7ade284 | 2016-12-14 18:47:49 -0800 | [diff] [blame] | 2246 | (stat & VALID_INPUT_POWER_SOURCE_STS_BIT); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2247 | return rc; |
| 2248 | } |
| 2249 | |
Nicholas Troast | 7f55c92 | 2017-07-25 13:18:03 -0700 | [diff] [blame] | 2250 | int smblib_get_prop_usb_voltage_max(struct smb_charger *chg, |
| 2251 | union power_supply_propval *val) |
| 2252 | { |
| 2253 | switch (chg->real_charger_type) { |
| 2254 | case POWER_SUPPLY_TYPE_USB_HVDCP: |
| 2255 | case POWER_SUPPLY_TYPE_USB_PD: |
| 2256 | if (chg->smb_version == PM660_SUBTYPE) |
| 2257 | val->intval = MICRO_9V; |
| 2258 | else |
| 2259 | val->intval = MICRO_12V; |
| 2260 | break; |
| 2261 | default: |
| 2262 | val->intval = MICRO_5V; |
| 2263 | break; |
| 2264 | } |
| 2265 | |
| 2266 | return 0; |
| 2267 | } |
| 2268 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2269 | int smblib_get_prop_usb_voltage_now(struct smb_charger *chg, |
| 2270 | union power_supply_propval *val) |
| 2271 | { |
Harry Yang | ba874ce | 2016-08-19 14:17:01 -0700 | [diff] [blame] | 2272 | if (!chg->iio.usbin_v_chan || |
| 2273 | PTR_ERR(chg->iio.usbin_v_chan) == -EPROBE_DEFER) |
| 2274 | chg->iio.usbin_v_chan = iio_channel_get(chg->dev, "usbin_v"); |
| 2275 | |
| 2276 | if (IS_ERR(chg->iio.usbin_v_chan)) |
| 2277 | return PTR_ERR(chg->iio.usbin_v_chan); |
| 2278 | |
| 2279 | return iio_read_channel_processed(chg->iio.usbin_v_chan, &val->intval); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2280 | } |
| 2281 | |
Harry Yang | ba874ce | 2016-08-19 14:17:01 -0700 | [diff] [blame] | 2282 | int smblib_get_prop_usb_current_now(struct smb_charger *chg, |
| 2283 | union power_supply_propval *val) |
| 2284 | { |
| 2285 | int rc = 0; |
| 2286 | |
| 2287 | rc = smblib_get_prop_usb_present(chg, val); |
| 2288 | if (rc < 0 || !val->intval) |
| 2289 | return rc; |
| 2290 | |
| 2291 | if (!chg->iio.usbin_i_chan || |
| 2292 | PTR_ERR(chg->iio.usbin_i_chan) == -EPROBE_DEFER) |
| 2293 | chg->iio.usbin_i_chan = iio_channel_get(chg->dev, "usbin_i"); |
| 2294 | |
| 2295 | if (IS_ERR(chg->iio.usbin_i_chan)) |
| 2296 | return PTR_ERR(chg->iio.usbin_i_chan); |
| 2297 | |
| 2298 | return iio_read_channel_processed(chg->iio.usbin_i_chan, &val->intval); |
| 2299 | } |
| 2300 | |
| 2301 | int smblib_get_prop_charger_temp(struct smb_charger *chg, |
| 2302 | union power_supply_propval *val) |
| 2303 | { |
| 2304 | int rc; |
| 2305 | |
| 2306 | if (!chg->iio.temp_chan || |
| 2307 | PTR_ERR(chg->iio.temp_chan) == -EPROBE_DEFER) |
| 2308 | chg->iio.temp_chan = iio_channel_get(chg->dev, "charger_temp"); |
| 2309 | |
| 2310 | if (IS_ERR(chg->iio.temp_chan)) |
| 2311 | return PTR_ERR(chg->iio.temp_chan); |
| 2312 | |
| 2313 | rc = iio_read_channel_processed(chg->iio.temp_chan, &val->intval); |
| 2314 | val->intval /= 100; |
| 2315 | return rc; |
| 2316 | } |
| 2317 | |
| 2318 | int smblib_get_prop_charger_temp_max(struct smb_charger *chg, |
| 2319 | union power_supply_propval *val) |
| 2320 | { |
| 2321 | int rc; |
| 2322 | |
| 2323 | if (!chg->iio.temp_max_chan || |
| 2324 | PTR_ERR(chg->iio.temp_max_chan) == -EPROBE_DEFER) |
| 2325 | chg->iio.temp_max_chan = iio_channel_get(chg->dev, |
| 2326 | "charger_temp_max"); |
| 2327 | if (IS_ERR(chg->iio.temp_max_chan)) |
| 2328 | return PTR_ERR(chg->iio.temp_max_chan); |
| 2329 | |
| 2330 | rc = iio_read_channel_processed(chg->iio.temp_max_chan, &val->intval); |
| 2331 | val->intval /= 100; |
| 2332 | return rc; |
| 2333 | } |
| 2334 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2335 | int smblib_get_prop_typec_cc_orientation(struct smb_charger *chg, |
| 2336 | union power_supply_propval *val) |
| 2337 | { |
Abhijeet Dharmapurikar | b0403c4 | 2017-04-17 12:26:26 -0700 | [diff] [blame] | 2338 | if (chg->typec_status[3] & CC_ATTACHED_BIT) |
| 2339 | val->intval = |
| 2340 | (bool)(chg->typec_status[3] & CC_ORIENTATION_BIT) + 1; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2341 | else |
| 2342 | val->intval = 0; |
| 2343 | |
Abhijeet Dharmapurikar | b0403c4 | 2017-04-17 12:26:26 -0700 | [diff] [blame] | 2344 | return 0; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2345 | } |
| 2346 | |
| 2347 | static const char * const smblib_typec_mode_name[] = { |
| 2348 | [POWER_SUPPLY_TYPEC_NONE] = "NONE", |
| 2349 | [POWER_SUPPLY_TYPEC_SOURCE_DEFAULT] = "SOURCE_DEFAULT", |
| 2350 | [POWER_SUPPLY_TYPEC_SOURCE_MEDIUM] = "SOURCE_MEDIUM", |
| 2351 | [POWER_SUPPLY_TYPEC_SOURCE_HIGH] = "SOURCE_HIGH", |
| 2352 | [POWER_SUPPLY_TYPEC_NON_COMPLIANT] = "NON_COMPLIANT", |
| 2353 | [POWER_SUPPLY_TYPEC_SINK] = "SINK", |
| 2354 | [POWER_SUPPLY_TYPEC_SINK_POWERED_CABLE] = "SINK_POWERED_CABLE", |
| 2355 | [POWER_SUPPLY_TYPEC_SINK_DEBUG_ACCESSORY] = "SINK_DEBUG_ACCESSORY", |
| 2356 | [POWER_SUPPLY_TYPEC_SINK_AUDIO_ADAPTER] = "SINK_AUDIO_ADAPTER", |
| 2357 | [POWER_SUPPLY_TYPEC_POWERED_CABLE_ONLY] = "POWERED_CABLE_ONLY", |
| 2358 | }; |
| 2359 | |
| 2360 | static int smblib_get_prop_ufp_mode(struct smb_charger *chg) |
| 2361 | { |
Abhijeet Dharmapurikar | b0403c4 | 2017-04-17 12:26:26 -0700 | [diff] [blame] | 2362 | switch (chg->typec_status[0]) { |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2363 | case UFP_TYPEC_RDSTD_BIT: |
| 2364 | return POWER_SUPPLY_TYPEC_SOURCE_DEFAULT; |
| 2365 | case UFP_TYPEC_RD1P5_BIT: |
| 2366 | return POWER_SUPPLY_TYPEC_SOURCE_MEDIUM; |
| 2367 | case UFP_TYPEC_RD3P0_BIT: |
| 2368 | return POWER_SUPPLY_TYPEC_SOURCE_HIGH; |
| 2369 | default: |
| 2370 | break; |
| 2371 | } |
| 2372 | |
Nicholas Troast | e1932e4 | 2017-04-12 12:38:18 -0700 | [diff] [blame] | 2373 | return POWER_SUPPLY_TYPEC_NONE; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2374 | } |
| 2375 | |
| 2376 | static int smblib_get_prop_dfp_mode(struct smb_charger *chg) |
| 2377 | { |
Abhijeet Dharmapurikar | b0403c4 | 2017-04-17 12:26:26 -0700 | [diff] [blame] | 2378 | switch (chg->typec_status[1] & DFP_TYPEC_MASK) { |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2379 | case DFP_RA_RA_BIT: |
| 2380 | return POWER_SUPPLY_TYPEC_SINK_AUDIO_ADAPTER; |
| 2381 | case DFP_RD_RD_BIT: |
| 2382 | return POWER_SUPPLY_TYPEC_SINK_DEBUG_ACCESSORY; |
| 2383 | case DFP_RD_RA_VCONN_BIT: |
| 2384 | return POWER_SUPPLY_TYPEC_SINK_POWERED_CABLE; |
| 2385 | case DFP_RD_OPEN_BIT: |
| 2386 | return POWER_SUPPLY_TYPEC_SINK; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2387 | default: |
| 2388 | break; |
| 2389 | } |
| 2390 | |
| 2391 | return POWER_SUPPLY_TYPEC_NONE; |
| 2392 | } |
| 2393 | |
Nicholas Troast | e1932e4 | 2017-04-12 12:38:18 -0700 | [diff] [blame] | 2394 | static int smblib_get_prop_typec_mode(struct smb_charger *chg) |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2395 | { |
Abhijeet Dharmapurikar | b0403c4 | 2017-04-17 12:26:26 -0700 | [diff] [blame] | 2396 | if (chg->typec_status[3] & UFP_DFP_MODE_STATUS_BIT) |
Nicholas Troast | e1932e4 | 2017-04-12 12:38:18 -0700 | [diff] [blame] | 2397 | return smblib_get_prop_dfp_mode(chg); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2398 | else |
Nicholas Troast | e1932e4 | 2017-04-12 12:38:18 -0700 | [diff] [blame] | 2399 | return smblib_get_prop_ufp_mode(chg); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2400 | } |
| 2401 | |
| 2402 | int smblib_get_prop_typec_power_role(struct smb_charger *chg, |
| 2403 | union power_supply_propval *val) |
| 2404 | { |
| 2405 | int rc = 0; |
| 2406 | u8 ctrl; |
| 2407 | |
| 2408 | rc = smblib_read(chg, TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG, &ctrl); |
| 2409 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 2410 | smblib_err(chg, "Couldn't read TYPE_C_INTRPT_ENB_SOFTWARE_CTRL rc=%d\n", |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2411 | rc); |
| 2412 | return rc; |
| 2413 | } |
| 2414 | smblib_dbg(chg, PR_REGISTER, "TYPE_C_INTRPT_ENB_SOFTWARE_CTRL = 0x%02x\n", |
| 2415 | ctrl); |
| 2416 | |
| 2417 | if (ctrl & TYPEC_DISABLE_CMD_BIT) { |
| 2418 | val->intval = POWER_SUPPLY_TYPEC_PR_NONE; |
| 2419 | return rc; |
| 2420 | } |
| 2421 | |
| 2422 | switch (ctrl & (DFP_EN_CMD_BIT | UFP_EN_CMD_BIT)) { |
| 2423 | case 0: |
| 2424 | val->intval = POWER_SUPPLY_TYPEC_PR_DUAL; |
| 2425 | break; |
| 2426 | case DFP_EN_CMD_BIT: |
| 2427 | val->intval = POWER_SUPPLY_TYPEC_PR_SOURCE; |
| 2428 | break; |
| 2429 | case UFP_EN_CMD_BIT: |
| 2430 | val->intval = POWER_SUPPLY_TYPEC_PR_SINK; |
| 2431 | break; |
| 2432 | default: |
| 2433 | val->intval = POWER_SUPPLY_TYPEC_PR_NONE; |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 2434 | smblib_err(chg, "unsupported power role 0x%02lx\n", |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2435 | ctrl & (DFP_EN_CMD_BIT | UFP_EN_CMD_BIT)); |
| 2436 | return -EINVAL; |
| 2437 | } |
| 2438 | |
| 2439 | return rc; |
| 2440 | } |
| 2441 | |
| 2442 | int smblib_get_prop_pd_allowed(struct smb_charger *chg, |
| 2443 | union power_supply_propval *val) |
| 2444 | { |
Abhijeet Dharmapurikar | b959887 | 2016-10-17 16:58:58 -0700 | [diff] [blame] | 2445 | val->intval = get_effective_result(chg->pd_allowed_votable); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2446 | return 0; |
| 2447 | } |
| 2448 | |
Nicholas Troast | 133a7f5 | 2016-06-29 13:48:20 -0700 | [diff] [blame] | 2449 | int smblib_get_prop_input_current_settled(struct smb_charger *chg, |
| 2450 | union power_supply_propval *val) |
| 2451 | { |
| 2452 | return smblib_get_charge_param(chg, &chg->param.icl_stat, &val->intval); |
| 2453 | } |
| 2454 | |
Fenglin Wu | ef4730e | 2017-01-11 18:16:25 +0800 | [diff] [blame] | 2455 | #define HVDCP3_STEP_UV 200000 |
| 2456 | int smblib_get_prop_input_voltage_settled(struct smb_charger *chg, |
| 2457 | union power_supply_propval *val) |
| 2458 | { |
Fenglin Wu | ef4730e | 2017-01-11 18:16:25 +0800 | [diff] [blame] | 2459 | int rc, pulses; |
Fenglin Wu | ef4730e | 2017-01-11 18:16:25 +0800 | [diff] [blame] | 2460 | |
Nicholas Troast | 5f314c1 | 2017-05-25 11:58:02 -0700 | [diff] [blame] | 2461 | switch (chg->real_charger_type) { |
Fenglin Wu | ef4730e | 2017-01-11 18:16:25 +0800 | [diff] [blame] | 2462 | case POWER_SUPPLY_TYPE_USB_HVDCP_3: |
Ashay Jaiswal | cda0d1c | 2017-05-15 17:15:29 +0530 | [diff] [blame] | 2463 | rc = smblib_get_pulse_cnt(chg, &pulses); |
Fenglin Wu | ef4730e | 2017-01-11 18:16:25 +0800 | [diff] [blame] | 2464 | if (rc < 0) { |
| 2465 | smblib_err(chg, |
| 2466 | "Couldn't read QC_PULSE_COUNT rc=%d\n", rc); |
| 2467 | return 0; |
| 2468 | } |
Fenglin Wu | ef4730e | 2017-01-11 18:16:25 +0800 | [diff] [blame] | 2469 | val->intval = MICRO_5V + HVDCP3_STEP_UV * pulses; |
| 2470 | break; |
Nicholas Troast | 5f314c1 | 2017-05-25 11:58:02 -0700 | [diff] [blame] | 2471 | case POWER_SUPPLY_TYPE_USB_PD: |
| 2472 | val->intval = chg->voltage_min_uv; |
| 2473 | break; |
Fenglin Wu | ef4730e | 2017-01-11 18:16:25 +0800 | [diff] [blame] | 2474 | default: |
| 2475 | val->intval = MICRO_5V; |
| 2476 | break; |
| 2477 | } |
| 2478 | |
| 2479 | return 0; |
| 2480 | } |
| 2481 | |
Abhijeet Dharmapurikar | f8a7a4a | 2016-10-07 18:46:45 -0700 | [diff] [blame] | 2482 | int smblib_get_prop_pd_in_hard_reset(struct smb_charger *chg, |
| 2483 | union power_supply_propval *val) |
| 2484 | { |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 2485 | val->intval = chg->pd_hard_reset; |
Abhijeet Dharmapurikar | f8a7a4a | 2016-10-07 18:46:45 -0700 | [diff] [blame] | 2486 | return 0; |
| 2487 | } |
| 2488 | |
Abhijeet Dharmapurikar | b959887 | 2016-10-17 16:58:58 -0700 | [diff] [blame] | 2489 | int smblib_get_pe_start(struct smb_charger *chg, |
| 2490 | union power_supply_propval *val) |
| 2491 | { |
| 2492 | /* |
| 2493 | * hvdcp timeout voter is the last one to allow pd. Use its vote |
| 2494 | * to indicate start of pe engine |
| 2495 | */ |
| 2496 | val->intval |
| 2497 | = !get_client_vote_locked(chg->pd_disallowed_votable_indirect, |
| 2498 | HVDCP_TIMEOUT_VOTER); |
| 2499 | return 0; |
| 2500 | } |
| 2501 | |
Nicholas Troast | 7fdbd2e | 2017-02-08 10:47:37 -0800 | [diff] [blame] | 2502 | int smblib_get_prop_die_health(struct smb_charger *chg, |
Nicholas Troast | b021dd9 | 2017-01-31 18:43:38 -0800 | [diff] [blame] | 2503 | union power_supply_propval *val) |
| 2504 | { |
Nicholas Troast | 7fdbd2e | 2017-02-08 10:47:37 -0800 | [diff] [blame] | 2505 | int rc; |
Nicholas Troast | b021dd9 | 2017-01-31 18:43:38 -0800 | [diff] [blame] | 2506 | u8 stat; |
| 2507 | |
| 2508 | rc = smblib_read(chg, TEMP_RANGE_STATUS_REG, &stat); |
| 2509 | if (rc < 0) { |
| 2510 | smblib_err(chg, "Couldn't read TEMP_RANGE_STATUS_REG rc=%d\n", |
| 2511 | rc); |
| 2512 | return rc; |
| 2513 | } |
| 2514 | |
Nicholas Troast | 7fdbd2e | 2017-02-08 10:47:37 -0800 | [diff] [blame] | 2515 | /* TEMP_RANGE bits are mutually exclusive */ |
| 2516 | switch (stat & TEMP_RANGE_MASK) { |
| 2517 | case TEMP_BELOW_RANGE_BIT: |
| 2518 | val->intval = POWER_SUPPLY_HEALTH_COOL; |
| 2519 | break; |
| 2520 | case TEMP_WITHIN_RANGE_BIT: |
| 2521 | val->intval = POWER_SUPPLY_HEALTH_WARM; |
| 2522 | break; |
| 2523 | case TEMP_ABOVE_RANGE_BIT: |
| 2524 | val->intval = POWER_SUPPLY_HEALTH_HOT; |
| 2525 | break; |
| 2526 | case ALERT_LEVEL_BIT: |
| 2527 | val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; |
| 2528 | break; |
| 2529 | default: |
| 2530 | val->intval = POWER_SUPPLY_HEALTH_UNKNOWN; |
Nicholas Troast | b021dd9 | 2017-01-31 18:43:38 -0800 | [diff] [blame] | 2531 | } |
| 2532 | |
Nicholas Troast | b021dd9 | 2017-01-31 18:43:38 -0800 | [diff] [blame] | 2533 | return 0; |
| 2534 | } |
| 2535 | |
Anirudh Ghayal | 1121f5d | 2017-07-26 20:26:20 +0530 | [diff] [blame] | 2536 | #define SDP_CURRENT_UA 500000 |
| 2537 | #define CDP_CURRENT_UA 1500000 |
| 2538 | #define DCP_CURRENT_UA 1500000 |
| 2539 | #define HVDCP_CURRENT_UA 3000000 |
| 2540 | #define TYPEC_DEFAULT_CURRENT_UA 900000 |
| 2541 | #define TYPEC_MEDIUM_CURRENT_UA 1500000 |
| 2542 | #define TYPEC_HIGH_CURRENT_UA 3000000 |
| 2543 | static int get_rp_based_dcp_current(struct smb_charger *chg, int typec_mode) |
| 2544 | { |
| 2545 | int rp_ua; |
| 2546 | |
| 2547 | switch (typec_mode) { |
| 2548 | case POWER_SUPPLY_TYPEC_SOURCE_HIGH: |
| 2549 | rp_ua = TYPEC_HIGH_CURRENT_UA; |
| 2550 | break; |
| 2551 | case POWER_SUPPLY_TYPEC_SOURCE_MEDIUM: |
| 2552 | case POWER_SUPPLY_TYPEC_SOURCE_DEFAULT: |
| 2553 | /* fall through */ |
| 2554 | default: |
| 2555 | rp_ua = DCP_CURRENT_UA; |
| 2556 | } |
| 2557 | |
| 2558 | return rp_ua; |
| 2559 | } |
| 2560 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2561 | /******************* |
| 2562 | * USB PSY SETTERS * |
| 2563 | * *****************/ |
| 2564 | |
Abhijeet Dharmapurikar | d92eca6 | 2016-10-07 19:04:33 -0700 | [diff] [blame] | 2565 | int smblib_set_prop_pd_current_max(struct smb_charger *chg, |
| 2566 | const union power_supply_propval *val) |
| 2567 | { |
| 2568 | int rc; |
| 2569 | |
| 2570 | if (chg->pd_active) |
| 2571 | rc = vote(chg->usb_icl_votable, PD_VOTER, true, val->intval); |
| 2572 | else |
| 2573 | rc = -EPERM; |
| 2574 | |
| 2575 | return rc; |
| 2576 | } |
| 2577 | |
Anirudh Ghayal | 1121f5d | 2017-07-26 20:26:20 +0530 | [diff] [blame] | 2578 | static int smblib_handle_usb_current(struct smb_charger *chg, |
| 2579 | int usb_current) |
| 2580 | { |
| 2581 | int rc = 0, rp_ua, typec_mode; |
| 2582 | |
| 2583 | if (chg->real_charger_type == POWER_SUPPLY_TYPE_USB_FLOAT) { |
| 2584 | if (usb_current == -ETIMEDOUT) { |
| 2585 | /* |
| 2586 | * Valid FLOAT charger, report the current based |
| 2587 | * of Rp |
| 2588 | */ |
| 2589 | typec_mode = smblib_get_prop_typec_mode(chg); |
| 2590 | rp_ua = get_rp_based_dcp_current(chg, typec_mode); |
| 2591 | rc = vote(chg->usb_icl_votable, LEGACY_UNKNOWN_VOTER, |
| 2592 | true, rp_ua); |
| 2593 | if (rc < 0) |
| 2594 | return rc; |
| 2595 | } else { |
| 2596 | /* |
| 2597 | * FLOAT charger detected as SDP by USB driver, |
| 2598 | * charge with the requested current and update the |
| 2599 | * real_charger_type |
| 2600 | */ |
| 2601 | chg->real_charger_type = POWER_SUPPLY_TYPE_USB; |
| 2602 | rc = vote(chg->usb_icl_votable, USB_PSY_VOTER, |
| 2603 | true, usb_current); |
| 2604 | if (rc < 0) |
| 2605 | return rc; |
| 2606 | rc = vote(chg->usb_icl_votable, LEGACY_UNKNOWN_VOTER, |
| 2607 | false, 0); |
| 2608 | if (rc < 0) |
| 2609 | return rc; |
| 2610 | } |
| 2611 | } else { |
| 2612 | rc = vote(chg->usb_icl_votable, USB_PSY_VOTER, |
| 2613 | true, usb_current); |
| 2614 | } |
| 2615 | |
| 2616 | return rc; |
| 2617 | } |
| 2618 | |
Nicholas Troast | 7f55c92 | 2017-07-25 13:18:03 -0700 | [diff] [blame] | 2619 | int smblib_set_prop_sdp_current_max(struct smb_charger *chg, |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2620 | const union power_supply_propval *val) |
| 2621 | { |
Nicholas Troast | 8d33b7d | 2017-01-16 11:18:38 -0800 | [diff] [blame] | 2622 | int rc = 0; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2623 | |
Abhijeet Dharmapurikar | d92eca6 | 2016-10-07 19:04:33 -0700 | [diff] [blame] | 2624 | if (!chg->pd_active) { |
Anirudh Ghayal | 1121f5d | 2017-07-26 20:26:20 +0530 | [diff] [blame] | 2625 | rc = smblib_handle_usb_current(chg, val->intval); |
Abhijeet Dharmapurikar | d92eca6 | 2016-10-07 19:04:33 -0700 | [diff] [blame] | 2626 | } else if (chg->system_suspend_supported) { |
| 2627 | if (val->intval <= USBIN_25MA) |
Abhijeet Dharmapurikar | 9567087 | 2017-02-09 22:55:51 +0530 | [diff] [blame] | 2628 | rc = vote(chg->usb_icl_votable, |
| 2629 | PD_SUSPEND_SUPPORTED_VOTER, true, val->intval); |
Abhijeet Dharmapurikar | d92eca6 | 2016-10-07 19:04:33 -0700 | [diff] [blame] | 2630 | else |
Abhijeet Dharmapurikar | 9567087 | 2017-02-09 22:55:51 +0530 | [diff] [blame] | 2631 | rc = vote(chg->usb_icl_votable, |
| 2632 | PD_SUSPEND_SUPPORTED_VOTER, false, 0); |
Abhijeet Dharmapurikar | d92eca6 | 2016-10-07 19:04:33 -0700 | [diff] [blame] | 2633 | } |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2634 | return rc; |
| 2635 | } |
| 2636 | |
Harry Yang | d89ff1f | 2016-12-05 14:59:11 -0800 | [diff] [blame] | 2637 | int smblib_set_prop_boost_current(struct smb_charger *chg, |
| 2638 | const union power_supply_propval *val) |
| 2639 | { |
| 2640 | int rc = 0; |
| 2641 | |
| 2642 | rc = smblib_set_charge_param(chg, &chg->param.freq_boost, |
| 2643 | val->intval <= chg->boost_threshold_ua ? |
Anirudh Ghayal | 4da3df9 | 2017-01-25 18:55:57 +0530 | [diff] [blame] | 2644 | chg->chg_freq.freq_below_otg_threshold : |
| 2645 | chg->chg_freq.freq_above_otg_threshold); |
Harry Yang | d89ff1f | 2016-12-05 14:59:11 -0800 | [diff] [blame] | 2646 | if (rc < 0) { |
| 2647 | dev_err(chg->dev, "Error in setting freq_boost rc=%d\n", rc); |
| 2648 | return rc; |
| 2649 | } |
| 2650 | |
| 2651 | chg->boost_current_ua = val->intval; |
| 2652 | return rc; |
| 2653 | } |
| 2654 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2655 | int smblib_set_prop_typec_power_role(struct smb_charger *chg, |
| 2656 | const union power_supply_propval *val) |
| 2657 | { |
| 2658 | int rc = 0; |
| 2659 | u8 power_role; |
| 2660 | |
| 2661 | switch (val->intval) { |
| 2662 | case POWER_SUPPLY_TYPEC_PR_NONE: |
| 2663 | power_role = TYPEC_DISABLE_CMD_BIT; |
| 2664 | break; |
| 2665 | case POWER_SUPPLY_TYPEC_PR_DUAL: |
| 2666 | power_role = 0; |
| 2667 | break; |
| 2668 | case POWER_SUPPLY_TYPEC_PR_SINK: |
| 2669 | power_role = UFP_EN_CMD_BIT; |
| 2670 | break; |
| 2671 | case POWER_SUPPLY_TYPEC_PR_SOURCE: |
| 2672 | power_role = DFP_EN_CMD_BIT; |
| 2673 | break; |
| 2674 | default: |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 2675 | smblib_err(chg, "power role %d not supported\n", val->intval); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2676 | return -EINVAL; |
| 2677 | } |
| 2678 | |
Jack Pham | 54a39bd | 2017-03-29 18:59:37 -0700 | [diff] [blame] | 2679 | if (power_role == UFP_EN_CMD_BIT) { |
| 2680 | /* disable PBS workaround when forcing sink mode */ |
| 2681 | rc = smblib_write(chg, TM_IO_DTEST4_SEL, 0x0); |
| 2682 | if (rc < 0) { |
| 2683 | smblib_err(chg, "Couldn't write to TM_IO_DTEST4_SEL rc=%d\n", |
| 2684 | rc); |
| 2685 | } |
| 2686 | } else { |
| 2687 | /* restore it back to 0xA5 */ |
| 2688 | rc = smblib_write(chg, TM_IO_DTEST4_SEL, 0xA5); |
| 2689 | if (rc < 0) { |
| 2690 | smblib_err(chg, "Couldn't write to TM_IO_DTEST4_SEL rc=%d\n", |
| 2691 | rc); |
| 2692 | } |
| 2693 | } |
| 2694 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2695 | rc = smblib_masked_write(chg, TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG, |
| 2696 | TYPEC_POWER_ROLE_CMD_MASK, power_role); |
| 2697 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 2698 | smblib_err(chg, "Couldn't write 0x%02x to TYPE_C_INTRPT_ENB_SOFTWARE_CTRL rc=%d\n", |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2699 | power_role, rc); |
| 2700 | return rc; |
| 2701 | } |
| 2702 | |
| 2703 | return rc; |
| 2704 | } |
| 2705 | |
Nicholas Troast | 7f55c92 | 2017-07-25 13:18:03 -0700 | [diff] [blame] | 2706 | int smblib_set_prop_pd_voltage_min(struct smb_charger *chg, |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2707 | const union power_supply_propval *val) |
| 2708 | { |
| 2709 | int rc, min_uv; |
| 2710 | |
| 2711 | min_uv = min(val->intval, chg->voltage_max_uv); |
| 2712 | rc = smblib_set_usb_pd_allowed_voltage(chg, min_uv, |
| 2713 | chg->voltage_max_uv); |
| 2714 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 2715 | smblib_err(chg, "invalid max voltage %duV rc=%d\n", |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2716 | val->intval, rc); |
| 2717 | return rc; |
| 2718 | } |
| 2719 | |
Harry Yang | aba1f5f | 2016-09-28 10:47:29 -0700 | [diff] [blame] | 2720 | chg->voltage_min_uv = min_uv; |
Nicholas Troast | 5f314c1 | 2017-05-25 11:58:02 -0700 | [diff] [blame] | 2721 | power_supply_changed(chg->usb_main_psy); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2722 | return rc; |
| 2723 | } |
| 2724 | |
Nicholas Troast | 7f55c92 | 2017-07-25 13:18:03 -0700 | [diff] [blame] | 2725 | int smblib_set_prop_pd_voltage_max(struct smb_charger *chg, |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2726 | const union power_supply_propval *val) |
| 2727 | { |
| 2728 | int rc, max_uv; |
| 2729 | |
| 2730 | max_uv = max(val->intval, chg->voltage_min_uv); |
| 2731 | rc = smblib_set_usb_pd_allowed_voltage(chg, chg->voltage_min_uv, |
| 2732 | max_uv); |
| 2733 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 2734 | smblib_err(chg, "invalid min voltage %duV rc=%d\n", |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2735 | val->intval, rc); |
| 2736 | return rc; |
| 2737 | } |
| 2738 | |
Harry Yang | aba1f5f | 2016-09-28 10:47:29 -0700 | [diff] [blame] | 2739 | chg->voltage_max_uv = max_uv; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2740 | return rc; |
| 2741 | } |
| 2742 | |
| 2743 | int smblib_set_prop_pd_active(struct smb_charger *chg, |
| 2744 | const union power_supply_propval *val) |
| 2745 | { |
| 2746 | int rc; |
Nicholas Troast | e1932e4 | 2017-04-12 12:38:18 -0700 | [diff] [blame] | 2747 | bool orientation, sink_attached, hvdcp; |
Abhijeet Dharmapurikar | 3c93db3 | 2017-04-17 17:36:14 -0700 | [diff] [blame] | 2748 | u8 stat; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2749 | |
Nicholas Troast | 8c28e0f | 2017-03-22 14:44:49 -0700 | [diff] [blame] | 2750 | if (!get_effective_result(chg->pd_allowed_votable)) |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2751 | return -EINVAL; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2752 | |
Nicholas Troast | 8c28e0f | 2017-03-22 14:44:49 -0700 | [diff] [blame] | 2753 | chg->pd_active = val->intval; |
| 2754 | if (chg->pd_active) { |
| 2755 | vote(chg->apsd_disable_votable, PD_VOTER, true, 0); |
| 2756 | vote(chg->pd_allowed_votable, PD_VOTER, true, 0); |
| 2757 | vote(chg->usb_irq_enable_votable, PD_VOTER, true, 0); |
| 2758 | |
| 2759 | /* |
| 2760 | * VCONN_EN_ORIENTATION_BIT controls whether to use CC1 or CC2 |
| 2761 | * line when TYPEC_SPARE_CFG_BIT (CC pin selection s/w override) |
| 2762 | * is set or when VCONN_EN_VALUE_BIT is set. |
| 2763 | */ |
Abhijeet Dharmapurikar | b0403c4 | 2017-04-17 12:26:26 -0700 | [diff] [blame] | 2764 | orientation = chg->typec_status[3] & CC_ORIENTATION_BIT; |
Harry Yang | 88acff4 | 2016-09-21 14:56:06 -0700 | [diff] [blame] | 2765 | rc = smblib_masked_write(chg, |
Abhijeet Dharmapurikar | 716cd96 | 2016-10-14 12:50:37 -0700 | [diff] [blame] | 2766 | TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG, |
| 2767 | VCONN_EN_ORIENTATION_BIT, |
| 2768 | orientation ? 0 : VCONN_EN_ORIENTATION_BIT); |
Nicholas Troast | 8c28e0f | 2017-03-22 14:44:49 -0700 | [diff] [blame] | 2769 | if (rc < 0) |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 2770 | smblib_err(chg, |
Harry Yang | 88acff4 | 2016-09-21 14:56:06 -0700 | [diff] [blame] | 2771 | "Couldn't enable vconn on CC line rc=%d\n", rc); |
Nicholas Troast | 8c28e0f | 2017-03-22 14:44:49 -0700 | [diff] [blame] | 2772 | |
| 2773 | /* SW controlled CC_OUT */ |
| 2774 | rc = smblib_masked_write(chg, TAPER_TIMER_SEL_CFG_REG, |
| 2775 | TYPEC_SPARE_CFG_BIT, TYPEC_SPARE_CFG_BIT); |
| 2776 | if (rc < 0) |
| 2777 | smblib_err(chg, "Couldn't enable SW cc_out rc=%d\n", |
| 2778 | rc); |
| 2779 | |
Abhijeet Dharmapurikar | 9567087 | 2017-02-09 22:55:51 +0530 | [diff] [blame] | 2780 | /* |
| 2781 | * Enforce 500mA for PD until the real vote comes in later. |
| 2782 | * It is guaranteed that pd_active is set prior to |
| 2783 | * pd_current_max |
| 2784 | */ |
Harry Yang | cd99520 | 2016-11-07 13:32:52 -0800 | [diff] [blame] | 2785 | rc = vote(chg->usb_icl_votable, PD_VOTER, true, USBIN_500MA); |
Nicholas Troast | 8c28e0f | 2017-03-22 14:44:49 -0700 | [diff] [blame] | 2786 | if (rc < 0) |
Harry Yang | cd99520 | 2016-11-07 13:32:52 -0800 | [diff] [blame] | 2787 | smblib_err(chg, "Couldn't vote for USB ICL rc=%d\n", |
Nicholas Troast | 8c28e0f | 2017-03-22 14:44:49 -0700 | [diff] [blame] | 2788 | rc); |
Harry Yang | cd99520 | 2016-11-07 13:32:52 -0800 | [diff] [blame] | 2789 | |
Nicholas Troast | f9e4499 | 2017-03-14 09:06:56 -0700 | [diff] [blame] | 2790 | /* since PD was found the cable must be non-legacy */ |
| 2791 | vote(chg->usb_icl_votable, LEGACY_UNKNOWN_VOTER, false, 0); |
| 2792 | |
Abhijeet Dharmapurikar | 7442e42 | 2017-02-08 13:35:14 -0800 | [diff] [blame] | 2793 | /* clear USB ICL vote for DCP_VOTER */ |
Harry Yang | 631b99e | 2016-11-17 11:24:25 -0800 | [diff] [blame] | 2794 | rc = vote(chg->usb_icl_votable, DCP_VOTER, false, 0); |
Abhijeet Dharmapurikar | 7442e42 | 2017-02-08 13:35:14 -0800 | [diff] [blame] | 2795 | if (rc < 0) |
Nicholas Troast | 8c28e0f | 2017-03-22 14:44:49 -0700 | [diff] [blame] | 2796 | smblib_err(chg, "Couldn't un-vote DCP from USB ICL rc=%d\n", |
| 2797 | rc); |
Abhijeet Dharmapurikar | 7442e42 | 2017-02-08 13:35:14 -0800 | [diff] [blame] | 2798 | |
Abhijeet Dharmapurikar | 9567087 | 2017-02-09 22:55:51 +0530 | [diff] [blame] | 2799 | /* remove USB_PSY_VOTER */ |
| 2800 | rc = vote(chg->usb_icl_votable, USB_PSY_VOTER, false, 0); |
Nicholas Troast | 8c28e0f | 2017-03-22 14:44:49 -0700 | [diff] [blame] | 2801 | if (rc < 0) |
Abhijeet Dharmapurikar | 9567087 | 2017-02-09 22:55:51 +0530 | [diff] [blame] | 2802 | smblib_err(chg, "Couldn't unvote USB_PSY rc=%d\n", rc); |
Nicholas Troast | 8c28e0f | 2017-03-22 14:44:49 -0700 | [diff] [blame] | 2803 | } else { |
Nicholas Troast | e1932e4 | 2017-04-12 12:38:18 -0700 | [diff] [blame] | 2804 | rc = smblib_read(chg, APSD_STATUS_REG, &stat); |
| 2805 | if (rc < 0) { |
| 2806 | smblib_err(chg, "Couldn't read APSD status rc=%d\n", |
| 2807 | rc); |
| 2808 | return rc; |
| 2809 | } |
| 2810 | |
| 2811 | hvdcp = stat & QC_CHARGER_BIT; |
Nicholas Troast | 8c28e0f | 2017-03-22 14:44:49 -0700 | [diff] [blame] | 2812 | vote(chg->apsd_disable_votable, PD_VOTER, false, 0); |
| 2813 | vote(chg->pd_allowed_votable, PD_VOTER, true, 0); |
| 2814 | vote(chg->usb_irq_enable_votable, PD_VOTER, true, 0); |
Abhijeet Dharmapurikar | 3c93db3 | 2017-04-17 17:36:14 -0700 | [diff] [blame] | 2815 | vote(chg->hvdcp_disable_votable_indirect, PD_INACTIVE_VOTER, |
| 2816 | false, 0); |
Nicholas Troast | 8c28e0f | 2017-03-22 14:44:49 -0700 | [diff] [blame] | 2817 | |
| 2818 | /* HW controlled CC_OUT */ |
| 2819 | rc = smblib_masked_write(chg, TAPER_TIMER_SEL_CFG_REG, |
| 2820 | TYPEC_SPARE_CFG_BIT, 0); |
| 2821 | if (rc < 0) |
| 2822 | smblib_err(chg, "Couldn't enable HW cc_out rc=%d\n", |
| 2823 | rc); |
| 2824 | |
Abhijeet Dharmapurikar | 3c93db3 | 2017-04-17 17:36:14 -0700 | [diff] [blame] | 2825 | /* |
| 2826 | * This WA should only run for HVDCP. Non-legacy SDP/CDP could |
| 2827 | * draw more, but this WA will remove Rd causing VBUS to drop, |
| 2828 | * and data could be interrupted. Non-legacy DCP could also draw |
| 2829 | * more, but it may impact compliance. |
| 2830 | */ |
Nicholas Troast | e1932e4 | 2017-04-12 12:38:18 -0700 | [diff] [blame] | 2831 | sink_attached = chg->typec_status[3] & UFP_DFP_MODE_STATUS_BIT; |
| 2832 | if (!chg->typec_legacy_valid && !sink_attached && hvdcp) |
Abhijeet Dharmapurikar | 3c93db3 | 2017-04-17 17:36:14 -0700 | [diff] [blame] | 2833 | schedule_work(&chg->legacy_detection_work); |
Harry Yang | 88acff4 | 2016-09-21 14:56:06 -0700 | [diff] [blame] | 2834 | } |
| 2835 | |
Harry Yang | 6607b4a | 2016-05-17 17:50:09 -0700 | [diff] [blame] | 2836 | smblib_update_usb_type(chg); |
Abhijeet Dharmapurikar | 76e2af5 | 2016-10-06 17:25:01 -0700 | [diff] [blame] | 2837 | power_supply_changed(chg->usb_psy); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 2838 | return rc; |
| 2839 | } |
| 2840 | |
Fenglin Wu | edd7079 | 2016-11-22 13:16:19 +0800 | [diff] [blame] | 2841 | int smblib_set_prop_ship_mode(struct smb_charger *chg, |
| 2842 | const union power_supply_propval *val) |
| 2843 | { |
| 2844 | int rc; |
| 2845 | |
| 2846 | smblib_dbg(chg, PR_MISC, "Set ship mode: %d!!\n", !!val->intval); |
| 2847 | |
| 2848 | rc = smblib_masked_write(chg, SHIP_MODE_REG, SHIP_MODE_EN_BIT, |
| 2849 | !!val->intval ? SHIP_MODE_EN_BIT : 0); |
| 2850 | if (rc < 0) |
| 2851 | dev_err(chg->dev, "Couldn't %s ship mode, rc=%d\n", |
| 2852 | !!val->intval ? "enable" : "disable", rc); |
| 2853 | |
| 2854 | return rc; |
| 2855 | } |
| 2856 | |
Harry Yang | 5e2bb71 | 2016-10-18 16:47:48 -0700 | [diff] [blame] | 2857 | int smblib_reg_block_update(struct smb_charger *chg, |
| 2858 | struct reg_info *entry) |
| 2859 | { |
| 2860 | int rc = 0; |
| 2861 | |
| 2862 | while (entry && entry->reg) { |
| 2863 | rc = smblib_read(chg, entry->reg, &entry->bak); |
| 2864 | if (rc < 0) { |
| 2865 | dev_err(chg->dev, "Error in reading %s rc=%d\n", |
| 2866 | entry->desc, rc); |
| 2867 | break; |
| 2868 | } |
| 2869 | entry->bak &= entry->mask; |
| 2870 | |
| 2871 | rc = smblib_masked_write(chg, entry->reg, |
| 2872 | entry->mask, entry->val); |
| 2873 | if (rc < 0) { |
| 2874 | dev_err(chg->dev, "Error in writing %s rc=%d\n", |
| 2875 | entry->desc, rc); |
| 2876 | break; |
| 2877 | } |
| 2878 | entry++; |
| 2879 | } |
| 2880 | |
| 2881 | return rc; |
| 2882 | } |
| 2883 | |
| 2884 | int smblib_reg_block_restore(struct smb_charger *chg, |
| 2885 | struct reg_info *entry) |
| 2886 | { |
| 2887 | int rc = 0; |
| 2888 | |
| 2889 | while (entry && entry->reg) { |
| 2890 | rc = smblib_masked_write(chg, entry->reg, |
| 2891 | entry->mask, entry->bak); |
| 2892 | if (rc < 0) { |
| 2893 | dev_err(chg->dev, "Error in writing %s rc=%d\n", |
| 2894 | entry->desc, rc); |
| 2895 | break; |
| 2896 | } |
| 2897 | entry++; |
| 2898 | } |
| 2899 | |
| 2900 | return rc; |
| 2901 | } |
| 2902 | |
Harry Yang | 755a34b | 2016-11-01 01:18:51 -0700 | [diff] [blame] | 2903 | static struct reg_info cc2_detach_settings[] = { |
| 2904 | { |
| 2905 | .reg = TYPE_C_CFG_2_REG, |
| 2906 | .mask = TYPE_C_UFP_MODE_BIT | EN_TRY_SOURCE_MODE_BIT, |
| 2907 | .val = TYPE_C_UFP_MODE_BIT, |
| 2908 | .desc = "TYPE_C_CFG_2_REG", |
| 2909 | }, |
| 2910 | { |
| 2911 | .reg = TYPE_C_CFG_3_REG, |
| 2912 | .mask = EN_TRYSINK_MODE_BIT, |
| 2913 | .val = 0, |
| 2914 | .desc = "TYPE_C_CFG_3_REG", |
| 2915 | }, |
| 2916 | { |
| 2917 | .reg = TAPER_TIMER_SEL_CFG_REG, |
| 2918 | .mask = TYPEC_SPARE_CFG_BIT, |
| 2919 | .val = TYPEC_SPARE_CFG_BIT, |
| 2920 | .desc = "TAPER_TIMER_SEL_CFG_REG", |
| 2921 | }, |
| 2922 | { |
| 2923 | .reg = TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG, |
| 2924 | .mask = VCONN_EN_ORIENTATION_BIT, |
| 2925 | .val = 0, |
| 2926 | .desc = "TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG", |
| 2927 | }, |
| 2928 | { |
| 2929 | .reg = MISC_CFG_REG, |
| 2930 | .mask = TCC_DEBOUNCE_20MS_BIT, |
| 2931 | .val = TCC_DEBOUNCE_20MS_BIT, |
| 2932 | .desc = "Tccdebounce time" |
| 2933 | }, |
| 2934 | { |
| 2935 | }, |
| 2936 | }; |
| 2937 | |
| 2938 | static int smblib_cc2_sink_removal_enter(struct smb_charger *chg) |
| 2939 | { |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 2940 | int rc, ccout, ufp_mode; |
| 2941 | u8 stat; |
Harry Yang | 755a34b | 2016-11-01 01:18:51 -0700 | [diff] [blame] | 2942 | |
| 2943 | if ((chg->wa_flags & TYPEC_CC2_REMOVAL_WA_BIT) == 0) |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 2944 | return 0; |
Harry Yang | 755a34b | 2016-11-01 01:18:51 -0700 | [diff] [blame] | 2945 | |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 2946 | if (chg->cc2_detach_wa_active) |
| 2947 | return 0; |
Harry Yang | 755a34b | 2016-11-01 01:18:51 -0700 | [diff] [blame] | 2948 | |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 2949 | rc = smblib_read(chg, TYPE_C_STATUS_4_REG, &stat); |
Harry Yang | 755a34b | 2016-11-01 01:18:51 -0700 | [diff] [blame] | 2950 | if (rc < 0) { |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 2951 | smblib_err(chg, "Couldn't read TYPE_C_STATUS_4 rc=%d\n", rc); |
Harry Yang | 755a34b | 2016-11-01 01:18:51 -0700 | [diff] [blame] | 2952 | return rc; |
| 2953 | } |
Nicholas Troast | e1932e4 | 2017-04-12 12:38:18 -0700 | [diff] [blame] | 2954 | |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 2955 | ccout = (stat & CC_ATTACHED_BIT) ? |
| 2956 | (!!(stat & CC_ORIENTATION_BIT) + 1) : 0; |
| 2957 | ufp_mode = (stat & TYPEC_DEBOUNCE_DONE_STATUS_BIT) ? |
| 2958 | !(stat & UFP_DFP_MODE_STATUS_BIT) : 0; |
Harry Yang | 755a34b | 2016-11-01 01:18:51 -0700 | [diff] [blame] | 2959 | |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 2960 | if (ccout != 2) |
| 2961 | return 0; |
Harry Yang | 755a34b | 2016-11-01 01:18:51 -0700 | [diff] [blame] | 2962 | |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 2963 | if (!ufp_mode) |
| 2964 | return 0; |
Harry Yang | 755a34b | 2016-11-01 01:18:51 -0700 | [diff] [blame] | 2965 | |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 2966 | chg->cc2_detach_wa_active = true; |
| 2967 | /* The CC2 removal WA will cause a type-c-change IRQ storm */ |
| 2968 | smblib_reg_block_update(chg, cc2_detach_settings); |
| 2969 | schedule_work(&chg->rdstd_cc2_detach_work); |
Harry Yang | 755a34b | 2016-11-01 01:18:51 -0700 | [diff] [blame] | 2970 | return rc; |
| 2971 | } |
| 2972 | |
| 2973 | static int smblib_cc2_sink_removal_exit(struct smb_charger *chg) |
| 2974 | { |
Harry Yang | 755a34b | 2016-11-01 01:18:51 -0700 | [diff] [blame] | 2975 | if ((chg->wa_flags & TYPEC_CC2_REMOVAL_WA_BIT) == 0) |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 2976 | return 0; |
Harry Yang | 755a34b | 2016-11-01 01:18:51 -0700 | [diff] [blame] | 2977 | |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 2978 | if (!chg->cc2_detach_wa_active) |
| 2979 | return 0; |
Harry Yang | 755a34b | 2016-11-01 01:18:51 -0700 | [diff] [blame] | 2980 | |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 2981 | chg->cc2_detach_wa_active = false; |
| 2982 | cancel_work_sync(&chg->rdstd_cc2_detach_work); |
| 2983 | smblib_reg_block_restore(chg, cc2_detach_settings); |
| 2984 | return 0; |
Harry Yang | 755a34b | 2016-11-01 01:18:51 -0700 | [diff] [blame] | 2985 | } |
| 2986 | |
Abhijeet Dharmapurikar | f8a7a4a | 2016-10-07 18:46:45 -0700 | [diff] [blame] | 2987 | int smblib_set_prop_pd_in_hard_reset(struct smb_charger *chg, |
| 2988 | const union power_supply_propval *val) |
| 2989 | { |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 2990 | int rc = 0; |
Abhijeet Dharmapurikar | f8a7a4a | 2016-10-07 18:46:45 -0700 | [diff] [blame] | 2991 | |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 2992 | if (chg->pd_hard_reset == val->intval) |
| 2993 | return rc; |
| 2994 | |
| 2995 | chg->pd_hard_reset = val->intval; |
Abhijeet Dharmapurikar | f8a7a4a | 2016-10-07 18:46:45 -0700 | [diff] [blame] | 2996 | rc = smblib_masked_write(chg, TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG, |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 2997 | EXIT_SNK_BASED_ON_CC_BIT, |
| 2998 | (chg->pd_hard_reset) ? EXIT_SNK_BASED_ON_CC_BIT : 0); |
| 2999 | if (rc < 0) |
| 3000 | smblib_err(chg, "Couldn't set EXIT_SNK_BASED_ON_CC rc=%d\n", |
Harry Yang | 755a34b | 2016-11-01 01:18:51 -0700 | [diff] [blame] | 3001 | rc); |
Abhijeet Dharmapurikar | f8a7a4a | 2016-10-07 18:46:45 -0700 | [diff] [blame] | 3002 | |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 3003 | vote(chg->apsd_disable_votable, PD_HARD_RESET_VOTER, |
| 3004 | chg->pd_hard_reset, 0); |
Harry Yang | 755a34b | 2016-11-01 01:18:51 -0700 | [diff] [blame] | 3005 | |
Abhijeet Dharmapurikar | f8a7a4a | 2016-10-07 18:46:45 -0700 | [diff] [blame] | 3006 | return rc; |
| 3007 | } |
| 3008 | |
Anirudh Ghayal | 941acaa | 2017-07-19 16:04:44 +0530 | [diff] [blame] | 3009 | static int smblib_recover_from_soft_jeita(struct smb_charger *chg) |
| 3010 | { |
| 3011 | u8 stat_1, stat_2; |
| 3012 | int rc; |
| 3013 | |
| 3014 | rc = smblib_read(chg, BATTERY_CHARGER_STATUS_1_REG, &stat_1); |
| 3015 | if (rc < 0) { |
| 3016 | smblib_err(chg, "Couldn't read BATTERY_CHARGER_STATUS_1 rc=%d\n", |
| 3017 | rc); |
| 3018 | return rc; |
| 3019 | } |
| 3020 | |
| 3021 | rc = smblib_read(chg, BATTERY_CHARGER_STATUS_2_REG, &stat_2); |
| 3022 | if (rc < 0) { |
| 3023 | smblib_err(chg, "Couldn't read BATTERY_CHARGER_STATUS_2 rc=%d\n", |
| 3024 | rc); |
| 3025 | return rc; |
| 3026 | } |
| 3027 | |
| 3028 | if ((chg->jeita_status && !(stat_2 & BAT_TEMP_STATUS_SOFT_LIMIT_MASK) && |
| 3029 | ((stat_1 & BATTERY_CHARGER_STATUS_MASK) == TERMINATE_CHARGE))) { |
| 3030 | /* |
| 3031 | * We are moving from JEITA soft -> Normal and charging |
| 3032 | * is terminated |
| 3033 | */ |
| 3034 | rc = smblib_write(chg, CHARGING_ENABLE_CMD_REG, 0); |
| 3035 | if (rc < 0) { |
| 3036 | smblib_err(chg, "Couldn't disable charging rc=%d\n", |
| 3037 | rc); |
| 3038 | return rc; |
| 3039 | } |
| 3040 | rc = smblib_write(chg, CHARGING_ENABLE_CMD_REG, |
| 3041 | CHARGING_ENABLE_CMD_BIT); |
| 3042 | if (rc < 0) { |
| 3043 | smblib_err(chg, "Couldn't enable charging rc=%d\n", |
| 3044 | rc); |
| 3045 | return rc; |
| 3046 | } |
| 3047 | } |
| 3048 | |
| 3049 | chg->jeita_status = stat_2 & BAT_TEMP_STATUS_SOFT_LIMIT_MASK; |
| 3050 | |
| 3051 | return 0; |
| 3052 | } |
| 3053 | |
Nicholas Troast | 7dbcad2 | 2016-10-05 13:30:18 -0700 | [diff] [blame] | 3054 | /************************ |
Abhijeet Dharmapurikar | 7442e42 | 2017-02-08 13:35:14 -0800 | [diff] [blame] | 3055 | * USB MAIN PSY GETTERS * |
| 3056 | ************************/ |
| 3057 | int smblib_get_prop_fcc_delta(struct smb_charger *chg, |
Anirudh Ghayal | 9d0196d | 2017-07-23 23:02:48 +0530 | [diff] [blame] | 3058 | union power_supply_propval *val) |
Abhijeet Dharmapurikar | 7442e42 | 2017-02-08 13:35:14 -0800 | [diff] [blame] | 3059 | { |
Anirudh Ghayal | 9d0196d | 2017-07-23 23:02:48 +0530 | [diff] [blame] | 3060 | int rc, jeita_cc_delta_ua = 0; |
Abhijeet Dharmapurikar | 7442e42 | 2017-02-08 13:35:14 -0800 | [diff] [blame] | 3061 | |
| 3062 | rc = smblib_get_jeita_cc_delta(chg, &jeita_cc_delta_ua); |
| 3063 | if (rc < 0) { |
| 3064 | smblib_err(chg, "Couldn't get jeita cc delta rc=%d\n", rc); |
| 3065 | jeita_cc_delta_ua = 0; |
Abhijeet Dharmapurikar | 7442e42 | 2017-02-08 13:35:14 -0800 | [diff] [blame] | 3066 | } |
| 3067 | |
Anirudh Ghayal | 9d0196d | 2017-07-23 23:02:48 +0530 | [diff] [blame] | 3068 | val->intval = jeita_cc_delta_ua; |
Abhijeet Dharmapurikar | 7442e42 | 2017-02-08 13:35:14 -0800 | [diff] [blame] | 3069 | return 0; |
| 3070 | } |
| 3071 | |
| 3072 | /************************ |
| 3073 | * USB MAIN PSY SETTERS * |
| 3074 | ************************/ |
Abhijeet Dharmapurikar | f59c835 | 2017-03-23 14:04:05 -0700 | [diff] [blame] | 3075 | int smblib_get_charge_current(struct smb_charger *chg, |
Abhijeet Dharmapurikar | 7442e42 | 2017-02-08 13:35:14 -0800 | [diff] [blame] | 3076 | int *total_current_ua) |
| 3077 | { |
Ashay Jaiswal | 7b6eb96 | 2017-04-26 14:34:29 +0530 | [diff] [blame] | 3078 | const struct apsd_result *apsd_result = smblib_get_apsd_result(chg); |
Abhijeet Dharmapurikar | 7442e42 | 2017-02-08 13:35:14 -0800 | [diff] [blame] | 3079 | union power_supply_propval val = {0, }; |
Abhijeet Dharmapurikar | f59c835 | 2017-03-23 14:04:05 -0700 | [diff] [blame] | 3080 | int rc = 0, typec_source_rd, current_ua; |
Abhijeet Dharmapurikar | 7442e42 | 2017-02-08 13:35:14 -0800 | [diff] [blame] | 3081 | bool non_compliant; |
| 3082 | u8 stat5; |
| 3083 | |
Abhijeet Dharmapurikar | f59c835 | 2017-03-23 14:04:05 -0700 | [diff] [blame] | 3084 | if (chg->pd_active) { |
| 3085 | *total_current_ua = |
| 3086 | get_client_vote_locked(chg->usb_icl_votable, PD_VOTER); |
| 3087 | return rc; |
| 3088 | } |
| 3089 | |
Abhijeet Dharmapurikar | 7442e42 | 2017-02-08 13:35:14 -0800 | [diff] [blame] | 3090 | rc = smblib_read(chg, TYPE_C_STATUS_5_REG, &stat5); |
| 3091 | if (rc < 0) { |
| 3092 | smblib_err(chg, "Couldn't read TYPE_C_STATUS_5 rc=%d\n", rc); |
| 3093 | return rc; |
| 3094 | } |
| 3095 | non_compliant = stat5 & TYPEC_NONCOMP_LEGACY_CABLE_STATUS_BIT; |
| 3096 | |
| 3097 | /* get settled ICL */ |
| 3098 | rc = smblib_get_prop_input_current_settled(chg, &val); |
| 3099 | if (rc < 0) { |
| 3100 | smblib_err(chg, "Couldn't get settled ICL rc=%d\n", rc); |
| 3101 | return rc; |
| 3102 | } |
| 3103 | |
| 3104 | typec_source_rd = smblib_get_prop_ufp_mode(chg); |
| 3105 | |
| 3106 | /* QC 2.0/3.0 adapter */ |
| 3107 | if (apsd_result->bit & (QC_3P0_BIT | QC_2P0_BIT)) { |
Ashay Jaiswal | dd08c62 | 2017-06-29 16:25:23 +0530 | [diff] [blame] | 3108 | *total_current_ua = HVDCP_CURRENT_UA; |
Abhijeet Dharmapurikar | 7442e42 | 2017-02-08 13:35:14 -0800 | [diff] [blame] | 3109 | return 0; |
| 3110 | } |
| 3111 | |
| 3112 | if (non_compliant) { |
| 3113 | switch (apsd_result->bit) { |
| 3114 | case CDP_CHARGER_BIT: |
Ashay Jaiswal | dd08c62 | 2017-06-29 16:25:23 +0530 | [diff] [blame] | 3115 | current_ua = CDP_CURRENT_UA; |
Abhijeet Dharmapurikar | 7442e42 | 2017-02-08 13:35:14 -0800 | [diff] [blame] | 3116 | break; |
| 3117 | case DCP_CHARGER_BIT: |
| 3118 | case OCP_CHARGER_BIT: |
| 3119 | case FLOAT_CHARGER_BIT: |
Ashay Jaiswal | dd08c62 | 2017-06-29 16:25:23 +0530 | [diff] [blame] | 3120 | current_ua = DCP_CURRENT_UA; |
Abhijeet Dharmapurikar | 7442e42 | 2017-02-08 13:35:14 -0800 | [diff] [blame] | 3121 | break; |
| 3122 | default: |
| 3123 | current_ua = 0; |
| 3124 | break; |
| 3125 | } |
| 3126 | |
| 3127 | *total_current_ua = max(current_ua, val.intval); |
| 3128 | return 0; |
| 3129 | } |
| 3130 | |
| 3131 | switch (typec_source_rd) { |
| 3132 | case POWER_SUPPLY_TYPEC_SOURCE_DEFAULT: |
| 3133 | switch (apsd_result->bit) { |
| 3134 | case CDP_CHARGER_BIT: |
Ashay Jaiswal | dd08c62 | 2017-06-29 16:25:23 +0530 | [diff] [blame] | 3135 | current_ua = CDP_CURRENT_UA; |
Abhijeet Dharmapurikar | 7442e42 | 2017-02-08 13:35:14 -0800 | [diff] [blame] | 3136 | break; |
| 3137 | case DCP_CHARGER_BIT: |
| 3138 | case OCP_CHARGER_BIT: |
| 3139 | case FLOAT_CHARGER_BIT: |
| 3140 | current_ua = chg->default_icl_ua; |
| 3141 | break; |
| 3142 | default: |
| 3143 | current_ua = 0; |
| 3144 | break; |
| 3145 | } |
| 3146 | break; |
| 3147 | case POWER_SUPPLY_TYPEC_SOURCE_MEDIUM: |
Ashay Jaiswal | dd08c62 | 2017-06-29 16:25:23 +0530 | [diff] [blame] | 3148 | current_ua = TYPEC_MEDIUM_CURRENT_UA; |
Abhijeet Dharmapurikar | 7442e42 | 2017-02-08 13:35:14 -0800 | [diff] [blame] | 3149 | break; |
| 3150 | case POWER_SUPPLY_TYPEC_SOURCE_HIGH: |
Ashay Jaiswal | dd08c62 | 2017-06-29 16:25:23 +0530 | [diff] [blame] | 3151 | current_ua = TYPEC_HIGH_CURRENT_UA; |
Abhijeet Dharmapurikar | 7442e42 | 2017-02-08 13:35:14 -0800 | [diff] [blame] | 3152 | break; |
| 3153 | case POWER_SUPPLY_TYPEC_NON_COMPLIANT: |
| 3154 | case POWER_SUPPLY_TYPEC_NONE: |
| 3155 | default: |
| 3156 | current_ua = 0; |
| 3157 | break; |
| 3158 | } |
| 3159 | |
| 3160 | *total_current_ua = max(current_ua, val.intval); |
| 3161 | return 0; |
| 3162 | } |
| 3163 | |
Abhijeet Dharmapurikar | 7442e42 | 2017-02-08 13:35:14 -0800 | [diff] [blame] | 3164 | /************************ |
Nicholas Troast | 7dbcad2 | 2016-10-05 13:30:18 -0700 | [diff] [blame] | 3165 | * PARALLEL PSY GETTERS * |
| 3166 | ************************/ |
| 3167 | |
| 3168 | int smblib_get_prop_slave_current_now(struct smb_charger *chg, |
Abhijeet Dharmapurikar | f59c835 | 2017-03-23 14:04:05 -0700 | [diff] [blame] | 3169 | union power_supply_propval *pval) |
Nicholas Troast | 7dbcad2 | 2016-10-05 13:30:18 -0700 | [diff] [blame] | 3170 | { |
| 3171 | if (IS_ERR_OR_NULL(chg->iio.batt_i_chan)) |
| 3172 | chg->iio.batt_i_chan = iio_channel_get(chg->dev, "batt_i"); |
| 3173 | |
| 3174 | if (IS_ERR(chg->iio.batt_i_chan)) |
| 3175 | return PTR_ERR(chg->iio.batt_i_chan); |
| 3176 | |
| 3177 | return iio_read_channel_processed(chg->iio.batt_i_chan, &pval->intval); |
| 3178 | } |
| 3179 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3180 | /********************** |
| 3181 | * INTERRUPT HANDLERS * |
| 3182 | **********************/ |
| 3183 | |
| 3184 | irqreturn_t smblib_handle_debug(int irq, void *data) |
| 3185 | { |
| 3186 | struct smb_irq_data *irq_data = data; |
| 3187 | struct smb_charger *chg = irq_data->parent_data; |
| 3188 | |
| 3189 | smblib_dbg(chg, PR_INTERRUPT, "IRQ: %s\n", irq_data->name); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3190 | return IRQ_HANDLED; |
| 3191 | } |
| 3192 | |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 3193 | irqreturn_t smblib_handle_otg_overcurrent(int irq, void *data) |
| 3194 | { |
| 3195 | struct smb_irq_data *irq_data = data; |
| 3196 | struct smb_charger *chg = irq_data->parent_data; |
| 3197 | int rc; |
| 3198 | u8 stat; |
| 3199 | |
| 3200 | rc = smblib_read(chg, OTG_BASE + INT_RT_STS_OFFSET, &stat); |
| 3201 | if (rc < 0) { |
| 3202 | dev_err(chg->dev, "Couldn't read OTG_INT_RT_STS rc=%d\n", rc); |
| 3203 | return IRQ_HANDLED; |
| 3204 | } |
| 3205 | |
Ashay Jaiswal | 7c24138 | 2017-03-06 15:26:38 +0530 | [diff] [blame] | 3206 | if (chg->wa_flags & OTG_WA) { |
| 3207 | if (stat & OTG_OC_DIS_SW_STS_RT_STS_BIT) |
| 3208 | smblib_err(chg, "OTG disabled by hw\n"); |
| 3209 | |
| 3210 | /* not handling software based hiccups for PM660 */ |
| 3211 | return IRQ_HANDLED; |
| 3212 | } |
| 3213 | |
Nicholas Troast | b11015f | 2017-01-17 17:56:45 -0800 | [diff] [blame] | 3214 | if (stat & OTG_OVERCURRENT_RT_STS_BIT) |
| 3215 | schedule_work(&chg->otg_oc_work); |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 3216 | |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 3217 | return IRQ_HANDLED; |
| 3218 | } |
| 3219 | |
Harry Yang | 6fe72ab | 2016-06-14 16:21:39 -0700 | [diff] [blame] | 3220 | irqreturn_t smblib_handle_chg_state_change(int irq, void *data) |
| 3221 | { |
| 3222 | struct smb_irq_data *irq_data = data; |
| 3223 | struct smb_charger *chg = irq_data->parent_data; |
Nicholas Troast | 8cb7755 | 2016-09-23 11:50:18 -0700 | [diff] [blame] | 3224 | u8 stat; |
Harry Yang | 1d1034c | 2016-06-15 12:09:42 -0700 | [diff] [blame] | 3225 | int rc; |
Harry Yang | 6fe72ab | 2016-06-14 16:21:39 -0700 | [diff] [blame] | 3226 | |
| 3227 | smblib_dbg(chg, PR_INTERRUPT, "IRQ: %s\n", irq_data->name); |
| 3228 | |
Nicholas Troast | 8cb7755 | 2016-09-23 11:50:18 -0700 | [diff] [blame] | 3229 | rc = smblib_read(chg, BATTERY_CHARGER_STATUS_1_REG, &stat); |
Harry Yang | 1d1034c | 2016-06-15 12:09:42 -0700 | [diff] [blame] | 3230 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 3231 | smblib_err(chg, "Couldn't read BATTERY_CHARGER_STATUS_1 rc=%d\n", |
Abhijeet Dharmapurikar | f59c835 | 2017-03-23 14:04:05 -0700 | [diff] [blame] | 3232 | rc); |
Harry Yang | 1d1034c | 2016-06-15 12:09:42 -0700 | [diff] [blame] | 3233 | return IRQ_HANDLED; |
| 3234 | } |
| 3235 | |
Nicholas Troast | 8cb7755 | 2016-09-23 11:50:18 -0700 | [diff] [blame] | 3236 | stat = stat & BATTERY_CHARGER_STATUS_MASK; |
Nicholas Troast | 8cb7755 | 2016-09-23 11:50:18 -0700 | [diff] [blame] | 3237 | power_supply_changed(chg->batt_psy); |
Harry Yang | 6fe72ab | 2016-06-14 16:21:39 -0700 | [diff] [blame] | 3238 | return IRQ_HANDLED; |
| 3239 | } |
| 3240 | |
Abhijeet Dharmapurikar | 2644cd6 | 2016-07-20 16:54:55 -0700 | [diff] [blame] | 3241 | irqreturn_t smblib_handle_batt_temp_changed(int irq, void *data) |
| 3242 | { |
| 3243 | struct smb_irq_data *irq_data = data; |
| 3244 | struct smb_charger *chg = irq_data->parent_data; |
Anirudh Ghayal | 941acaa | 2017-07-19 16:04:44 +0530 | [diff] [blame] | 3245 | int rc; |
| 3246 | |
| 3247 | rc = smblib_recover_from_soft_jeita(chg); |
| 3248 | if (rc < 0) { |
| 3249 | smblib_err(chg, "Couldn't recover chg from soft jeita rc=%d\n", |
| 3250 | rc); |
| 3251 | return IRQ_HANDLED; |
| 3252 | } |
Abhijeet Dharmapurikar | 2644cd6 | 2016-07-20 16:54:55 -0700 | [diff] [blame] | 3253 | |
| 3254 | rerun_election(chg->fcc_votable); |
| 3255 | power_supply_changed(chg->batt_psy); |
| 3256 | return IRQ_HANDLED; |
| 3257 | } |
| 3258 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3259 | irqreturn_t smblib_handle_batt_psy_changed(int irq, void *data) |
| 3260 | { |
| 3261 | struct smb_irq_data *irq_data = data; |
| 3262 | struct smb_charger *chg = irq_data->parent_data; |
| 3263 | |
Nicholas Troast | 47ae461 | 2016-08-03 09:49:36 -0700 | [diff] [blame] | 3264 | smblib_dbg(chg, PR_INTERRUPT, "IRQ: %s\n", irq_data->name); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3265 | power_supply_changed(chg->batt_psy); |
| 3266 | return IRQ_HANDLED; |
| 3267 | } |
| 3268 | |
| 3269 | irqreturn_t smblib_handle_usb_psy_changed(int irq, void *data) |
| 3270 | { |
| 3271 | struct smb_irq_data *irq_data = data; |
| 3272 | struct smb_charger *chg = irq_data->parent_data; |
| 3273 | |
Nicholas Troast | 47ae461 | 2016-08-03 09:49:36 -0700 | [diff] [blame] | 3274 | smblib_dbg(chg, PR_INTERRUPT, "IRQ: %s\n", irq_data->name); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3275 | power_supply_changed(chg->usb_psy); |
| 3276 | return IRQ_HANDLED; |
| 3277 | } |
| 3278 | |
Subbaraman Narayanamurthy | 0932748 | 2017-02-06 16:33:12 -0800 | [diff] [blame] | 3279 | irqreturn_t smblib_handle_usbin_uv(int irq, void *data) |
| 3280 | { |
| 3281 | struct smb_irq_data *irq_data = data; |
| 3282 | struct smb_charger *chg = irq_data->parent_data; |
| 3283 | struct storm_watch *wdata; |
| 3284 | |
| 3285 | smblib_dbg(chg, PR_INTERRUPT, "IRQ: %s\n", irq_data->name); |
| 3286 | if (!chg->irq_info[SWITCH_POWER_OK_IRQ].irq_data) |
| 3287 | return IRQ_HANDLED; |
| 3288 | |
| 3289 | wdata = &chg->irq_info[SWITCH_POWER_OK_IRQ].irq_data->storm_data; |
| 3290 | reset_storm_count(wdata); |
| 3291 | return IRQ_HANDLED; |
| 3292 | } |
| 3293 | |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 3294 | static void smblib_micro_usb_plugin(struct smb_charger *chg, bool vbus_rising) |
| 3295 | { |
Abhijeet Dharmapurikar | 11330d9 | 2017-04-17 12:15:19 -0700 | [diff] [blame] | 3296 | if (vbus_rising) { |
| 3297 | /* use the typec flag even though its not typec */ |
| 3298 | chg->typec_present = 1; |
| 3299 | } else { |
| 3300 | chg->typec_present = 0; |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 3301 | smblib_update_usb_type(chg); |
| 3302 | extcon_set_cable_state_(chg->extcon, EXTCON_USB, false); |
| 3303 | smblib_uusb_removal(chg); |
| 3304 | } |
| 3305 | } |
| 3306 | |
Abhijeet Dharmapurikar | 95c9508 | 2017-04-24 14:06:54 -0700 | [diff] [blame] | 3307 | void smblib_usb_plugin_hard_reset_locked(struct smb_charger *chg) |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 3308 | { |
Abhijeet Dharmapurikar | 95c9508 | 2017-04-24 14:06:54 -0700 | [diff] [blame] | 3309 | int rc; |
| 3310 | u8 stat; |
| 3311 | bool vbus_rising; |
Ashay Jaiswal | 2f3b0c1 | 2017-06-14 16:04:45 +0530 | [diff] [blame] | 3312 | struct smb_irq_data *data; |
| 3313 | struct storm_watch *wdata; |
Abhijeet Dharmapurikar | 95c9508 | 2017-04-24 14:06:54 -0700 | [diff] [blame] | 3314 | |
| 3315 | rc = smblib_read(chg, USBIN_BASE + INT_RT_STS_OFFSET, &stat); |
| 3316 | if (rc < 0) { |
| 3317 | smblib_err(chg, "Couldn't read USB_INT_RT_STS rc=%d\n", rc); |
| 3318 | return; |
| 3319 | } |
| 3320 | |
| 3321 | vbus_rising = (bool)(stat & USBIN_PLUGIN_RT_STS_BIT); |
| 3322 | |
Anirudh Ghayal | 36feefe | 2017-05-26 09:41:25 +0530 | [diff] [blame] | 3323 | if (vbus_rising) { |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 3324 | smblib_cc2_sink_removal_exit(chg); |
Anirudh Ghayal | 36feefe | 2017-05-26 09:41:25 +0530 | [diff] [blame] | 3325 | } else { |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 3326 | smblib_cc2_sink_removal_enter(chg); |
Ashay Jaiswal | 2f3b0c1 | 2017-06-14 16:04:45 +0530 | [diff] [blame] | 3327 | if (chg->wa_flags & BOOST_BACK_WA) { |
| 3328 | data = chg->irq_info[SWITCH_POWER_OK_IRQ].irq_data; |
| 3329 | if (data) { |
| 3330 | wdata = &data->storm_data; |
| 3331 | update_storm_count(wdata, |
| 3332 | WEAK_CHG_STORM_COUNT); |
| 3333 | vote(chg->usb_icl_votable, BOOST_BACK_VOTER, |
| 3334 | false, 0); |
| 3335 | vote(chg->usb_icl_votable, WEAK_CHARGER_VOTER, |
| 3336 | false, 0); |
| 3337 | } |
| 3338 | } |
Anirudh Ghayal | 36feefe | 2017-05-26 09:41:25 +0530 | [diff] [blame] | 3339 | } |
Abhijeet Dharmapurikar | 95c9508 | 2017-04-24 14:06:54 -0700 | [diff] [blame] | 3340 | |
| 3341 | power_supply_changed(chg->usb_psy); |
| 3342 | smblib_dbg(chg, PR_INTERRUPT, "IRQ: usbin-plugin %s\n", |
| 3343 | vbus_rising ? "attached" : "detached"); |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 3344 | } |
| 3345 | |
Ashay Jaiswal | c036167 | 2017-03-21 12:24:16 +0530 | [diff] [blame] | 3346 | #define PL_DELAY_MS 30000 |
Abhijeet Dharmapurikar | 11330d9 | 2017-04-17 12:15:19 -0700 | [diff] [blame] | 3347 | void smblib_usb_plugin_locked(struct smb_charger *chg) |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3348 | { |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3349 | int rc; |
| 3350 | u8 stat; |
Nicholas Troast | 7ec38eb | 2016-11-14 10:28:39 -0800 | [diff] [blame] | 3351 | bool vbus_rising; |
Ashay Jaiswal | 2f3b0c1 | 2017-06-14 16:04:45 +0530 | [diff] [blame] | 3352 | struct smb_irq_data *data; |
| 3353 | struct storm_watch *wdata; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3354 | |
Harry Yang | cdad2bf | 2016-10-04 17:03:56 -0700 | [diff] [blame] | 3355 | rc = smblib_read(chg, USBIN_BASE + INT_RT_STS_OFFSET, &stat); |
| 3356 | if (rc < 0) { |
Abhijeet Dharmapurikar | 11330d9 | 2017-04-17 12:15:19 -0700 | [diff] [blame] | 3357 | smblib_err(chg, "Couldn't read USB_INT_RT_STS rc=%d\n", rc); |
| 3358 | return; |
Harry Yang | cdad2bf | 2016-10-04 17:03:56 -0700 | [diff] [blame] | 3359 | } |
| 3360 | |
Nicholas Troast | 7ec38eb | 2016-11-14 10:28:39 -0800 | [diff] [blame] | 3361 | vbus_rising = (bool)(stat & USBIN_PLUGIN_RT_STS_BIT); |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 3362 | smblib_set_opt_freq_buck(chg, vbus_rising ? chg->chg_freq.freq_5V : |
| 3363 | chg->chg_freq.freq_removal); |
Harry Yang | cdad2bf | 2016-10-04 17:03:56 -0700 | [diff] [blame] | 3364 | |
Nicholas Troast | 7ec38eb | 2016-11-14 10:28:39 -0800 | [diff] [blame] | 3365 | if (vbus_rising) { |
Ashay Jaiswal | 13a1b81 | 2017-07-17 14:49:05 +0530 | [diff] [blame] | 3366 | rc = smblib_request_dpdm(chg, true); |
| 3367 | if (rc < 0) |
| 3368 | smblib_err(chg, "Couldn't to enable DPDM rc=%d\n", rc); |
Ashay Jaiswal | c036167 | 2017-03-21 12:24:16 +0530 | [diff] [blame] | 3369 | |
| 3370 | /* Schedule work to enable parallel charger */ |
| 3371 | vote(chg->awake_votable, PL_DELAY_VOTER, true, 0); |
| 3372 | schedule_delayed_work(&chg->pl_enable_work, |
| 3373 | msecs_to_jiffies(PL_DELAY_MS)); |
Harry Yang | 1ed7f16 | 2017-08-25 11:26:51 -0700 | [diff] [blame] | 3374 | /* vbus rising when APSD was disabled and PD_ACTIVE = 0 */ |
| 3375 | if (get_effective_result(chg->apsd_disable_votable) && |
| 3376 | !chg->pd_active) |
| 3377 | pr_err("APSD disabled on vbus rising without PD\n"); |
Abhijeet Dharmapurikar | 17288f2 | 2016-07-05 14:03:31 -0700 | [diff] [blame] | 3378 | } else { |
Ashay Jaiswal | 2f3b0c1 | 2017-06-14 16:04:45 +0530 | [diff] [blame] | 3379 | if (chg->wa_flags & BOOST_BACK_WA) { |
| 3380 | data = chg->irq_info[SWITCH_POWER_OK_IRQ].irq_data; |
| 3381 | if (data) { |
| 3382 | wdata = &data->storm_data; |
| 3383 | update_storm_count(wdata, |
| 3384 | WEAK_CHG_STORM_COUNT); |
| 3385 | vote(chg->usb_icl_votable, BOOST_BACK_VOTER, |
| 3386 | false, 0); |
| 3387 | vote(chg->usb_icl_votable, WEAK_CHARGER_VOTER, |
| 3388 | false, 0); |
| 3389 | } |
| 3390 | } |
Nicholas Troast | abedaf7 | 2016-09-16 11:07:45 -0700 | [diff] [blame] | 3391 | |
Ashay Jaiswal | 13a1b81 | 2017-07-17 14:49:05 +0530 | [diff] [blame] | 3392 | rc = smblib_request_dpdm(chg, false); |
| 3393 | if (rc < 0) |
| 3394 | smblib_err(chg, "Couldn't disable DPDM rc=%d\n", rc); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3395 | } |
| 3396 | |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 3397 | if (chg->micro_usb_mode) |
| 3398 | smblib_micro_usb_plugin(chg, vbus_rising); |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 3399 | |
Nicholas Troast | 62d8662 | 2016-09-22 11:41:33 -0700 | [diff] [blame] | 3400 | power_supply_changed(chg->usb_psy); |
Abhijeet Dharmapurikar | 11330d9 | 2017-04-17 12:15:19 -0700 | [diff] [blame] | 3401 | smblib_dbg(chg, PR_INTERRUPT, "IRQ: usbin-plugin %s\n", |
| 3402 | vbus_rising ? "attached" : "detached"); |
| 3403 | } |
| 3404 | |
| 3405 | irqreturn_t smblib_handle_usb_plugin(int irq, void *data) |
| 3406 | { |
| 3407 | struct smb_irq_data *irq_data = data; |
| 3408 | struct smb_charger *chg = irq_data->parent_data; |
| 3409 | |
| 3410 | mutex_lock(&chg->lock); |
Abhijeet Dharmapurikar | 95c9508 | 2017-04-24 14:06:54 -0700 | [diff] [blame] | 3411 | if (chg->pd_hard_reset) |
| 3412 | smblib_usb_plugin_hard_reset_locked(chg); |
| 3413 | else |
| 3414 | smblib_usb_plugin_locked(chg); |
Abhijeet Dharmapurikar | 11330d9 | 2017-04-17 12:15:19 -0700 | [diff] [blame] | 3415 | mutex_unlock(&chg->lock); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3416 | return IRQ_HANDLED; |
| 3417 | } |
| 3418 | |
Abhijeet Dharmapurikar | c0b0f59 | 2016-10-14 10:55:42 -0700 | [diff] [blame] | 3419 | #define USB_WEAK_INPUT_UA 1400000 |
Ashay Jaiswal | 4d82578 | 2017-02-18 10:11:34 +0530 | [diff] [blame] | 3420 | #define ICL_CHANGE_DELAY_MS 1000 |
Harry Yang | 6fe72ab | 2016-06-14 16:21:39 -0700 | [diff] [blame] | 3421 | irqreturn_t smblib_handle_icl_change(int irq, void *data) |
| 3422 | { |
Ashay Jaiswal | 4d82578 | 2017-02-18 10:11:34 +0530 | [diff] [blame] | 3423 | u8 stat; |
| 3424 | int rc, settled_ua, delay = ICL_CHANGE_DELAY_MS; |
Harry Yang | 6fe72ab | 2016-06-14 16:21:39 -0700 | [diff] [blame] | 3425 | struct smb_irq_data *irq_data = data; |
| 3426 | struct smb_charger *chg = irq_data->parent_data; |
| 3427 | |
Nicholas Troast | bb9e1a3 | 2017-02-09 10:57:28 -0800 | [diff] [blame] | 3428 | if (chg->mode == PARALLEL_MASTER) { |
Ashay Jaiswal | 4d82578 | 2017-02-18 10:11:34 +0530 | [diff] [blame] | 3429 | rc = smblib_read(chg, AICL_STATUS_REG, &stat); |
| 3430 | if (rc < 0) { |
| 3431 | smblib_err(chg, "Couldn't read AICL_STATUS rc=%d\n", |
| 3432 | rc); |
| 3433 | return IRQ_HANDLED; |
| 3434 | } |
| 3435 | |
| 3436 | rc = smblib_get_charge_param(chg, &chg->param.icl_stat, |
| 3437 | &settled_ua); |
| 3438 | if (rc < 0) { |
| 3439 | smblib_err(chg, "Couldn't get ICL status rc=%d\n", rc); |
| 3440 | return IRQ_HANDLED; |
| 3441 | } |
| 3442 | |
| 3443 | /* If AICL settled then schedule work now */ |
| 3444 | if ((settled_ua == get_effective_result(chg->usb_icl_votable)) |
| 3445 | || (stat & AICL_DONE_BIT)) |
| 3446 | delay = 0; |
| 3447 | |
Ashay Jaiswal | ac85486 | 2017-03-06 23:58:55 +0530 | [diff] [blame] | 3448 | cancel_delayed_work_sync(&chg->icl_change_work); |
Ashay Jaiswal | 4d82578 | 2017-02-18 10:11:34 +0530 | [diff] [blame] | 3449 | schedule_delayed_work(&chg->icl_change_work, |
| 3450 | msecs_to_jiffies(delay)); |
Nicholas Troast | bb9e1a3 | 2017-02-09 10:57:28 -0800 | [diff] [blame] | 3451 | } |
Harry Yang | 1d1034c | 2016-06-15 12:09:42 -0700 | [diff] [blame] | 3452 | |
Harry Yang | 6fe72ab | 2016-06-14 16:21:39 -0700 | [diff] [blame] | 3453 | return IRQ_HANDLED; |
| 3454 | } |
| 3455 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3456 | static void smblib_handle_slow_plugin_timeout(struct smb_charger *chg, |
| 3457 | bool rising) |
| 3458 | { |
| 3459 | smblib_dbg(chg, PR_INTERRUPT, "IRQ: slow-plugin-timeout %s\n", |
| 3460 | rising ? "rising" : "falling"); |
| 3461 | } |
| 3462 | |
| 3463 | static void smblib_handle_sdp_enumeration_done(struct smb_charger *chg, |
| 3464 | bool rising) |
| 3465 | { |
| 3466 | smblib_dbg(chg, PR_INTERRUPT, "IRQ: sdp-enumeration-done %s\n", |
| 3467 | rising ? "rising" : "falling"); |
| 3468 | } |
| 3469 | |
Harry Yang | cdad2bf | 2016-10-04 17:03:56 -0700 | [diff] [blame] | 3470 | #define QC3_PULSES_FOR_6V 5 |
| 3471 | #define QC3_PULSES_FOR_9V 20 |
| 3472 | #define QC3_PULSES_FOR_12V 35 |
| 3473 | static void smblib_hvdcp_adaptive_voltage_change(struct smb_charger *chg) |
| 3474 | { |
| 3475 | int rc; |
| 3476 | u8 stat; |
| 3477 | int pulses; |
| 3478 | |
Fenglin Wu | ef4730e | 2017-01-11 18:16:25 +0800 | [diff] [blame] | 3479 | power_supply_changed(chg->usb_main_psy); |
Fenglin Wu | 80826e0 | 2017-04-25 21:45:08 +0800 | [diff] [blame] | 3480 | if (chg->real_charger_type == POWER_SUPPLY_TYPE_USB_HVDCP) { |
Harry Yang | cdad2bf | 2016-10-04 17:03:56 -0700 | [diff] [blame] | 3481 | rc = smblib_read(chg, QC_CHANGE_STATUS_REG, &stat); |
| 3482 | if (rc < 0) { |
| 3483 | smblib_err(chg, |
| 3484 | "Couldn't read QC_CHANGE_STATUS rc=%d\n", rc); |
| 3485 | return; |
| 3486 | } |
| 3487 | |
| 3488 | switch (stat & QC_2P0_STATUS_MASK) { |
| 3489 | case QC_5V_BIT: |
Anirudh Ghayal | 4da3df9 | 2017-01-25 18:55:57 +0530 | [diff] [blame] | 3490 | smblib_set_opt_freq_buck(chg, |
| 3491 | chg->chg_freq.freq_5V); |
Harry Yang | cdad2bf | 2016-10-04 17:03:56 -0700 | [diff] [blame] | 3492 | break; |
| 3493 | case QC_9V_BIT: |
Anirudh Ghayal | 4da3df9 | 2017-01-25 18:55:57 +0530 | [diff] [blame] | 3494 | smblib_set_opt_freq_buck(chg, |
| 3495 | chg->chg_freq.freq_9V); |
Harry Yang | cdad2bf | 2016-10-04 17:03:56 -0700 | [diff] [blame] | 3496 | break; |
| 3497 | case QC_12V_BIT: |
Anirudh Ghayal | 4da3df9 | 2017-01-25 18:55:57 +0530 | [diff] [blame] | 3498 | smblib_set_opt_freq_buck(chg, |
| 3499 | chg->chg_freq.freq_12V); |
Harry Yang | cdad2bf | 2016-10-04 17:03:56 -0700 | [diff] [blame] | 3500 | break; |
| 3501 | default: |
Anirudh Ghayal | 4da3df9 | 2017-01-25 18:55:57 +0530 | [diff] [blame] | 3502 | smblib_set_opt_freq_buck(chg, |
| 3503 | chg->chg_freq.freq_removal); |
Harry Yang | cdad2bf | 2016-10-04 17:03:56 -0700 | [diff] [blame] | 3504 | break; |
| 3505 | } |
| 3506 | } |
| 3507 | |
Fenglin Wu | 80826e0 | 2017-04-25 21:45:08 +0800 | [diff] [blame] | 3508 | if (chg->real_charger_type == POWER_SUPPLY_TYPE_USB_HVDCP_3) { |
Ashay Jaiswal | cda0d1c | 2017-05-15 17:15:29 +0530 | [diff] [blame] | 3509 | rc = smblib_get_pulse_cnt(chg, &pulses); |
Harry Yang | cdad2bf | 2016-10-04 17:03:56 -0700 | [diff] [blame] | 3510 | if (rc < 0) { |
| 3511 | smblib_err(chg, |
| 3512 | "Couldn't read QC_PULSE_COUNT rc=%d\n", rc); |
| 3513 | return; |
| 3514 | } |
Harry Yang | cdad2bf | 2016-10-04 17:03:56 -0700 | [diff] [blame] | 3515 | |
| 3516 | if (pulses < QC3_PULSES_FOR_6V) |
Anirudh Ghayal | 4da3df9 | 2017-01-25 18:55:57 +0530 | [diff] [blame] | 3517 | smblib_set_opt_freq_buck(chg, |
| 3518 | chg->chg_freq.freq_5V); |
Harry Yang | cdad2bf | 2016-10-04 17:03:56 -0700 | [diff] [blame] | 3519 | else if (pulses < QC3_PULSES_FOR_9V) |
Anirudh Ghayal | 4da3df9 | 2017-01-25 18:55:57 +0530 | [diff] [blame] | 3520 | smblib_set_opt_freq_buck(chg, |
| 3521 | chg->chg_freq.freq_6V_8V); |
Harry Yang | cdad2bf | 2016-10-04 17:03:56 -0700 | [diff] [blame] | 3522 | else if (pulses < QC3_PULSES_FOR_12V) |
Anirudh Ghayal | 4da3df9 | 2017-01-25 18:55:57 +0530 | [diff] [blame] | 3523 | smblib_set_opt_freq_buck(chg, |
| 3524 | chg->chg_freq.freq_9V); |
Harry Yang | cdad2bf | 2016-10-04 17:03:56 -0700 | [diff] [blame] | 3525 | else |
Anirudh Ghayal | 4da3df9 | 2017-01-25 18:55:57 +0530 | [diff] [blame] | 3526 | smblib_set_opt_freq_buck(chg, |
| 3527 | chg->chg_freq.freq_12V); |
Harry Yang | cdad2bf | 2016-10-04 17:03:56 -0700 | [diff] [blame] | 3528 | } |
| 3529 | } |
| 3530 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3531 | /* triggers when HVDCP 3.0 authentication has finished */ |
| 3532 | static void smblib_handle_hvdcp_3p0_auth_done(struct smb_charger *chg, |
| 3533 | bool rising) |
| 3534 | { |
| 3535 | const struct apsd_result *apsd_result; |
Harry Yang | cdad2bf | 2016-10-04 17:03:56 -0700 | [diff] [blame] | 3536 | int rc; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3537 | |
| 3538 | if (!rising) |
| 3539 | return; |
| 3540 | |
Ashay Jaiswal | 67ec707 | 2017-02-16 14:14:58 +0530 | [diff] [blame] | 3541 | if (chg->wa_flags & QC_AUTH_INTERRUPT_WA_BIT) { |
| 3542 | /* |
| 3543 | * Disable AUTH_IRQ_EN_CFG_BIT to receive adapter voltage |
| 3544 | * change interrupt. |
| 3545 | */ |
| 3546 | rc = smblib_masked_write(chg, |
| 3547 | USBIN_SOURCE_CHANGE_INTRPT_ENB_REG, |
| 3548 | AUTH_IRQ_EN_CFG_BIT, 0); |
| 3549 | if (rc < 0) |
| 3550 | smblib_err(chg, |
| 3551 | "Couldn't enable QC auth setting rc=%d\n", rc); |
| 3552 | } |
Harry Yang | cdad2bf | 2016-10-04 17:03:56 -0700 | [diff] [blame] | 3553 | |
Harry Yang | aba1f5f | 2016-09-28 10:47:29 -0700 | [diff] [blame] | 3554 | if (chg->mode == PARALLEL_MASTER) |
| 3555 | vote(chg->pl_enable_votable_indirect, USBIN_V_VOTER, true, 0); |
| 3556 | |
Ashay Jaiswal | ac85486 | 2017-03-06 23:58:55 +0530 | [diff] [blame] | 3557 | /* the APSD done handler will set the USB supply type */ |
| 3558 | apsd_result = smblib_get_apsd_result(chg); |
| 3559 | if (get_effective_result(chg->hvdcp_hw_inov_dis_votable)) { |
| 3560 | if (apsd_result->pst == POWER_SUPPLY_TYPE_USB_HVDCP) { |
| 3561 | /* force HVDCP2 to 9V if INOV is disabled */ |
| 3562 | rc = smblib_masked_write(chg, CMD_HVDCP_2_REG, |
| 3563 | FORCE_9V_BIT, FORCE_9V_BIT); |
| 3564 | if (rc < 0) |
| 3565 | smblib_err(chg, |
| 3566 | "Couldn't force 9V HVDCP rc=%d\n", rc); |
| 3567 | } |
| 3568 | } |
| 3569 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3570 | smblib_dbg(chg, PR_INTERRUPT, "IRQ: hvdcp-3p0-auth-done rising; %s detected\n", |
| 3571 | apsd_result->name); |
| 3572 | } |
| 3573 | |
Harry Yang | 1369b7a | 2016-09-27 15:59:50 -0700 | [diff] [blame] | 3574 | static void smblib_handle_hvdcp_check_timeout(struct smb_charger *chg, |
| 3575 | bool rising, bool qc_charger) |
| 3576 | { |
Ashay Jaiswal | 7b6eb96 | 2017-04-26 14:34:29 +0530 | [diff] [blame] | 3577 | const struct apsd_result *apsd_result = smblib_get_apsd_result(chg); |
Abhijeet Dharmapurikar | 9567087 | 2017-02-09 22:55:51 +0530 | [diff] [blame] | 3578 | |
Abhijeet Dharmapurikar | 4b13c6e | 2016-10-05 15:15:10 -0700 | [diff] [blame] | 3579 | /* Hold off PD only until hvdcp 2.0 detection timeout */ |
Abhijeet Dharmapurikar | 716cd96 | 2016-10-14 12:50:37 -0700 | [diff] [blame] | 3580 | if (rising) { |
Abhijeet Dharmapurikar | 4b13c6e | 2016-10-05 15:15:10 -0700 | [diff] [blame] | 3581 | vote(chg->pd_disallowed_votable_indirect, HVDCP_TIMEOUT_VOTER, |
Abhijeet Dharmapurikar | 716cd96 | 2016-10-14 12:50:37 -0700 | [diff] [blame] | 3582 | false, 0); |
Abhijeet Dharmapurikar | 74021fc | 2017-02-06 18:39:19 -0800 | [diff] [blame] | 3583 | |
Harry Yang | 4bf7d96 | 2017-03-13 16:51:43 -0700 | [diff] [blame] | 3584 | /* enable HDC and ICL irq for QC2/3 charger */ |
| 3585 | if (qc_charger) |
| 3586 | vote(chg->usb_irq_enable_votable, QC_VOTER, true, 0); |
| 3587 | |
Abhijeet Dharmapurikar | 74021fc | 2017-02-06 18:39:19 -0800 | [diff] [blame] | 3588 | /* |
| 3589 | * HVDCP detection timeout done |
| 3590 | * If adapter is not QC2.0/QC3.0 - it is a plain old DCP. |
| 3591 | */ |
| 3592 | if (!qc_charger && (apsd_result->bit & DCP_CHARGER_BIT)) |
| 3593 | /* enforce DCP ICL if specified */ |
| 3594 | vote(chg->usb_icl_votable, DCP_VOTER, |
| 3595 | chg->dcp_icl_ua != -EINVAL, chg->dcp_icl_ua); |
Abhijeet Dharmapurikar | 716cd96 | 2016-10-14 12:50:37 -0700 | [diff] [blame] | 3596 | } |
Harry Yang | 1369b7a | 2016-09-27 15:59:50 -0700 | [diff] [blame] | 3597 | |
| 3598 | smblib_dbg(chg, PR_INTERRUPT, "IRQ: smblib_handle_hvdcp_check_timeout %s\n", |
| 3599 | rising ? "rising" : "falling"); |
| 3600 | } |
| 3601 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3602 | /* triggers when HVDCP is detected */ |
| 3603 | static void smblib_handle_hvdcp_detect_done(struct smb_charger *chg, |
| 3604 | bool rising) |
| 3605 | { |
| 3606 | if (!rising) |
| 3607 | return; |
| 3608 | |
| 3609 | /* the APSD done handler will set the USB supply type */ |
| 3610 | cancel_delayed_work_sync(&chg->hvdcp_detect_work); |
| 3611 | smblib_dbg(chg, PR_INTERRUPT, "IRQ: hvdcp-detect-done %s\n", |
| 3612 | rising ? "rising" : "falling"); |
| 3613 | } |
| 3614 | |
Nicholas Troast | f9e4499 | 2017-03-14 09:06:56 -0700 | [diff] [blame] | 3615 | static void smblib_force_legacy_icl(struct smb_charger *chg, int pst) |
| 3616 | { |
Ashay Jaiswal | dd08c62 | 2017-06-29 16:25:23 +0530 | [diff] [blame] | 3617 | int typec_mode; |
| 3618 | int rp_ua; |
| 3619 | |
Abhijeet Dharmapurikar | 3c93db3 | 2017-04-17 17:36:14 -0700 | [diff] [blame] | 3620 | /* while PD is active it should have complete ICL control */ |
| 3621 | if (chg->pd_active) |
| 3622 | return; |
| 3623 | |
Nicholas Troast | f9e4499 | 2017-03-14 09:06:56 -0700 | [diff] [blame] | 3624 | switch (pst) { |
| 3625 | case POWER_SUPPLY_TYPE_USB: |
| 3626 | /* |
| 3627 | * USB_PSY will vote to increase the current to 500/900mA once |
| 3628 | * enumeration is done. Ensure that USB_PSY has at least voted |
| 3629 | * for 100mA before releasing the LEGACY_UNKNOWN vote |
| 3630 | */ |
| 3631 | if (!is_client_vote_enabled(chg->usb_icl_votable, |
| 3632 | USB_PSY_VOTER)) |
| 3633 | vote(chg->usb_icl_votable, USB_PSY_VOTER, true, 100000); |
| 3634 | vote(chg->usb_icl_votable, LEGACY_UNKNOWN_VOTER, false, 0); |
| 3635 | break; |
| 3636 | case POWER_SUPPLY_TYPE_USB_CDP: |
| 3637 | vote(chg->usb_icl_votable, LEGACY_UNKNOWN_VOTER, true, 1500000); |
| 3638 | break; |
| 3639 | case POWER_SUPPLY_TYPE_USB_DCP: |
Ashay Jaiswal | dd08c62 | 2017-06-29 16:25:23 +0530 | [diff] [blame] | 3640 | typec_mode = smblib_get_prop_typec_mode(chg); |
| 3641 | rp_ua = get_rp_based_dcp_current(chg, typec_mode); |
| 3642 | vote(chg->usb_icl_votable, LEGACY_UNKNOWN_VOTER, true, rp_ua); |
Nicholas Troast | f9e4499 | 2017-03-14 09:06:56 -0700 | [diff] [blame] | 3643 | break; |
Anirudh Ghayal | 1121f5d | 2017-07-26 20:26:20 +0530 | [diff] [blame] | 3644 | case POWER_SUPPLY_TYPE_USB_FLOAT: |
| 3645 | /* |
| 3646 | * limit ICL to 100mA, the USB driver will enumerate to check |
| 3647 | * if this is a SDP and appropriately set the current |
| 3648 | */ |
| 3649 | vote(chg->usb_icl_votable, LEGACY_UNKNOWN_VOTER, true, 100000); |
| 3650 | break; |
Nicholas Troast | f9e4499 | 2017-03-14 09:06:56 -0700 | [diff] [blame] | 3651 | case POWER_SUPPLY_TYPE_USB_HVDCP: |
| 3652 | case POWER_SUPPLY_TYPE_USB_HVDCP_3: |
| 3653 | vote(chg->usb_icl_votable, LEGACY_UNKNOWN_VOTER, true, 3000000); |
| 3654 | break; |
| 3655 | default: |
| 3656 | smblib_err(chg, "Unknown APSD %d; forcing 500mA\n", pst); |
| 3657 | vote(chg->usb_icl_votable, LEGACY_UNKNOWN_VOTER, true, 500000); |
| 3658 | break; |
| 3659 | } |
| 3660 | } |
| 3661 | |
Ankit Sharma | de321e1 | 2017-09-01 20:17:45 +0530 | [diff] [blame] | 3662 | static void smblib_notify_extcon_props(struct smb_charger *chg, int id) |
| 3663 | { |
| 3664 | union extcon_property_value val; |
| 3665 | union power_supply_propval prop_val; |
| 3666 | |
| 3667 | smblib_get_prop_typec_cc_orientation(chg, &prop_val); |
| 3668 | val.intval = ((prop_val.intval == 2) ? 1 : 0); |
| 3669 | extcon_set_property(chg->extcon, id, |
| 3670 | EXTCON_PROP_USB_TYPEC_POLARITY, val); |
| 3671 | |
| 3672 | val.intval = true; |
| 3673 | extcon_set_property(chg->extcon, id, |
| 3674 | EXTCON_PROP_USB_SS, val); |
| 3675 | } |
| 3676 | |
| 3677 | static void smblib_notify_device_mode(struct smb_charger *chg, bool enable) |
| 3678 | { |
| 3679 | if (enable) |
| 3680 | smblib_notify_extcon_props(chg, EXTCON_USB); |
| 3681 | |
| 3682 | extcon_set_state_sync(chg->extcon, EXTCON_USB, enable); |
| 3683 | } |
| 3684 | |
| 3685 | static void smblib_notify_usb_host(struct smb_charger *chg, bool enable) |
| 3686 | { |
| 3687 | if (enable) |
| 3688 | smblib_notify_extcon_props(chg, EXTCON_USB_HOST); |
| 3689 | |
| 3690 | extcon_set_state_sync(chg->extcon, EXTCON_USB_HOST, enable); |
| 3691 | } |
| 3692 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3693 | #define HVDCP_DET_MS 2500 |
| 3694 | static void smblib_handle_apsd_done(struct smb_charger *chg, bool rising) |
| 3695 | { |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3696 | const struct apsd_result *apsd_result; |
| 3697 | |
| 3698 | if (!rising) |
| 3699 | return; |
| 3700 | |
Abhijeet Dharmapurikar | b959887 | 2016-10-17 16:58:58 -0700 | [diff] [blame] | 3701 | apsd_result = smblib_update_usb_type(chg); |
Nicholas Troast | f9e4499 | 2017-03-14 09:06:56 -0700 | [diff] [blame] | 3702 | |
Abhijeet Dharmapurikar | 3c93db3 | 2017-04-17 17:36:14 -0700 | [diff] [blame] | 3703 | if (!chg->typec_legacy_valid) |
Nicholas Troast | f9e4499 | 2017-03-14 09:06:56 -0700 | [diff] [blame] | 3704 | smblib_force_legacy_icl(chg, apsd_result->pst); |
| 3705 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3706 | switch (apsd_result->bit) { |
| 3707 | case SDP_CHARGER_BIT: |
| 3708 | case CDP_CHARGER_BIT: |
Ashay Jaiswal | 4aa2c0c | 2016-12-07 19:39:38 +0530 | [diff] [blame] | 3709 | if (chg->micro_usb_mode) |
| 3710 | extcon_set_cable_state_(chg->extcon, EXTCON_USB, |
| 3711 | true); |
Abhijeet Dharmapurikar | bb9505f | 2017-03-22 18:31:28 -0700 | [diff] [blame] | 3712 | /* if not DCP then no hvdcp timeout happens. Enable pd here */ |
| 3713 | vote(chg->pd_disallowed_votable_indirect, HVDCP_TIMEOUT_VOTER, |
| 3714 | false, 0); |
Ankit Sharma | de321e1 | 2017-09-01 20:17:45 +0530 | [diff] [blame] | 3715 | if (chg->use_extcon) |
| 3716 | smblib_notify_device_mode(chg, true); |
Abhijeet Dharmapurikar | bb9505f | 2017-03-22 18:31:28 -0700 | [diff] [blame] | 3717 | break; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3718 | case OCP_CHARGER_BIT: |
| 3719 | case FLOAT_CHARGER_BIT: |
Ashay Jaiswal | c036167 | 2017-03-21 12:24:16 +0530 | [diff] [blame] | 3720 | /* if not DCP then no hvdcp timeout happens, Enable pd here. */ |
Abhijeet Dharmapurikar | 4b13c6e | 2016-10-05 15:15:10 -0700 | [diff] [blame] | 3721 | vote(chg->pd_disallowed_votable_indirect, HVDCP_TIMEOUT_VOTER, |
| 3722 | false, 0); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3723 | break; |
| 3724 | case DCP_CHARGER_BIT: |
Harry Yang | 1369b7a | 2016-09-27 15:59:50 -0700 | [diff] [blame] | 3725 | if (chg->wa_flags & QC_CHARGER_DETECTION_WA_BIT) |
| 3726 | schedule_delayed_work(&chg->hvdcp_detect_work, |
| 3727 | msecs_to_jiffies(HVDCP_DET_MS)); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3728 | break; |
| 3729 | default: |
| 3730 | break; |
| 3731 | } |
| 3732 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3733 | smblib_dbg(chg, PR_INTERRUPT, "IRQ: apsd-done rising; %s detected\n", |
| 3734 | apsd_result->name); |
| 3735 | } |
| 3736 | |
| 3737 | irqreturn_t smblib_handle_usb_source_change(int irq, void *data) |
| 3738 | { |
| 3739 | struct smb_irq_data *irq_data = data; |
| 3740 | struct smb_charger *chg = irq_data->parent_data; |
| 3741 | int rc = 0; |
| 3742 | u8 stat; |
| 3743 | |
| 3744 | rc = smblib_read(chg, APSD_STATUS_REG, &stat); |
| 3745 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 3746 | smblib_err(chg, "Couldn't read APSD_STATUS rc=%d\n", rc); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3747 | return IRQ_HANDLED; |
| 3748 | } |
| 3749 | smblib_dbg(chg, PR_REGISTER, "APSD_STATUS = 0x%02x\n", stat); |
| 3750 | |
Ashay Jaiswal | 8507aa5 | 2017-04-14 09:42:32 +0530 | [diff] [blame] | 3751 | if (chg->micro_usb_mode && (stat & APSD_DTC_STATUS_DONE_BIT) |
| 3752 | && !chg->uusb_apsd_rerun_done) { |
| 3753 | /* |
| 3754 | * Force re-run APSD to handle slow insertion related |
| 3755 | * charger-mis-detection. |
| 3756 | */ |
| 3757 | chg->uusb_apsd_rerun_done = true; |
| 3758 | smblib_rerun_apsd(chg); |
| 3759 | return IRQ_HANDLED; |
| 3760 | } |
| 3761 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3762 | smblib_handle_apsd_done(chg, |
| 3763 | (bool)(stat & APSD_DTC_STATUS_DONE_BIT)); |
| 3764 | |
| 3765 | smblib_handle_hvdcp_detect_done(chg, |
| 3766 | (bool)(stat & QC_CHARGER_BIT)); |
| 3767 | |
Harry Yang | 1369b7a | 2016-09-27 15:59:50 -0700 | [diff] [blame] | 3768 | smblib_handle_hvdcp_check_timeout(chg, |
| 3769 | (bool)(stat & HVDCP_CHECK_TIMEOUT_BIT), |
| 3770 | (bool)(stat & QC_CHARGER_BIT)); |
| 3771 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3772 | smblib_handle_hvdcp_3p0_auth_done(chg, |
| 3773 | (bool)(stat & QC_AUTH_DONE_STATUS_BIT)); |
| 3774 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3775 | smblib_handle_sdp_enumeration_done(chg, |
| 3776 | (bool)(stat & ENUMERATION_DONE_BIT)); |
| 3777 | |
| 3778 | smblib_handle_slow_plugin_timeout(chg, |
| 3779 | (bool)(stat & SLOW_PLUGIN_TIMEOUT_BIT)); |
| 3780 | |
Harry Yang | cdad2bf | 2016-10-04 17:03:56 -0700 | [diff] [blame] | 3781 | smblib_hvdcp_adaptive_voltage_change(chg); |
| 3782 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3783 | power_supply_changed(chg->usb_psy); |
| 3784 | |
Abhijeet Dharmapurikar | 5e0bfbe | 2017-02-12 19:16:15 -0800 | [diff] [blame] | 3785 | rc = smblib_read(chg, APSD_STATUS_REG, &stat); |
| 3786 | if (rc < 0) { |
| 3787 | smblib_err(chg, "Couldn't read APSD_STATUS rc=%d\n", rc); |
| 3788 | return IRQ_HANDLED; |
| 3789 | } |
| 3790 | smblib_dbg(chg, PR_REGISTER, "APSD_STATUS = 0x%02x\n", stat); |
| 3791 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3792 | return IRQ_HANDLED; |
| 3793 | } |
| 3794 | |
Abhijeet Dharmapurikar | 99ca045 | 2016-10-13 09:50:41 -0700 | [diff] [blame] | 3795 | static void typec_sink_insertion(struct smb_charger *chg) |
| 3796 | { |
| 3797 | /* when a sink is inserted we should not wait on hvdcp timeout to |
| 3798 | * enable pd |
| 3799 | */ |
| 3800 | vote(chg->pd_disallowed_votable_indirect, HVDCP_TIMEOUT_VOTER, |
| 3801 | false, 0); |
Ankit Sharma | de321e1 | 2017-09-01 20:17:45 +0530 | [diff] [blame] | 3802 | if (chg->use_extcon) { |
| 3803 | smblib_notify_usb_host(chg, true); |
| 3804 | chg->otg_present = true; |
| 3805 | } |
Abhijeet Dharmapurikar | 99ca045 | 2016-10-13 09:50:41 -0700 | [diff] [blame] | 3806 | } |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3807 | |
Harry Yang | d89ff1f | 2016-12-05 14:59:11 -0800 | [diff] [blame] | 3808 | static void typec_sink_removal(struct smb_charger *chg) |
| 3809 | { |
Anirudh Ghayal | 4da3df9 | 2017-01-25 18:55:57 +0530 | [diff] [blame] | 3810 | smblib_set_charge_param(chg, &chg->param.freq_boost, |
| 3811 | chg->chg_freq.freq_above_otg_threshold); |
Harry Yang | d89ff1f | 2016-12-05 14:59:11 -0800 | [diff] [blame] | 3812 | chg->boost_current_ua = 0; |
| 3813 | } |
| 3814 | |
Abhijeet Dharmapurikar | 99ca045 | 2016-10-13 09:50:41 -0700 | [diff] [blame] | 3815 | static void smblib_handle_typec_removal(struct smb_charger *chg) |
| 3816 | { |
Nicholas Troast | fe74c59 | 2017-03-14 09:20:55 -0700 | [diff] [blame] | 3817 | int rc; |
Ashay Jaiswal | 2f3b0c1 | 2017-06-14 16:04:45 +0530 | [diff] [blame] | 3818 | struct smb_irq_data *data; |
| 3819 | struct storm_watch *wdata; |
Nicholas Troast | fe74c59 | 2017-03-14 09:20:55 -0700 | [diff] [blame] | 3820 | |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 3821 | chg->cc2_detach_wa_active = false; |
| 3822 | |
Ashay Jaiswal | 13a1b81 | 2017-07-17 14:49:05 +0530 | [diff] [blame] | 3823 | rc = smblib_request_dpdm(chg, false); |
| 3824 | if (rc < 0) |
| 3825 | smblib_err(chg, "Couldn't disable DPDM rc=%d\n", rc); |
Anirudh Ghayal | 36feefe | 2017-05-26 09:41:25 +0530 | [diff] [blame] | 3826 | |
Ashay Jaiswal | 2f3b0c1 | 2017-06-14 16:04:45 +0530 | [diff] [blame] | 3827 | if (chg->wa_flags & BOOST_BACK_WA) { |
| 3828 | data = chg->irq_info[SWITCH_POWER_OK_IRQ].irq_data; |
| 3829 | if (data) { |
| 3830 | wdata = &data->storm_data; |
| 3831 | update_storm_count(wdata, WEAK_CHG_STORM_COUNT); |
| 3832 | vote(chg->usb_icl_votable, BOOST_BACK_VOTER, false, 0); |
| 3833 | vote(chg->usb_icl_votable, WEAK_CHARGER_VOTER, |
| 3834 | false, 0); |
| 3835 | } |
| 3836 | } |
Anirudh Ghayal | 36feefe | 2017-05-26 09:41:25 +0530 | [diff] [blame] | 3837 | |
Nicholas Troast | 8c28e0f | 2017-03-22 14:44:49 -0700 | [diff] [blame] | 3838 | /* reset APSD voters */ |
| 3839 | vote(chg->apsd_disable_votable, PD_HARD_RESET_VOTER, false, 0); |
| 3840 | vote(chg->apsd_disable_votable, PD_VOTER, false, 0); |
Ashay Jaiswal | c036167 | 2017-03-21 12:24:16 +0530 | [diff] [blame] | 3841 | |
Nicholas Troast | 8c28e0f | 2017-03-22 14:44:49 -0700 | [diff] [blame] | 3842 | cancel_delayed_work_sync(&chg->pl_enable_work); |
| 3843 | cancel_delayed_work_sync(&chg->hvdcp_detect_work); |
| 3844 | |
| 3845 | /* reset input current limit voters */ |
| 3846 | vote(chg->usb_icl_votable, LEGACY_UNKNOWN_VOTER, true, 100000); |
| 3847 | vote(chg->usb_icl_votable, PD_VOTER, false, 0); |
| 3848 | vote(chg->usb_icl_votable, USB_PSY_VOTER, false, 0); |
| 3849 | vote(chg->usb_icl_votable, DCP_VOTER, false, 0); |
| 3850 | vote(chg->usb_icl_votable, PL_USBIN_USBIN_VOTER, false, 0); |
Ashay Jaiswal | ae23a04 | 2017-05-18 15:28:31 +0530 | [diff] [blame] | 3851 | vote(chg->usb_icl_votable, SW_QC3_VOTER, false, 0); |
Abhijeet Dharmapurikar | 66d2c44 | 2017-07-12 11:55:36 -0700 | [diff] [blame] | 3852 | vote(chg->usb_icl_votable, OTG_VOTER, false, 0); |
Nicholas Troast | 8c28e0f | 2017-03-22 14:44:49 -0700 | [diff] [blame] | 3853 | |
| 3854 | /* reset hvdcp voters */ |
| 3855 | vote(chg->hvdcp_disable_votable_indirect, VBUS_CC_SHORT_VOTER, true, 0); |
| 3856 | vote(chg->hvdcp_disable_votable_indirect, PD_INACTIVE_VOTER, true, 0); |
| 3857 | |
| 3858 | /* reset power delivery voters */ |
| 3859 | vote(chg->pd_allowed_votable, PD_VOTER, false, 0); |
Abhijeet Dharmapurikar | 99ca045 | 2016-10-13 09:50:41 -0700 | [diff] [blame] | 3860 | vote(chg->pd_disallowed_votable_indirect, CC_DETACHED_VOTER, true, 0); |
| 3861 | vote(chg->pd_disallowed_votable_indirect, HVDCP_TIMEOUT_VOTER, true, 0); |
Nicholas Troast | 8c28e0f | 2017-03-22 14:44:49 -0700 | [diff] [blame] | 3862 | |
| 3863 | /* reset usb irq voters */ |
Harry Yang | 4bf7d96 | 2017-03-13 16:51:43 -0700 | [diff] [blame] | 3864 | vote(chg->usb_irq_enable_votable, PD_VOTER, false, 0); |
| 3865 | vote(chg->usb_irq_enable_votable, QC_VOTER, false, 0); |
Abhijeet Dharmapurikar | 99ca045 | 2016-10-13 09:50:41 -0700 | [diff] [blame] | 3866 | |
Nicholas Troast | 8c28e0f | 2017-03-22 14:44:49 -0700 | [diff] [blame] | 3867 | /* reset parallel voters */ |
| 3868 | vote(chg->pl_disable_votable, PL_DELAY_VOTER, true, 0); |
Harry Yang | a796cf7 | 2017-07-19 19:26:45 -0700 | [diff] [blame] | 3869 | vote(chg->pl_disable_votable, FCC_CHANGE_VOTER, false, 0); |
Nicholas Troast | 8c28e0f | 2017-03-22 14:44:49 -0700 | [diff] [blame] | 3870 | vote(chg->pl_enable_votable_indirect, USBIN_I_VOTER, false, 0); |
| 3871 | vote(chg->pl_enable_votable_indirect, USBIN_V_VOTER, false, 0); |
| 3872 | vote(chg->awake_votable, PL_DELAY_VOTER, false, 0); |
Harry Yang | 1d1034c | 2016-06-15 12:09:42 -0700 | [diff] [blame] | 3873 | |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 3874 | chg->vconn_attempts = 0; |
| 3875 | chg->otg_attempts = 0; |
Ashay Jaiswal | 6d308da | 2017-02-18 09:59:23 +0530 | [diff] [blame] | 3876 | chg->pulse_cnt = 0; |
| 3877 | chg->usb_icl_delta_ua = 0; |
Nicholas Troast | 8c28e0f | 2017-03-22 14:44:49 -0700 | [diff] [blame] | 3878 | chg->voltage_min_uv = MICRO_5V; |
| 3879 | chg->voltage_max_uv = MICRO_5V; |
| 3880 | chg->pd_active = 0; |
| 3881 | chg->pd_hard_reset = 0; |
Abhijeet Dharmapurikar | 3c93db3 | 2017-04-17 17:36:14 -0700 | [diff] [blame] | 3882 | chg->typec_legacy_valid = false; |
Harry Yang | 3b113a5 | 2016-12-08 12:37:40 -0800 | [diff] [blame] | 3883 | |
Anirudh Ghayal | 1121f5d | 2017-07-26 20:26:20 +0530 | [diff] [blame] | 3884 | /* write back the default FLOAT charger configuration */ |
| 3885 | rc = smblib_masked_write(chg, USBIN_OPTIONS_2_CFG_REG, |
| 3886 | (u8)FLOAT_OPTIONS_MASK, chg->float_cfg); |
| 3887 | if (rc < 0) |
| 3888 | smblib_err(chg, "Couldn't write float charger options rc=%d\n", |
| 3889 | rc); |
| 3890 | |
Abhijeet Dharmapurikar | 676bd79 | 2017-05-31 16:21:57 -0700 | [diff] [blame] | 3891 | /* reset back to 120mS tCC debounce */ |
| 3892 | rc = smblib_masked_write(chg, MISC_CFG_REG, TCC_DEBOUNCE_20MS_BIT, 0); |
| 3893 | if (rc < 0) |
| 3894 | smblib_err(chg, "Couldn't set 120mS tCC debounce rc=%d\n", rc); |
| 3895 | |
Nicholas Troast | fe74c59 | 2017-03-14 09:20:55 -0700 | [diff] [blame] | 3896 | /* enable APSD CC trigger for next insertion */ |
| 3897 | rc = smblib_masked_write(chg, TYPE_C_CFG_REG, |
| 3898 | APSD_START_ON_CC_BIT, APSD_START_ON_CC_BIT); |
| 3899 | if (rc < 0) |
| 3900 | smblib_err(chg, "Couldn't enable APSD_START_ON_CC rc=%d\n", rc); |
Abhijeet Dharmapurikar | 5e0bfbe | 2017-02-12 19:16:15 -0800 | [diff] [blame] | 3901 | |
Nicholas Troast | 8c28e0f | 2017-03-22 14:44:49 -0700 | [diff] [blame] | 3902 | if (chg->wa_flags & QC_AUTH_INTERRUPT_WA_BIT) { |
| 3903 | /* re-enable AUTH_IRQ_EN_CFG_BIT */ |
| 3904 | rc = smblib_masked_write(chg, |
| 3905 | USBIN_SOURCE_CHANGE_INTRPT_ENB_REG, |
| 3906 | AUTH_IRQ_EN_CFG_BIT, AUTH_IRQ_EN_CFG_BIT); |
| 3907 | if (rc < 0) |
| 3908 | smblib_err(chg, |
| 3909 | "Couldn't enable QC auth setting rc=%d\n", rc); |
| 3910 | } |
| 3911 | |
| 3912 | /* reconfigure allowed voltage for HVDCP */ |
| 3913 | rc = smblib_set_adapter_allowance(chg, |
| 3914 | USBIN_ADAPTER_ALLOW_5V_OR_9V_TO_12V); |
| 3915 | if (rc < 0) |
| 3916 | smblib_err(chg, "Couldn't set USBIN_ADAPTER_ALLOW_5V_OR_9V_TO_12V rc=%d\n", |
| 3917 | rc); |
| 3918 | |
| 3919 | /* enable DRP */ |
| 3920 | rc = smblib_masked_write(chg, TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG, |
| 3921 | TYPEC_POWER_ROLE_CMD_MASK, 0); |
| 3922 | if (rc < 0) |
| 3923 | smblib_err(chg, "Couldn't enable DRP rc=%d\n", rc); |
| 3924 | |
| 3925 | /* HW controlled CC_OUT */ |
| 3926 | rc = smblib_masked_write(chg, TAPER_TIMER_SEL_CFG_REG, |
| 3927 | TYPEC_SPARE_CFG_BIT, 0); |
| 3928 | if (rc < 0) |
| 3929 | smblib_err(chg, "Couldn't enable HW cc_out rc=%d\n", rc); |
| 3930 | |
| 3931 | /* restore crude sensor */ |
| 3932 | rc = smblib_write(chg, TM_IO_DTEST4_SEL, 0xA5); |
| 3933 | if (rc < 0) |
| 3934 | smblib_err(chg, "Couldn't restore crude sensor rc=%d\n", rc); |
| 3935 | |
Abhijeet Dharmapurikar | 255da95 | 2017-05-24 20:52:09 -0700 | [diff] [blame] | 3936 | mutex_lock(&chg->vconn_oc_lock); |
| 3937 | if (!chg->vconn_en) |
| 3938 | goto unlock; |
| 3939 | |
| 3940 | smblib_masked_write(chg, TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG, |
| 3941 | VCONN_EN_VALUE_BIT, 0); |
| 3942 | chg->vconn_en = false; |
| 3943 | |
| 3944 | unlock: |
| 3945 | mutex_unlock(&chg->vconn_oc_lock); |
| 3946 | |
Abhijeet Dharmapurikar | 319d694 | 2017-06-05 17:14:17 -0700 | [diff] [blame] | 3947 | /* clear exit sink based on cc */ |
| 3948 | rc = smblib_masked_write(chg, TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG, |
| 3949 | EXIT_SNK_BASED_ON_CC_BIT, 0); |
| 3950 | if (rc < 0) |
| 3951 | smblib_err(chg, "Couldn't clear exit_sink_based_on_cc rc=%d\n", |
| 3952 | rc); |
| 3953 | |
Abhijeet Dharmapurikar | 5e0bfbe | 2017-02-12 19:16:15 -0800 | [diff] [blame] | 3954 | typec_sink_removal(chg); |
Nicholas Troast | 8c28e0f | 2017-03-22 14:44:49 -0700 | [diff] [blame] | 3955 | smblib_update_usb_type(chg); |
Ankit Sharma | de321e1 | 2017-09-01 20:17:45 +0530 | [diff] [blame] | 3956 | |
| 3957 | if (chg->use_extcon) { |
| 3958 | if (chg->otg_present) |
| 3959 | smblib_notify_usb_host(chg, false); |
| 3960 | else |
| 3961 | smblib_notify_device_mode(chg, false); |
| 3962 | } |
| 3963 | chg->otg_present = false; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 3964 | } |
| 3965 | |
Nicholas Troast | e1932e4 | 2017-04-12 12:38:18 -0700 | [diff] [blame] | 3966 | static void smblib_handle_typec_insertion(struct smb_charger *chg) |
Abhijeet Dharmapurikar | a8075d7 | 2016-10-06 12:59:04 -0700 | [diff] [blame] | 3967 | { |
Abhijeet Dharmapurikar | 3c93db3 | 2017-04-17 17:36:14 -0700 | [diff] [blame] | 3968 | int rc; |
Abhijeet Dharmapurikar | a8075d7 | 2016-10-06 12:59:04 -0700 | [diff] [blame] | 3969 | |
Abhijeet Dharmapurikar | 99ca045 | 2016-10-13 09:50:41 -0700 | [diff] [blame] | 3970 | vote(chg->pd_disallowed_votable_indirect, CC_DETACHED_VOTER, false, 0); |
Abhijeet Dharmapurikar | a8075d7 | 2016-10-06 12:59:04 -0700 | [diff] [blame] | 3971 | |
Nicholas Troast | fe74c59 | 2017-03-14 09:20:55 -0700 | [diff] [blame] | 3972 | /* disable APSD CC trigger since CC is attached */ |
| 3973 | rc = smblib_masked_write(chg, TYPE_C_CFG_REG, APSD_START_ON_CC_BIT, 0); |
| 3974 | if (rc < 0) |
| 3975 | smblib_err(chg, "Couldn't disable APSD_START_ON_CC rc=%d\n", |
| 3976 | rc); |
| 3977 | |
Ashay Jaiswal | 13a1b81 | 2017-07-17 14:49:05 +0530 | [diff] [blame] | 3978 | if (chg->typec_status[3] & UFP_DFP_MODE_STATUS_BIT) { |
Abhijeet Dharmapurikar | 99ca045 | 2016-10-13 09:50:41 -0700 | [diff] [blame] | 3979 | typec_sink_insertion(chg); |
Ashay Jaiswal | 13a1b81 | 2017-07-17 14:49:05 +0530 | [diff] [blame] | 3980 | } else { |
| 3981 | rc = smblib_request_dpdm(chg, true); |
| 3982 | if (rc < 0) |
| 3983 | smblib_err(chg, "Couldn't to enable DPDM rc=%d\n", rc); |
Harry Yang | d89ff1f | 2016-12-05 14:59:11 -0800 | [diff] [blame] | 3984 | typec_sink_removal(chg); |
Ashay Jaiswal | 13a1b81 | 2017-07-17 14:49:05 +0530 | [diff] [blame] | 3985 | } |
Abhijeet Dharmapurikar | a8075d7 | 2016-10-06 12:59:04 -0700 | [diff] [blame] | 3986 | } |
| 3987 | |
Ashay Jaiswal | dd08c62 | 2017-06-29 16:25:23 +0530 | [diff] [blame] | 3988 | static void smblib_handle_rp_change(struct smb_charger *chg, int typec_mode) |
| 3989 | { |
| 3990 | int rp_ua; |
| 3991 | const struct apsd_result *apsd = smblib_get_apsd_result(chg); |
| 3992 | |
| 3993 | if ((apsd->pst != POWER_SUPPLY_TYPE_USB_DCP) |
| 3994 | && (apsd->pst != POWER_SUPPLY_TYPE_USB_FLOAT)) |
| 3995 | return; |
| 3996 | |
| 3997 | /* |
Anirudh Ghayal | 1121f5d | 2017-07-26 20:26:20 +0530 | [diff] [blame] | 3998 | * if APSD indicates FLOAT and the USB stack had detected SDP, |
| 3999 | * do not respond to Rp changes as we do not confirm that its |
| 4000 | * a legacy cable |
| 4001 | */ |
| 4002 | if (chg->real_charger_type == POWER_SUPPLY_TYPE_USB) |
| 4003 | return; |
| 4004 | /* |
| 4005 | * We want the ICL vote @ 100mA for a FLOAT charger |
| 4006 | * until the detection by the USB stack is complete. |
| 4007 | * Ignore the Rp changes unless there is a |
| 4008 | * pre-existing valid vote. |
| 4009 | */ |
| 4010 | if (apsd->pst == POWER_SUPPLY_TYPE_USB_FLOAT && |
| 4011 | get_client_vote(chg->usb_icl_votable, |
| 4012 | LEGACY_UNKNOWN_VOTER) <= 100000) |
| 4013 | return; |
| 4014 | |
| 4015 | /* |
Ashay Jaiswal | dd08c62 | 2017-06-29 16:25:23 +0530 | [diff] [blame] | 4016 | * handle Rp change for DCP/FLOAT/OCP. |
| 4017 | * Update the current only if the Rp is different from |
| 4018 | * the last Rp value. |
| 4019 | */ |
| 4020 | smblib_dbg(chg, PR_MISC, "CC change old_mode=%d new_mode=%d\n", |
| 4021 | chg->typec_mode, typec_mode); |
| 4022 | |
| 4023 | rp_ua = get_rp_based_dcp_current(chg, typec_mode); |
| 4024 | vote(chg->usb_icl_votable, LEGACY_UNKNOWN_VOTER, true, rp_ua); |
| 4025 | } |
| 4026 | |
Nicholas Troast | e1932e4 | 2017-04-12 12:38:18 -0700 | [diff] [blame] | 4027 | static void smblib_handle_typec_cc_state_change(struct smb_charger *chg) |
Abhijeet Dharmapurikar | 99ca045 | 2016-10-13 09:50:41 -0700 | [diff] [blame] | 4028 | { |
Ashay Jaiswal | dd08c62 | 2017-06-29 16:25:23 +0530 | [diff] [blame] | 4029 | int typec_mode; |
| 4030 | |
Nicholas Troast | e1932e4 | 2017-04-12 12:38:18 -0700 | [diff] [blame] | 4031 | if (chg->pr_swap_in_progress) |
| 4032 | return; |
Abhijeet Dharmapurikar | 99ca045 | 2016-10-13 09:50:41 -0700 | [diff] [blame] | 4033 | |
Ashay Jaiswal | dd08c62 | 2017-06-29 16:25:23 +0530 | [diff] [blame] | 4034 | typec_mode = smblib_get_prop_typec_mode(chg); |
| 4035 | if (chg->typec_present && (typec_mode != chg->typec_mode)) |
| 4036 | smblib_handle_rp_change(chg, typec_mode); |
| 4037 | |
| 4038 | chg->typec_mode = typec_mode; |
| 4039 | |
Nicholas Troast | e1932e4 | 2017-04-12 12:38:18 -0700 | [diff] [blame] | 4040 | if (!chg->typec_present && chg->typec_mode != POWER_SUPPLY_TYPEC_NONE) { |
| 4041 | chg->typec_present = true; |
| 4042 | smblib_dbg(chg, PR_MISC, "TypeC %s insertion\n", |
| 4043 | smblib_typec_mode_name[chg->typec_mode]); |
| 4044 | smblib_handle_typec_insertion(chg); |
| 4045 | } else if (chg->typec_present && |
| 4046 | chg->typec_mode == POWER_SUPPLY_TYPEC_NONE) { |
| 4047 | chg->typec_present = false; |
| 4048 | smblib_dbg(chg, PR_MISC, "TypeC removal\n"); |
| 4049 | smblib_handle_typec_removal(chg); |
Nicholas Troast | 8c28e0f | 2017-03-22 14:44:49 -0700 | [diff] [blame] | 4050 | } |
Abhijeet Dharmapurikar | 99ca045 | 2016-10-13 09:50:41 -0700 | [diff] [blame] | 4051 | |
Abhijeet Dharmapurikar | 66d2c44 | 2017-07-12 11:55:36 -0700 | [diff] [blame] | 4052 | /* suspend usb if sink */ |
Harry Yang | ca0664e | 2017-08-18 11:40:06 -0700 | [diff] [blame] | 4053 | if ((chg->typec_status[3] & UFP_DFP_MODE_STATUS_BIT) |
| 4054 | && chg->typec_present) |
Abhijeet Dharmapurikar | 66d2c44 | 2017-07-12 11:55:36 -0700 | [diff] [blame] | 4055 | vote(chg->usb_icl_votable, OTG_VOTER, true, 0); |
| 4056 | else |
| 4057 | vote(chg->usb_icl_votable, OTG_VOTER, false, 0); |
| 4058 | |
Nicholas Troast | e1932e4 | 2017-04-12 12:38:18 -0700 | [diff] [blame] | 4059 | smblib_dbg(chg, PR_INTERRUPT, "IRQ: cc-state-change; Type-C %s detected\n", |
| 4060 | smblib_typec_mode_name[chg->typec_mode]); |
Abhijeet Dharmapurikar | 99ca045 | 2016-10-13 09:50:41 -0700 | [diff] [blame] | 4061 | } |
| 4062 | |
Abhijeet Dharmapurikar | 11330d9 | 2017-04-17 12:15:19 -0700 | [diff] [blame] | 4063 | static void smblib_usb_typec_change(struct smb_charger *chg) |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 4064 | { |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 4065 | int rc; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 4066 | |
Abhijeet Dharmapurikar | b0403c4 | 2017-04-17 12:26:26 -0700 | [diff] [blame] | 4067 | rc = smblib_multibyte_read(chg, TYPE_C_STATUS_1_REG, |
| 4068 | chg->typec_status, 5); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 4069 | if (rc < 0) { |
Abhijeet Dharmapurikar | b0403c4 | 2017-04-17 12:26:26 -0700 | [diff] [blame] | 4070 | smblib_err(chg, "Couldn't cache USB Type-C status rc=%d\n", rc); |
Abhijeet Dharmapurikar | 11330d9 | 2017-04-17 12:15:19 -0700 | [diff] [blame] | 4071 | return; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 4072 | } |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 4073 | |
Nicholas Troast | e1932e4 | 2017-04-12 12:38:18 -0700 | [diff] [blame] | 4074 | smblib_handle_typec_cc_state_change(chg); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 4075 | |
Abhijeet Dharmapurikar | b0403c4 | 2017-04-17 12:26:26 -0700 | [diff] [blame] | 4076 | if (chg->typec_status[3] & TYPEC_VBUS_ERROR_STATUS_BIT) |
Abhijeet Dharmapurikar | 11330d9 | 2017-04-17 12:15:19 -0700 | [diff] [blame] | 4077 | smblib_dbg(chg, PR_INTERRUPT, "IRQ: vbus-error\n"); |
Harry Yang | d757c0f | 2016-09-23 10:52:05 -0700 | [diff] [blame] | 4078 | |
Abhijeet Dharmapurikar | b0403c4 | 2017-04-17 12:26:26 -0700 | [diff] [blame] | 4079 | if (chg->typec_status[3] & TYPEC_VCONN_OVERCURR_STATUS_BIT) |
Nicholas Troast | b11015f | 2017-01-17 17:56:45 -0800 | [diff] [blame] | 4080 | schedule_work(&chg->vconn_oc_work); |
Nicholas Troast | 8995a70 | 2016-12-05 10:22:22 -0800 | [diff] [blame] | 4081 | |
Nicholas Troast | b148655 | 2016-11-10 08:20:11 -0800 | [diff] [blame] | 4082 | power_supply_changed(chg->usb_psy); |
Abhijeet Dharmapurikar | 11330d9 | 2017-04-17 12:15:19 -0700 | [diff] [blame] | 4083 | } |
| 4084 | |
| 4085 | irqreturn_t smblib_handle_usb_typec_change(int irq, void *data) |
| 4086 | { |
| 4087 | struct smb_irq_data *irq_data = data; |
| 4088 | struct smb_charger *chg = irq_data->parent_data; |
| 4089 | |
| 4090 | if (chg->micro_usb_mode) { |
Ashay Jaiswal | 1bf4133 | 2017-05-03 15:10:25 +0530 | [diff] [blame] | 4091 | cancel_delayed_work_sync(&chg->uusb_otg_work); |
| 4092 | vote(chg->awake_votable, OTG_DELAY_VOTER, true, 0); |
| 4093 | smblib_dbg(chg, PR_INTERRUPT, "Scheduling OTG work\n"); |
| 4094 | schedule_delayed_work(&chg->uusb_otg_work, |
| 4095 | msecs_to_jiffies(chg->otg_delay_ms)); |
Abhijeet Dharmapurikar | 11330d9 | 2017-04-17 12:15:19 -0700 | [diff] [blame] | 4096 | return IRQ_HANDLED; |
| 4097 | } |
| 4098 | |
Abhijeet Dharmapurikar | 3c93db3 | 2017-04-17 17:36:14 -0700 | [diff] [blame] | 4099 | if (chg->cc2_detach_wa_active || chg->typec_en_dis_active) { |
| 4100 | smblib_dbg(chg, PR_INTERRUPT, "Ignoring since %s active\n", |
| 4101 | chg->cc2_detach_wa_active ? |
| 4102 | "cc2_detach_wa" : "typec_en_dis"); |
Abhijeet Dharmapurikar | 11330d9 | 2017-04-17 12:15:19 -0700 | [diff] [blame] | 4103 | return IRQ_HANDLED; |
| 4104 | } |
| 4105 | |
Abhijeet Dharmapurikar | 049b255 | 2017-07-12 11:27:51 -0700 | [diff] [blame] | 4106 | if (chg->pr_swap_in_progress) { |
| 4107 | smblib_dbg(chg, PR_INTERRUPT, |
| 4108 | "Ignoring since pr_swap_in_progress\n"); |
| 4109 | return IRQ_HANDLED; |
| 4110 | } |
| 4111 | |
Abhijeet Dharmapurikar | 11330d9 | 2017-04-17 12:15:19 -0700 | [diff] [blame] | 4112 | mutex_lock(&chg->lock); |
| 4113 | smblib_usb_typec_change(chg); |
| 4114 | mutex_unlock(&chg->lock); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 4115 | return IRQ_HANDLED; |
| 4116 | } |
| 4117 | |
Abhijeet Dharmapurikar | 2391664 | 2016-10-03 10:38:50 -0700 | [diff] [blame] | 4118 | irqreturn_t smblib_handle_dc_plugin(int irq, void *data) |
| 4119 | { |
| 4120 | struct smb_irq_data *irq_data = data; |
| 4121 | struct smb_charger *chg = irq_data->parent_data; |
| 4122 | |
| 4123 | power_supply_changed(chg->dc_psy); |
| 4124 | return IRQ_HANDLED; |
| 4125 | } |
| 4126 | |
Abhijeet Dharmapurikar | 0e36900 | 2016-09-07 17:25:36 -0700 | [diff] [blame] | 4127 | irqreturn_t smblib_handle_high_duty_cycle(int irq, void *data) |
| 4128 | { |
| 4129 | struct smb_irq_data *irq_data = data; |
| 4130 | struct smb_charger *chg = irq_data->parent_data; |
| 4131 | |
| 4132 | chg->is_hdc = true; |
| 4133 | schedule_delayed_work(&chg->clear_hdc_work, msecs_to_jiffies(60)); |
| 4134 | |
| 4135 | return IRQ_HANDLED; |
| 4136 | } |
| 4137 | |
Anirudh Ghayal | 36feefe | 2017-05-26 09:41:25 +0530 | [diff] [blame] | 4138 | static void smblib_bb_removal_work(struct work_struct *work) |
| 4139 | { |
| 4140 | struct smb_charger *chg = container_of(work, struct smb_charger, |
| 4141 | bb_removal_work.work); |
| 4142 | |
| 4143 | vote(chg->usb_icl_votable, BOOST_BACK_VOTER, false, 0); |
| 4144 | vote(chg->awake_votable, BOOST_BACK_VOTER, false, 0); |
| 4145 | } |
| 4146 | |
| 4147 | #define BOOST_BACK_UNVOTE_DELAY_MS 750 |
Ashay Jaiswal | 2f3b0c1 | 2017-06-14 16:04:45 +0530 | [diff] [blame] | 4148 | #define BOOST_BACK_STORM_COUNT 3 |
| 4149 | #define WEAK_CHG_STORM_COUNT 8 |
Nicholas Troast | abedaf7 | 2016-09-16 11:07:45 -0700 | [diff] [blame] | 4150 | irqreturn_t smblib_handle_switcher_power_ok(int irq, void *data) |
| 4151 | { |
| 4152 | struct smb_irq_data *irq_data = data; |
| 4153 | struct smb_charger *chg = irq_data->parent_data; |
Ashay Jaiswal | 2f3b0c1 | 2017-06-14 16:04:45 +0530 | [diff] [blame] | 4154 | struct storm_watch *wdata = &irq_data->storm_data; |
Abhijeet Dharmapurikar | 3c93db3 | 2017-04-17 17:36:14 -0700 | [diff] [blame] | 4155 | int rc, usb_icl; |
Nicholas Troast | abedaf7 | 2016-09-16 11:07:45 -0700 | [diff] [blame] | 4156 | u8 stat; |
| 4157 | |
| 4158 | if (!(chg->wa_flags & BOOST_BACK_WA)) |
| 4159 | return IRQ_HANDLED; |
| 4160 | |
| 4161 | rc = smblib_read(chg, POWER_PATH_STATUS_REG, &stat); |
| 4162 | if (rc < 0) { |
| 4163 | smblib_err(chg, "Couldn't read POWER_PATH_STATUS rc=%d\n", rc); |
| 4164 | return IRQ_HANDLED; |
| 4165 | } |
| 4166 | |
Abhijeet Dharmapurikar | 3c93db3 | 2017-04-17 17:36:14 -0700 | [diff] [blame] | 4167 | /* skip suspending input if its already suspended by some other voter */ |
| 4168 | usb_icl = get_effective_result(chg->usb_icl_votable); |
Harry Yang | 379484a | 2017-08-29 10:26:14 -0700 | [diff] [blame] | 4169 | if ((stat & USE_USBIN_BIT) && usb_icl >= 0 && usb_icl <= USBIN_25MA) |
Nicholas Troast | abedaf7 | 2016-09-16 11:07:45 -0700 | [diff] [blame] | 4170 | return IRQ_HANDLED; |
| 4171 | |
Abhijeet Dharmapurikar | 2823fe3 | 2016-12-05 17:52:11 -0800 | [diff] [blame] | 4172 | if (stat & USE_DCIN_BIT) |
Nicholas Troast | abedaf7 | 2016-09-16 11:07:45 -0700 | [diff] [blame] | 4173 | return IRQ_HANDLED; |
| 4174 | |
| 4175 | if (is_storming(&irq_data->storm_data)) { |
Ashay Jaiswal | 2f3b0c1 | 2017-06-14 16:04:45 +0530 | [diff] [blame] | 4176 | /* This could be a weak charger reduce ICL */ |
| 4177 | if (!is_client_vote_enabled(chg->usb_icl_votable, |
| 4178 | WEAK_CHARGER_VOTER)) { |
| 4179 | smblib_err(chg, |
| 4180 | "Weak charger detected: voting %dmA ICL\n", |
| 4181 | *chg->weak_chg_icl_ua / 1000); |
| 4182 | vote(chg->usb_icl_votable, WEAK_CHARGER_VOTER, |
| 4183 | true, *chg->weak_chg_icl_ua); |
| 4184 | /* |
| 4185 | * reset storm data and set the storm threshold |
| 4186 | * to 3 for reverse boost detection. |
| 4187 | */ |
| 4188 | update_storm_count(wdata, BOOST_BACK_STORM_COUNT); |
| 4189 | } else { |
| 4190 | smblib_err(chg, |
| 4191 | "Reverse boost detected: voting 0mA to suspend input\n"); |
| 4192 | vote(chg->usb_icl_votable, BOOST_BACK_VOTER, true, 0); |
| 4193 | vote(chg->awake_votable, BOOST_BACK_VOTER, true, 0); |
| 4194 | /* |
| 4195 | * Remove the boost-back vote after a delay, to avoid |
| 4196 | * permanently suspending the input if the boost-back |
| 4197 | * condition is unintentionally hit. |
| 4198 | */ |
| 4199 | schedule_delayed_work(&chg->bb_removal_work, |
| 4200 | msecs_to_jiffies(BOOST_BACK_UNVOTE_DELAY_MS)); |
| 4201 | } |
Nicholas Troast | abedaf7 | 2016-09-16 11:07:45 -0700 | [diff] [blame] | 4202 | } |
| 4203 | |
| 4204 | return IRQ_HANDLED; |
| 4205 | } |
| 4206 | |
Nicholas Troast | 15dc0c8 | 2016-10-18 15:15:21 -0700 | [diff] [blame] | 4207 | irqreturn_t smblib_handle_wdog_bark(int irq, void *data) |
| 4208 | { |
| 4209 | struct smb_irq_data *irq_data = data; |
| 4210 | struct smb_charger *chg = irq_data->parent_data; |
| 4211 | int rc; |
| 4212 | |
Anirudh Ghayal | 9d0196d | 2017-07-23 23:02:48 +0530 | [diff] [blame] | 4213 | smblib_dbg(chg, PR_INTERRUPT, "IRQ: %s\n", irq_data->name); |
| 4214 | |
Nicholas Troast | 15dc0c8 | 2016-10-18 15:15:21 -0700 | [diff] [blame] | 4215 | rc = smblib_write(chg, BARK_BITE_WDOG_PET_REG, BARK_BITE_WDOG_PET_BIT); |
| 4216 | if (rc < 0) |
| 4217 | smblib_err(chg, "Couldn't pet the dog rc=%d\n", rc); |
| 4218 | |
Ashay Jaiswal | 9aba44a | 2017-07-20 17:41:36 +0530 | [diff] [blame] | 4219 | if (chg->step_chg_enabled || chg->sw_jeita_enabled) |
Anirudh Ghayal | 9d0196d | 2017-07-23 23:02:48 +0530 | [diff] [blame] | 4220 | power_supply_changed(chg->batt_psy); |
| 4221 | |
Nicholas Troast | 15dc0c8 | 2016-10-18 15:15:21 -0700 | [diff] [blame] | 4222 | return IRQ_HANDLED; |
| 4223 | } |
| 4224 | |
Abhijeet Dharmapurikar | 255da95 | 2017-05-24 20:52:09 -0700 | [diff] [blame] | 4225 | /************** |
| 4226 | * Additional USB PSY getters/setters |
| 4227 | * that call interrupt functions |
| 4228 | ***************/ |
| 4229 | |
| 4230 | int smblib_get_prop_pr_swap_in_progress(struct smb_charger *chg, |
| 4231 | union power_supply_propval *val) |
| 4232 | { |
| 4233 | val->intval = chg->pr_swap_in_progress; |
| 4234 | return 0; |
| 4235 | } |
| 4236 | |
| 4237 | int smblib_set_prop_pr_swap_in_progress(struct smb_charger *chg, |
| 4238 | const union power_supply_propval *val) |
| 4239 | { |
Abhijeet Dharmapurikar | 676bd79 | 2017-05-31 16:21:57 -0700 | [diff] [blame] | 4240 | int rc; |
| 4241 | |
Abhijeet Dharmapurikar | 255da95 | 2017-05-24 20:52:09 -0700 | [diff] [blame] | 4242 | chg->pr_swap_in_progress = val->intval; |
| 4243 | /* |
| 4244 | * call the cc changed irq to handle real removals while |
| 4245 | * PR_SWAP was in progress |
| 4246 | */ |
| 4247 | smblib_usb_typec_change(chg); |
Abhijeet Dharmapurikar | 676bd79 | 2017-05-31 16:21:57 -0700 | [diff] [blame] | 4248 | rc = smblib_masked_write(chg, MISC_CFG_REG, TCC_DEBOUNCE_20MS_BIT, |
| 4249 | val->intval ? TCC_DEBOUNCE_20MS_BIT : 0); |
| 4250 | if (rc < 0) |
| 4251 | smblib_err(chg, "Couldn't set tCC debounce rc=%d\n", rc); |
Abhijeet Dharmapurikar | 255da95 | 2017-05-24 20:52:09 -0700 | [diff] [blame] | 4252 | return 0; |
| 4253 | } |
| 4254 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 4255 | /*************** |
| 4256 | * Work Queues * |
| 4257 | ***************/ |
Ashay Jaiswal | 1bf4133 | 2017-05-03 15:10:25 +0530 | [diff] [blame] | 4258 | static void smblib_uusb_otg_work(struct work_struct *work) |
| 4259 | { |
| 4260 | struct smb_charger *chg = container_of(work, struct smb_charger, |
| 4261 | uusb_otg_work.work); |
| 4262 | int rc; |
| 4263 | u8 stat; |
| 4264 | bool otg; |
| 4265 | |
| 4266 | rc = smblib_read(chg, TYPE_C_STATUS_3_REG, &stat); |
| 4267 | if (rc < 0) { |
| 4268 | smblib_err(chg, "Couldn't read TYPE_C_STATUS_3 rc=%d\n", rc); |
| 4269 | goto out; |
| 4270 | } |
| 4271 | |
| 4272 | otg = !!(stat & (U_USB_GND_NOVBUS_BIT | U_USB_GND_BIT)); |
| 4273 | extcon_set_cable_state_(chg->extcon, EXTCON_USB_HOST, otg); |
| 4274 | smblib_dbg(chg, PR_REGISTER, "TYPE_C_STATUS_3 = 0x%02x OTG=%d\n", |
| 4275 | stat, otg); |
| 4276 | power_supply_changed(chg->usb_psy); |
| 4277 | |
| 4278 | out: |
| 4279 | vote(chg->awake_votable, OTG_DELAY_VOTER, false, 0); |
| 4280 | } |
| 4281 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 4282 | |
| 4283 | static void smblib_hvdcp_detect_work(struct work_struct *work) |
| 4284 | { |
| 4285 | struct smb_charger *chg = container_of(work, struct smb_charger, |
| 4286 | hvdcp_detect_work.work); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 4287 | |
Abhijeet Dharmapurikar | 4b13c6e | 2016-10-05 15:15:10 -0700 | [diff] [blame] | 4288 | vote(chg->pd_disallowed_votable_indirect, HVDCP_TIMEOUT_VOTER, |
| 4289 | false, 0); |
Abhijeet Dharmapurikar | 3c93db3 | 2017-04-17 17:36:14 -0700 | [diff] [blame] | 4290 | power_supply_changed(chg->usb_psy); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 4291 | } |
| 4292 | |
Harry Yang | fe91384 | 2016-08-10 12:27:28 -0700 | [diff] [blame] | 4293 | static void bms_update_work(struct work_struct *work) |
Harry Yang | 5e1a522 | 2016-07-26 15:16:04 -0700 | [diff] [blame] | 4294 | { |
| 4295 | struct smb_charger *chg = container_of(work, struct smb_charger, |
| 4296 | bms_update_work); |
Ashay Jaiswal | 2fb369d | 2017-01-12 21:38:29 +0530 | [diff] [blame] | 4297 | |
| 4298 | smblib_suspend_on_debug_battery(chg); |
| 4299 | |
| 4300 | if (chg->batt_psy) |
| 4301 | power_supply_changed(chg->batt_psy); |
Harry Yang | 5e1a522 | 2016-07-26 15:16:04 -0700 | [diff] [blame] | 4302 | } |
| 4303 | |
Harry Yang | 166b15d | 2017-08-28 13:00:40 -0700 | [diff] [blame^] | 4304 | static void pl_update_work(struct work_struct *work) |
| 4305 | { |
| 4306 | struct smb_charger *chg = container_of(work, struct smb_charger, |
| 4307 | pl_update_work); |
| 4308 | |
| 4309 | smblib_stat_sw_override_cfg(chg, false); |
| 4310 | } |
| 4311 | |
Abhijeet Dharmapurikar | 0e36900 | 2016-09-07 17:25:36 -0700 | [diff] [blame] | 4312 | static void clear_hdc_work(struct work_struct *work) |
| 4313 | { |
| 4314 | struct smb_charger *chg = container_of(work, struct smb_charger, |
| 4315 | clear_hdc_work.work); |
| 4316 | |
| 4317 | chg->is_hdc = 0; |
| 4318 | } |
| 4319 | |
Harry Yang | 755a34b | 2016-11-01 01:18:51 -0700 | [diff] [blame] | 4320 | static void rdstd_cc2_detach_work(struct work_struct *work) |
| 4321 | { |
| 4322 | int rc; |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 4323 | u8 stat4, stat5; |
Harry Yang | 755a34b | 2016-11-01 01:18:51 -0700 | [diff] [blame] | 4324 | struct smb_charger *chg = container_of(work, struct smb_charger, |
| 4325 | rdstd_cc2_detach_work); |
| 4326 | |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 4327 | if (!chg->cc2_detach_wa_active) |
| 4328 | return; |
| 4329 | |
Harry Yang | 755a34b | 2016-11-01 01:18:51 -0700 | [diff] [blame] | 4330 | /* |
| 4331 | * WA steps - |
| 4332 | * 1. Enable both UFP and DFP, wait for 10ms. |
| 4333 | * 2. Disable DFP, wait for 30ms. |
| 4334 | * 3. Removal detected if both TYPEC_DEBOUNCE_DONE_STATUS |
| 4335 | * and TIMER_STAGE bits are gone, otherwise repeat all by |
| 4336 | * work rescheduling. |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 4337 | * Note, work will be cancelled when USB_PLUGIN rises. |
Harry Yang | 755a34b | 2016-11-01 01:18:51 -0700 | [diff] [blame] | 4338 | */ |
| 4339 | |
| 4340 | rc = smblib_masked_write(chg, TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG, |
| 4341 | UFP_EN_CMD_BIT | DFP_EN_CMD_BIT, |
| 4342 | UFP_EN_CMD_BIT | DFP_EN_CMD_BIT); |
| 4343 | if (rc < 0) { |
| 4344 | smblib_err(chg, "Couldn't write TYPE_C_CTRL_REG rc=%d\n", rc); |
| 4345 | return; |
| 4346 | } |
| 4347 | |
| 4348 | usleep_range(10000, 11000); |
| 4349 | |
| 4350 | rc = smblib_masked_write(chg, TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG, |
| 4351 | UFP_EN_CMD_BIT | DFP_EN_CMD_BIT, |
| 4352 | UFP_EN_CMD_BIT); |
| 4353 | if (rc < 0) { |
| 4354 | smblib_err(chg, "Couldn't write TYPE_C_CTRL_REG rc=%d\n", rc); |
| 4355 | return; |
| 4356 | } |
| 4357 | |
| 4358 | usleep_range(30000, 31000); |
| 4359 | |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 4360 | rc = smblib_read(chg, TYPE_C_STATUS_4_REG, &stat4); |
Harry Yang | 755a34b | 2016-11-01 01:18:51 -0700 | [diff] [blame] | 4361 | if (rc < 0) { |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 4362 | smblib_err(chg, "Couldn't read TYPE_C_STATUS_4 rc=%d\n", rc); |
Harry Yang | 755a34b | 2016-11-01 01:18:51 -0700 | [diff] [blame] | 4363 | return; |
| 4364 | } |
Harry Yang | 755a34b | 2016-11-01 01:18:51 -0700 | [diff] [blame] | 4365 | |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 4366 | rc = smblib_read(chg, TYPE_C_STATUS_5_REG, &stat5); |
Harry Yang | 755a34b | 2016-11-01 01:18:51 -0700 | [diff] [blame] | 4367 | if (rc < 0) { |
| 4368 | smblib_err(chg, |
| 4369 | "Couldn't read TYPE_C_STATUS_5_REG rc=%d\n", rc); |
| 4370 | return; |
| 4371 | } |
Harry Yang | 755a34b | 2016-11-01 01:18:51 -0700 | [diff] [blame] | 4372 | |
Abhijeet Dharmapurikar | 0e43281 | 2017-04-17 14:52:46 -0700 | [diff] [blame] | 4373 | if ((stat4 & TYPEC_DEBOUNCE_DONE_STATUS_BIT) |
| 4374 | || (stat5 & TIMER_STAGE_2_BIT)) { |
| 4375 | smblib_dbg(chg, PR_MISC, "rerunning DD=%d TS2BIT=%d\n", |
| 4376 | (int)(stat4 & TYPEC_DEBOUNCE_DONE_STATUS_BIT), |
| 4377 | (int)(stat5 & TIMER_STAGE_2_BIT)); |
| 4378 | goto rerun; |
| 4379 | } |
| 4380 | |
| 4381 | smblib_dbg(chg, PR_MISC, "Bingo CC2 Removal detected\n"); |
| 4382 | chg->cc2_detach_wa_active = false; |
| 4383 | rc = smblib_masked_write(chg, TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG, |
| 4384 | EXIT_SNK_BASED_ON_CC_BIT, 0); |
Harry Yang | 755a34b | 2016-11-01 01:18:51 -0700 | [diff] [blame] | 4385 | smblib_reg_block_restore(chg, cc2_detach_settings); |
Abhijeet Dharmapurikar | 11330d9 | 2017-04-17 12:15:19 -0700 | [diff] [blame] | 4386 | mutex_lock(&chg->lock); |
| 4387 | smblib_usb_typec_change(chg); |
| 4388 | mutex_unlock(&chg->lock); |
Harry Yang | 755a34b | 2016-11-01 01:18:51 -0700 | [diff] [blame] | 4389 | return; |
| 4390 | |
| 4391 | rerun: |
| 4392 | schedule_work(&chg->rdstd_cc2_detach_work); |
| 4393 | } |
| 4394 | |
Nicholas Troast | b11015f | 2017-01-17 17:56:45 -0800 | [diff] [blame] | 4395 | static void smblib_otg_oc_exit(struct smb_charger *chg, bool success) |
| 4396 | { |
| 4397 | int rc; |
| 4398 | |
| 4399 | chg->otg_attempts = 0; |
| 4400 | if (!success) { |
| 4401 | smblib_err(chg, "OTG soft start failed\n"); |
| 4402 | chg->otg_en = false; |
| 4403 | } |
| 4404 | |
| 4405 | smblib_dbg(chg, PR_OTG, "enabling VBUS < 1V check\n"); |
| 4406 | rc = smblib_masked_write(chg, OTG_CFG_REG, |
| 4407 | QUICKSTART_OTG_FASTROLESWAP_BIT, 0); |
| 4408 | if (rc < 0) |
| 4409 | smblib_err(chg, "Couldn't enable VBUS < 1V check rc=%d\n", rc); |
Nicholas Troast | b11015f | 2017-01-17 17:56:45 -0800 | [diff] [blame] | 4410 | } |
| 4411 | |
| 4412 | #define MAX_OC_FALLING_TRIES 10 |
| 4413 | static void smblib_otg_oc_work(struct work_struct *work) |
| 4414 | { |
| 4415 | struct smb_charger *chg = container_of(work, struct smb_charger, |
| 4416 | otg_oc_work); |
| 4417 | int rc, i; |
| 4418 | u8 stat; |
| 4419 | |
| 4420 | if (!chg->vbus_vreg || !chg->vbus_vreg->rdev) |
| 4421 | return; |
| 4422 | |
| 4423 | smblib_err(chg, "over-current detected on VBUS\n"); |
| 4424 | mutex_lock(&chg->otg_oc_lock); |
| 4425 | if (!chg->otg_en) |
| 4426 | goto unlock; |
| 4427 | |
| 4428 | smblib_dbg(chg, PR_OTG, "disabling VBUS < 1V check\n"); |
| 4429 | smblib_masked_write(chg, OTG_CFG_REG, |
| 4430 | QUICKSTART_OTG_FASTROLESWAP_BIT, |
| 4431 | QUICKSTART_OTG_FASTROLESWAP_BIT); |
| 4432 | |
| 4433 | /* |
| 4434 | * If 500ms has passed and another over-current interrupt has not |
| 4435 | * triggered then it is likely that the software based soft start was |
| 4436 | * successful and the VBUS < 1V restriction should be re-enabled. |
| 4437 | */ |
| 4438 | schedule_delayed_work(&chg->otg_ss_done_work, msecs_to_jiffies(500)); |
| 4439 | |
| 4440 | rc = _smblib_vbus_regulator_disable(chg->vbus_vreg->rdev); |
| 4441 | if (rc < 0) { |
| 4442 | smblib_err(chg, "Couldn't disable VBUS rc=%d\n", rc); |
| 4443 | goto unlock; |
| 4444 | } |
| 4445 | |
| 4446 | if (++chg->otg_attempts > OTG_MAX_ATTEMPTS) { |
| 4447 | cancel_delayed_work_sync(&chg->otg_ss_done_work); |
| 4448 | smblib_err(chg, "OTG failed to enable after %d attempts\n", |
| 4449 | chg->otg_attempts - 1); |
| 4450 | smblib_otg_oc_exit(chg, false); |
| 4451 | goto unlock; |
| 4452 | } |
| 4453 | |
| 4454 | /* |
| 4455 | * The real time status should go low within 10ms. Poll every 1-2ms to |
| 4456 | * minimize the delay when re-enabling OTG. |
| 4457 | */ |
| 4458 | for (i = 0; i < MAX_OC_FALLING_TRIES; ++i) { |
| 4459 | usleep_range(1000, 2000); |
| 4460 | rc = smblib_read(chg, OTG_BASE + INT_RT_STS_OFFSET, &stat); |
| 4461 | if (rc >= 0 && !(stat & OTG_OVERCURRENT_RT_STS_BIT)) |
| 4462 | break; |
| 4463 | } |
| 4464 | |
| 4465 | if (i >= MAX_OC_FALLING_TRIES) { |
| 4466 | cancel_delayed_work_sync(&chg->otg_ss_done_work); |
| 4467 | smblib_err(chg, "OTG OC did not fall after %dms\n", |
| 4468 | 2 * MAX_OC_FALLING_TRIES); |
| 4469 | smblib_otg_oc_exit(chg, false); |
| 4470 | goto unlock; |
| 4471 | } |
| 4472 | |
| 4473 | smblib_dbg(chg, PR_OTG, "OTG OC fell after %dms\n", 2 * i + 1); |
| 4474 | rc = _smblib_vbus_regulator_enable(chg->vbus_vreg->rdev); |
| 4475 | if (rc < 0) { |
| 4476 | smblib_err(chg, "Couldn't enable VBUS rc=%d\n", rc); |
| 4477 | goto unlock; |
| 4478 | } |
| 4479 | |
| 4480 | unlock: |
| 4481 | mutex_unlock(&chg->otg_oc_lock); |
| 4482 | } |
| 4483 | |
| 4484 | static void smblib_vconn_oc_work(struct work_struct *work) |
| 4485 | { |
| 4486 | struct smb_charger *chg = container_of(work, struct smb_charger, |
| 4487 | vconn_oc_work); |
| 4488 | int rc, i; |
| 4489 | u8 stat; |
| 4490 | |
Ashay Jaiswal | 15edce4 | 2017-03-31 23:29:58 +0530 | [diff] [blame] | 4491 | if (chg->micro_usb_mode) |
| 4492 | return; |
| 4493 | |
Nicholas Troast | b11015f | 2017-01-17 17:56:45 -0800 | [diff] [blame] | 4494 | smblib_err(chg, "over-current detected on VCONN\n"); |
| 4495 | if (!chg->vconn_vreg || !chg->vconn_vreg->rdev) |
| 4496 | return; |
| 4497 | |
Nicholas Troast | 85d3a5a | 2017-05-03 10:19:43 -0700 | [diff] [blame] | 4498 | mutex_lock(&chg->vconn_oc_lock); |
Nicholas Troast | b11015f | 2017-01-17 17:56:45 -0800 | [diff] [blame] | 4499 | rc = _smblib_vconn_regulator_disable(chg->vconn_vreg->rdev); |
| 4500 | if (rc < 0) { |
| 4501 | smblib_err(chg, "Couldn't disable VCONN rc=%d\n", rc); |
| 4502 | goto unlock; |
| 4503 | } |
| 4504 | |
| 4505 | if (++chg->vconn_attempts > VCONN_MAX_ATTEMPTS) { |
| 4506 | smblib_err(chg, "VCONN failed to enable after %d attempts\n", |
| 4507 | chg->otg_attempts - 1); |
| 4508 | chg->vconn_en = false; |
| 4509 | chg->vconn_attempts = 0; |
| 4510 | goto unlock; |
| 4511 | } |
| 4512 | |
| 4513 | /* |
| 4514 | * The real time status should go low within 10ms. Poll every 1-2ms to |
| 4515 | * minimize the delay when re-enabling OTG. |
| 4516 | */ |
| 4517 | for (i = 0; i < MAX_OC_FALLING_TRIES; ++i) { |
| 4518 | usleep_range(1000, 2000); |
| 4519 | rc = smblib_read(chg, TYPE_C_STATUS_4_REG, &stat); |
| 4520 | if (rc >= 0 && !(stat & TYPEC_VCONN_OVERCURR_STATUS_BIT)) |
| 4521 | break; |
| 4522 | } |
| 4523 | |
| 4524 | if (i >= MAX_OC_FALLING_TRIES) { |
| 4525 | smblib_err(chg, "VCONN OC did not fall after %dms\n", |
| 4526 | 2 * MAX_OC_FALLING_TRIES); |
| 4527 | chg->vconn_en = false; |
| 4528 | chg->vconn_attempts = 0; |
| 4529 | goto unlock; |
| 4530 | } |
| 4531 | |
| 4532 | smblib_dbg(chg, PR_OTG, "VCONN OC fell after %dms\n", 2 * i + 1); |
| 4533 | if (++chg->vconn_attempts > VCONN_MAX_ATTEMPTS) { |
| 4534 | smblib_err(chg, "VCONN failed to enable after %d attempts\n", |
| 4535 | chg->vconn_attempts - 1); |
| 4536 | chg->vconn_en = false; |
| 4537 | goto unlock; |
| 4538 | } |
| 4539 | |
| 4540 | rc = _smblib_vconn_regulator_enable(chg->vconn_vreg->rdev); |
| 4541 | if (rc < 0) { |
| 4542 | smblib_err(chg, "Couldn't enable VCONN rc=%d\n", rc); |
| 4543 | goto unlock; |
| 4544 | } |
| 4545 | |
| 4546 | unlock: |
Nicholas Troast | 85d3a5a | 2017-05-03 10:19:43 -0700 | [diff] [blame] | 4547 | mutex_unlock(&chg->vconn_oc_lock); |
Nicholas Troast | b11015f | 2017-01-17 17:56:45 -0800 | [diff] [blame] | 4548 | } |
| 4549 | |
| 4550 | static void smblib_otg_ss_done_work(struct work_struct *work) |
| 4551 | { |
| 4552 | struct smb_charger *chg = container_of(work, struct smb_charger, |
| 4553 | otg_ss_done_work.work); |
| 4554 | int rc; |
| 4555 | bool success = false; |
| 4556 | u8 stat; |
| 4557 | |
| 4558 | mutex_lock(&chg->otg_oc_lock); |
| 4559 | rc = smblib_read(chg, OTG_STATUS_REG, &stat); |
| 4560 | if (rc < 0) |
| 4561 | smblib_err(chg, "Couldn't read OTG status rc=%d\n", rc); |
| 4562 | else if (stat & BOOST_SOFTSTART_DONE_BIT) |
| 4563 | success = true; |
| 4564 | |
| 4565 | smblib_otg_oc_exit(chg, success); |
| 4566 | mutex_unlock(&chg->otg_oc_lock); |
| 4567 | } |
| 4568 | |
Ashay Jaiswal | 4d82578 | 2017-02-18 10:11:34 +0530 | [diff] [blame] | 4569 | static void smblib_icl_change_work(struct work_struct *work) |
| 4570 | { |
| 4571 | struct smb_charger *chg = container_of(work, struct smb_charger, |
| 4572 | icl_change_work.work); |
| 4573 | int rc, settled_ua; |
| 4574 | |
| 4575 | rc = smblib_get_charge_param(chg, &chg->param.icl_stat, &settled_ua); |
| 4576 | if (rc < 0) { |
| 4577 | smblib_err(chg, "Couldn't get ICL status rc=%d\n", rc); |
| 4578 | return; |
| 4579 | } |
| 4580 | |
| 4581 | power_supply_changed(chg->usb_main_psy); |
Ashay Jaiswal | 4d82578 | 2017-02-18 10:11:34 +0530 | [diff] [blame] | 4582 | |
| 4583 | smblib_dbg(chg, PR_INTERRUPT, "icl_settled=%d\n", settled_ua); |
| 4584 | } |
| 4585 | |
Ashay Jaiswal | c036167 | 2017-03-21 12:24:16 +0530 | [diff] [blame] | 4586 | static void smblib_pl_enable_work(struct work_struct *work) |
| 4587 | { |
| 4588 | struct smb_charger *chg = container_of(work, struct smb_charger, |
| 4589 | pl_enable_work.work); |
| 4590 | |
| 4591 | smblib_dbg(chg, PR_PARALLEL, "timer expired, enabling parallel\n"); |
| 4592 | vote(chg->pl_disable_votable, PL_DELAY_VOTER, false, 0); |
| 4593 | vote(chg->awake_votable, PL_DELAY_VOTER, false, 0); |
| 4594 | } |
| 4595 | |
Abhijeet Dharmapurikar | 3c93db3 | 2017-04-17 17:36:14 -0700 | [diff] [blame] | 4596 | static void smblib_legacy_detection_work(struct work_struct *work) |
| 4597 | { |
| 4598 | struct smb_charger *chg = container_of(work, struct smb_charger, |
| 4599 | legacy_detection_work); |
| 4600 | int rc; |
| 4601 | u8 stat; |
| 4602 | bool legacy, rp_high; |
| 4603 | |
| 4604 | mutex_lock(&chg->lock); |
| 4605 | chg->typec_en_dis_active = 1; |
| 4606 | smblib_dbg(chg, PR_MISC, "running legacy unknown workaround\n"); |
| 4607 | rc = smblib_masked_write(chg, |
| 4608 | TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG, |
| 4609 | TYPEC_DISABLE_CMD_BIT, |
| 4610 | TYPEC_DISABLE_CMD_BIT); |
| 4611 | if (rc < 0) |
| 4612 | smblib_err(chg, "Couldn't disable type-c rc=%d\n", rc); |
| 4613 | |
| 4614 | /* wait for the adapter to turn off VBUS */ |
| 4615 | msleep(500); |
| 4616 | |
| 4617 | rc = smblib_masked_write(chg, |
| 4618 | TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG, |
| 4619 | TYPEC_DISABLE_CMD_BIT, 0); |
| 4620 | if (rc < 0) |
| 4621 | smblib_err(chg, "Couldn't enable type-c rc=%d\n", rc); |
| 4622 | |
| 4623 | /* wait for type-c detection to complete */ |
| 4624 | msleep(100); |
| 4625 | |
| 4626 | rc = smblib_read(chg, TYPE_C_STATUS_5_REG, &stat); |
| 4627 | if (rc < 0) { |
| 4628 | smblib_err(chg, "Couldn't read typec stat5 rc = %d\n", rc); |
| 4629 | goto unlock; |
| 4630 | } |
| 4631 | |
| 4632 | chg->typec_legacy_valid = true; |
| 4633 | vote(chg->usb_icl_votable, LEGACY_UNKNOWN_VOTER, false, 0); |
| 4634 | legacy = stat & TYPEC_LEGACY_CABLE_STATUS_BIT; |
Nicholas Troast | e1932e4 | 2017-04-12 12:38:18 -0700 | [diff] [blame] | 4635 | rp_high = chg->typec_mode == POWER_SUPPLY_TYPEC_SOURCE_HIGH; |
Abhijeet Dharmapurikar | 3c93db3 | 2017-04-17 17:36:14 -0700 | [diff] [blame] | 4636 | if (!legacy || !rp_high) |
| 4637 | vote(chg->hvdcp_disable_votable_indirect, VBUS_CC_SHORT_VOTER, |
| 4638 | false, 0); |
| 4639 | |
| 4640 | unlock: |
| 4641 | chg->typec_en_dis_active = 0; |
Nicholas Troast | 6439e17 | 2017-06-02 14:45:39 -0700 | [diff] [blame] | 4642 | smblib_usb_typec_change(chg); |
Abhijeet Dharmapurikar | 3c93db3 | 2017-04-17 17:36:14 -0700 | [diff] [blame] | 4643 | mutex_unlock(&chg->lock); |
| 4644 | } |
| 4645 | |
Harry Yang | ba874ce | 2016-08-19 14:17:01 -0700 | [diff] [blame] | 4646 | static int smblib_create_votables(struct smb_charger *chg) |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 4647 | { |
| 4648 | int rc = 0; |
| 4649 | |
Ashay Jaiswal | 37b8ea6 | 2017-02-03 11:37:28 +0530 | [diff] [blame] | 4650 | chg->fcc_votable = find_votable("FCC"); |
Harry Yang | 0c35ff6 | 2017-04-06 00:02:30 -0700 | [diff] [blame] | 4651 | if (chg->fcc_votable == NULL) { |
| 4652 | rc = -EINVAL; |
| 4653 | smblib_err(chg, "Couldn't find FCC votable rc=%d\n", rc); |
Ashay Jaiswal | 37b8ea6 | 2017-02-03 11:37:28 +0530 | [diff] [blame] | 4654 | return rc; |
| 4655 | } |
| 4656 | |
| 4657 | chg->fv_votable = find_votable("FV"); |
Harry Yang | 0c35ff6 | 2017-04-06 00:02:30 -0700 | [diff] [blame] | 4658 | if (chg->fv_votable == NULL) { |
| 4659 | rc = -EINVAL; |
| 4660 | smblib_err(chg, "Couldn't find FV votable rc=%d\n", rc); |
Ashay Jaiswal | 37b8ea6 | 2017-02-03 11:37:28 +0530 | [diff] [blame] | 4661 | return rc; |
| 4662 | } |
| 4663 | |
Ashay Jaiswal | ae1586d | 2017-03-22 23:18:51 +0530 | [diff] [blame] | 4664 | chg->usb_icl_votable = find_votable("USB_ICL"); |
| 4665 | if (!chg->usb_icl_votable) { |
Harry Yang | 0c35ff6 | 2017-04-06 00:02:30 -0700 | [diff] [blame] | 4666 | rc = -EINVAL; |
| 4667 | smblib_err(chg, "Couldn't find USB_ICL votable rc=%d\n", rc); |
Ashay Jaiswal | ae1586d | 2017-03-22 23:18:51 +0530 | [diff] [blame] | 4668 | return rc; |
| 4669 | } |
| 4670 | |
Ashay Jaiswal | 37b8ea6 | 2017-02-03 11:37:28 +0530 | [diff] [blame] | 4671 | chg->pl_disable_votable = find_votable("PL_DISABLE"); |
Harry Yang | 0c35ff6 | 2017-04-06 00:02:30 -0700 | [diff] [blame] | 4672 | if (chg->pl_disable_votable == NULL) { |
| 4673 | rc = -EINVAL; |
| 4674 | smblib_err(chg, "Couldn't find votable PL_DISABLE rc=%d\n", rc); |
Ashay Jaiswal | 37b8ea6 | 2017-02-03 11:37:28 +0530 | [diff] [blame] | 4675 | return rc; |
| 4676 | } |
Abhijeet Dharmapurikar | 38ef142 | 2017-05-18 15:37:56 -0700 | [diff] [blame] | 4677 | |
| 4678 | chg->pl_enable_votable_indirect = find_votable("PL_ENABLE_INDIRECT"); |
| 4679 | if (chg->pl_enable_votable_indirect == NULL) { |
| 4680 | rc = -EINVAL; |
| 4681 | smblib_err(chg, |
| 4682 | "Couldn't find votable PL_ENABLE_INDIRECT rc=%d\n", |
| 4683 | rc); |
| 4684 | return rc; |
| 4685 | } |
| 4686 | |
Ashay Jaiswal | c036167 | 2017-03-21 12:24:16 +0530 | [diff] [blame] | 4687 | vote(chg->pl_disable_votable, PL_DELAY_VOTER, true, 0); |
Ashay Jaiswal | 37b8ea6 | 2017-02-03 11:37:28 +0530 | [diff] [blame] | 4688 | |
Abhijeet Dharmapurikar | 8e9e757 | 2016-06-06 16:13:14 -0700 | [diff] [blame] | 4689 | chg->dc_suspend_votable = create_votable("DC_SUSPEND", VOTE_SET_ANY, |
| 4690 | smblib_dc_suspend_vote_callback, |
| 4691 | chg); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 4692 | if (IS_ERR(chg->dc_suspend_votable)) { |
| 4693 | rc = PTR_ERR(chg->dc_suspend_votable); |
| 4694 | return rc; |
| 4695 | } |
| 4696 | |
Abhijeet Dharmapurikar | 8e9e757 | 2016-06-06 16:13:14 -0700 | [diff] [blame] | 4697 | chg->dc_icl_votable = create_votable("DC_ICL", VOTE_MIN, |
| 4698 | smblib_dc_icl_vote_callback, |
| 4699 | chg); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 4700 | if (IS_ERR(chg->dc_icl_votable)) { |
| 4701 | rc = PTR_ERR(chg->dc_icl_votable); |
| 4702 | return rc; |
| 4703 | } |
| 4704 | |
Abhijeet Dharmapurikar | 4b13c6e | 2016-10-05 15:15:10 -0700 | [diff] [blame] | 4705 | chg->pd_disallowed_votable_indirect |
| 4706 | = create_votable("PD_DISALLOWED_INDIRECT", VOTE_SET_ANY, |
| 4707 | smblib_pd_disallowed_votable_indirect_callback, chg); |
| 4708 | if (IS_ERR(chg->pd_disallowed_votable_indirect)) { |
| 4709 | rc = PTR_ERR(chg->pd_disallowed_votable_indirect); |
| 4710 | return rc; |
| 4711 | } |
| 4712 | |
| 4713 | chg->pd_allowed_votable = create_votable("PD_ALLOWED", |
| 4714 | VOTE_SET_ANY, NULL, NULL); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 4715 | if (IS_ERR(chg->pd_allowed_votable)) { |
| 4716 | rc = PTR_ERR(chg->pd_allowed_votable); |
| 4717 | return rc; |
| 4718 | } |
| 4719 | |
Harry Yang | 223c628 | 2016-06-14 15:48:36 -0700 | [diff] [blame] | 4720 | chg->awake_votable = create_votable("AWAKE", VOTE_SET_ANY, |
| 4721 | smblib_awake_vote_callback, |
| 4722 | chg); |
| 4723 | if (IS_ERR(chg->awake_votable)) { |
| 4724 | rc = PTR_ERR(chg->awake_votable); |
| 4725 | return rc; |
| 4726 | } |
| 4727 | |
Abhijeet Dharmapurikar | ee54de0 | 2016-07-08 14:51:47 -0700 | [diff] [blame] | 4728 | chg->chg_disable_votable = create_votable("CHG_DISABLE", VOTE_SET_ANY, |
| 4729 | smblib_chg_disable_vote_callback, |
| 4730 | chg); |
| 4731 | if (IS_ERR(chg->chg_disable_votable)) { |
| 4732 | rc = PTR_ERR(chg->chg_disable_votable); |
| 4733 | return rc; |
| 4734 | } |
| 4735 | |
Harry Yang | aba1f5f | 2016-09-28 10:47:29 -0700 | [diff] [blame] | 4736 | |
Ashay Jaiswal | 4aa2c0c | 2016-12-07 19:39:38 +0530 | [diff] [blame] | 4737 | chg->hvdcp_disable_votable_indirect = create_votable( |
| 4738 | "HVDCP_DISABLE_INDIRECT", |
| 4739 | VOTE_SET_ANY, |
| 4740 | smblib_hvdcp_disable_indirect_vote_callback, |
| 4741 | chg); |
| 4742 | if (IS_ERR(chg->hvdcp_disable_votable_indirect)) { |
| 4743 | rc = PTR_ERR(chg->hvdcp_disable_votable_indirect); |
| 4744 | return rc; |
| 4745 | } |
| 4746 | |
| 4747 | chg->hvdcp_enable_votable = create_votable("HVDCP_ENABLE", |
Abhijeet Dharmapurikar | f0b0a04 | 2016-10-10 16:43:43 -0700 | [diff] [blame] | 4748 | VOTE_SET_ANY, |
Ashay Jaiswal | 4aa2c0c | 2016-12-07 19:39:38 +0530 | [diff] [blame] | 4749 | smblib_hvdcp_enable_vote_callback, |
Abhijeet Dharmapurikar | f0b0a04 | 2016-10-10 16:43:43 -0700 | [diff] [blame] | 4750 | chg); |
Ashay Jaiswal | 4aa2c0c | 2016-12-07 19:39:38 +0530 | [diff] [blame] | 4751 | if (IS_ERR(chg->hvdcp_enable_votable)) { |
| 4752 | rc = PTR_ERR(chg->hvdcp_enable_votable); |
Abhijeet Dharmapurikar | f0b0a04 | 2016-10-10 16:43:43 -0700 | [diff] [blame] | 4753 | return rc; |
| 4754 | } |
Abhijeet Dharmapurikar | f8a7a4a | 2016-10-07 18:46:45 -0700 | [diff] [blame] | 4755 | |
| 4756 | chg->apsd_disable_votable = create_votable("APSD_DISABLE", |
| 4757 | VOTE_SET_ANY, |
| 4758 | smblib_apsd_disable_vote_callback, |
| 4759 | chg); |
| 4760 | if (IS_ERR(chg->apsd_disable_votable)) { |
| 4761 | rc = PTR_ERR(chg->apsd_disable_votable); |
| 4762 | return rc; |
| 4763 | } |
| 4764 | |
Ashay Jaiswal | 6d308da | 2017-02-18 09:59:23 +0530 | [diff] [blame] | 4765 | chg->hvdcp_hw_inov_dis_votable = create_votable("HVDCP_HW_INOV_DIS", |
| 4766 | VOTE_SET_ANY, |
| 4767 | smblib_hvdcp_hw_inov_dis_vote_callback, |
| 4768 | chg); |
| 4769 | if (IS_ERR(chg->hvdcp_hw_inov_dis_votable)) { |
| 4770 | rc = PTR_ERR(chg->hvdcp_hw_inov_dis_votable); |
| 4771 | return rc; |
| 4772 | } |
| 4773 | |
Harry Yang | 4bf7d96 | 2017-03-13 16:51:43 -0700 | [diff] [blame] | 4774 | chg->usb_irq_enable_votable = create_votable("USB_IRQ_DISABLE", |
| 4775 | VOTE_SET_ANY, |
| 4776 | smblib_usb_irq_enable_vote_callback, |
| 4777 | chg); |
| 4778 | if (IS_ERR(chg->usb_irq_enable_votable)) { |
| 4779 | rc = PTR_ERR(chg->usb_irq_enable_votable); |
| 4780 | return rc; |
| 4781 | } |
| 4782 | |
Abhijeet Dharmapurikar | 3c93db3 | 2017-04-17 17:36:14 -0700 | [diff] [blame] | 4783 | chg->typec_irq_disable_votable = create_votable("TYPEC_IRQ_DISABLE", |
| 4784 | VOTE_SET_ANY, |
| 4785 | smblib_typec_irq_disable_vote_callback, |
| 4786 | chg); |
| 4787 | if (IS_ERR(chg->typec_irq_disable_votable)) { |
| 4788 | rc = PTR_ERR(chg->typec_irq_disable_votable); |
| 4789 | return rc; |
| 4790 | } |
| 4791 | |
Nicholas Troast | 320839e | 2016-06-03 10:18:00 -0700 | [diff] [blame] | 4792 | return rc; |
| 4793 | } |
| 4794 | |
Harry Yang | ba874ce | 2016-08-19 14:17:01 -0700 | [diff] [blame] | 4795 | static void smblib_destroy_votables(struct smb_charger *chg) |
| 4796 | { |
Harry Yang | ba874ce | 2016-08-19 14:17:01 -0700 | [diff] [blame] | 4797 | if (chg->dc_suspend_votable) |
| 4798 | destroy_votable(chg->dc_suspend_votable); |
Harry Yang | ba874ce | 2016-08-19 14:17:01 -0700 | [diff] [blame] | 4799 | if (chg->usb_icl_votable) |
| 4800 | destroy_votable(chg->usb_icl_votable); |
| 4801 | if (chg->dc_icl_votable) |
| 4802 | destroy_votable(chg->dc_icl_votable); |
Abhijeet Dharmapurikar | 4b13c6e | 2016-10-05 15:15:10 -0700 | [diff] [blame] | 4803 | if (chg->pd_disallowed_votable_indirect) |
| 4804 | destroy_votable(chg->pd_disallowed_votable_indirect); |
Harry Yang | ba874ce | 2016-08-19 14:17:01 -0700 | [diff] [blame] | 4805 | if (chg->pd_allowed_votable) |
| 4806 | destroy_votable(chg->pd_allowed_votable); |
| 4807 | if (chg->awake_votable) |
| 4808 | destroy_votable(chg->awake_votable); |
Harry Yang | aba1f5f | 2016-09-28 10:47:29 -0700 | [diff] [blame] | 4809 | if (chg->chg_disable_votable) |
| 4810 | destroy_votable(chg->chg_disable_votable); |
Abhijeet Dharmapurikar | f8a7a4a | 2016-10-07 18:46:45 -0700 | [diff] [blame] | 4811 | if (chg->apsd_disable_votable) |
| 4812 | destroy_votable(chg->apsd_disable_votable); |
Ashay Jaiswal | 6d308da | 2017-02-18 09:59:23 +0530 | [diff] [blame] | 4813 | if (chg->hvdcp_hw_inov_dis_votable) |
| 4814 | destroy_votable(chg->hvdcp_hw_inov_dis_votable); |
Abhijeet Dharmapurikar | 3c93db3 | 2017-04-17 17:36:14 -0700 | [diff] [blame] | 4815 | if (chg->typec_irq_disable_votable) |
| 4816 | destroy_votable(chg->typec_irq_disable_votable); |
Harry Yang | ba874ce | 2016-08-19 14:17:01 -0700 | [diff] [blame] | 4817 | } |
| 4818 | |
| 4819 | static void smblib_iio_deinit(struct smb_charger *chg) |
| 4820 | { |
| 4821 | if (!IS_ERR_OR_NULL(chg->iio.temp_chan)) |
| 4822 | iio_channel_release(chg->iio.temp_chan); |
| 4823 | if (!IS_ERR_OR_NULL(chg->iio.temp_max_chan)) |
| 4824 | iio_channel_release(chg->iio.temp_max_chan); |
| 4825 | if (!IS_ERR_OR_NULL(chg->iio.usbin_i_chan)) |
| 4826 | iio_channel_release(chg->iio.usbin_i_chan); |
| 4827 | if (!IS_ERR_OR_NULL(chg->iio.usbin_v_chan)) |
| 4828 | iio_channel_release(chg->iio.usbin_v_chan); |
Nicholas Troast | 7dbcad2 | 2016-10-05 13:30:18 -0700 | [diff] [blame] | 4829 | if (!IS_ERR_OR_NULL(chg->iio.batt_i_chan)) |
| 4830 | iio_channel_release(chg->iio.batt_i_chan); |
Harry Yang | ba874ce | 2016-08-19 14:17:01 -0700 | [diff] [blame] | 4831 | } |
| 4832 | |
Nicholas Troast | 320839e | 2016-06-03 10:18:00 -0700 | [diff] [blame] | 4833 | int smblib_init(struct smb_charger *chg) |
| 4834 | { |
| 4835 | int rc = 0; |
| 4836 | |
Abhijeet Dharmapurikar | 11330d9 | 2017-04-17 12:15:19 -0700 | [diff] [blame] | 4837 | mutex_init(&chg->lock); |
Nicholas Troast | 320839e | 2016-06-03 10:18:00 -0700 | [diff] [blame] | 4838 | mutex_init(&chg->write_lock); |
Nicholas Troast | b11015f | 2017-01-17 17:56:45 -0800 | [diff] [blame] | 4839 | mutex_init(&chg->otg_oc_lock); |
Nicholas Troast | 85d3a5a | 2017-05-03 10:19:43 -0700 | [diff] [blame] | 4840 | mutex_init(&chg->vconn_oc_lock); |
Harry Yang | fe91384 | 2016-08-10 12:27:28 -0700 | [diff] [blame] | 4841 | INIT_WORK(&chg->bms_update_work, bms_update_work); |
Harry Yang | 166b15d | 2017-08-28 13:00:40 -0700 | [diff] [blame^] | 4842 | INIT_WORK(&chg->pl_update_work, pl_update_work); |
Harry Yang | 755a34b | 2016-11-01 01:18:51 -0700 | [diff] [blame] | 4843 | INIT_WORK(&chg->rdstd_cc2_detach_work, rdstd_cc2_detach_work); |
Nicholas Troast | 320839e | 2016-06-03 10:18:00 -0700 | [diff] [blame] | 4844 | INIT_DELAYED_WORK(&chg->hvdcp_detect_work, smblib_hvdcp_detect_work); |
Abhijeet Dharmapurikar | 0e36900 | 2016-09-07 17:25:36 -0700 | [diff] [blame] | 4845 | INIT_DELAYED_WORK(&chg->clear_hdc_work, clear_hdc_work); |
Nicholas Troast | b11015f | 2017-01-17 17:56:45 -0800 | [diff] [blame] | 4846 | INIT_WORK(&chg->otg_oc_work, smblib_otg_oc_work); |
| 4847 | INIT_WORK(&chg->vconn_oc_work, smblib_vconn_oc_work); |
| 4848 | INIT_DELAYED_WORK(&chg->otg_ss_done_work, smblib_otg_ss_done_work); |
Ashay Jaiswal | 4d82578 | 2017-02-18 10:11:34 +0530 | [diff] [blame] | 4849 | INIT_DELAYED_WORK(&chg->icl_change_work, smblib_icl_change_work); |
Ashay Jaiswal | c036167 | 2017-03-21 12:24:16 +0530 | [diff] [blame] | 4850 | INIT_DELAYED_WORK(&chg->pl_enable_work, smblib_pl_enable_work); |
Abhijeet Dharmapurikar | 3c93db3 | 2017-04-17 17:36:14 -0700 | [diff] [blame] | 4851 | INIT_WORK(&chg->legacy_detection_work, smblib_legacy_detection_work); |
Ashay Jaiswal | 1bf4133 | 2017-05-03 15:10:25 +0530 | [diff] [blame] | 4852 | INIT_DELAYED_WORK(&chg->uusb_otg_work, smblib_uusb_otg_work); |
Anirudh Ghayal | 36feefe | 2017-05-26 09:41:25 +0530 | [diff] [blame] | 4853 | INIT_DELAYED_WORK(&chg->bb_removal_work, smblib_bb_removal_work); |
Abhijeet Dharmapurikar | 9e7e48c | 2016-08-23 12:55:49 -0700 | [diff] [blame] | 4854 | chg->fake_capacity = -EINVAL; |
Abhijeet Dharmapurikar | 2ec2a79 | 2017-05-01 20:00:25 -0700 | [diff] [blame] | 4855 | chg->fake_input_current_limited = -EINVAL; |
Harry Yang | 589dd42 | 2017-07-28 18:41:42 -0700 | [diff] [blame] | 4856 | chg->fake_batt_status = -EINVAL; |
Nicholas Troast | 320839e | 2016-06-03 10:18:00 -0700 | [diff] [blame] | 4857 | |
| 4858 | switch (chg->mode) { |
| 4859 | case PARALLEL_MASTER: |
Harry Yang | 0c35ff6 | 2017-04-06 00:02:30 -0700 | [diff] [blame] | 4860 | rc = qcom_batt_init(); |
| 4861 | if (rc < 0) { |
| 4862 | smblib_err(chg, "Couldn't init qcom_batt_init rc=%d\n", |
| 4863 | rc); |
| 4864 | return rc; |
| 4865 | } |
| 4866 | |
Ashay Jaiswal | 9aba44a | 2017-07-20 17:41:36 +0530 | [diff] [blame] | 4867 | rc = qcom_step_chg_init(chg->step_chg_enabled, |
| 4868 | chg->sw_jeita_enabled); |
Anirudh Ghayal | 9d0196d | 2017-07-23 23:02:48 +0530 | [diff] [blame] | 4869 | if (rc < 0) { |
| 4870 | smblib_err(chg, "Couldn't init qcom_step_chg_init rc=%d\n", |
| 4871 | rc); |
| 4872 | return rc; |
| 4873 | } |
| 4874 | |
Nicholas Troast | 320839e | 2016-06-03 10:18:00 -0700 | [diff] [blame] | 4875 | rc = smblib_create_votables(chg); |
| 4876 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 4877 | smblib_err(chg, "Couldn't create votables rc=%d\n", |
Nicholas Troast | 320839e | 2016-06-03 10:18:00 -0700 | [diff] [blame] | 4878 | rc); |
Harry Yang | 58a9e7a | 2016-06-23 14:54:43 -0700 | [diff] [blame] | 4879 | return rc; |
Nicholas Troast | 320839e | 2016-06-03 10:18:00 -0700 | [diff] [blame] | 4880 | } |
Harry Yang | 58a9e7a | 2016-06-23 14:54:43 -0700 | [diff] [blame] | 4881 | |
Harry Yang | 5e1a522 | 2016-07-26 15:16:04 -0700 | [diff] [blame] | 4882 | rc = smblib_register_notifier(chg); |
| 4883 | if (rc < 0) { |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 4884 | smblib_err(chg, |
Harry Yang | 5e1a522 | 2016-07-26 15:16:04 -0700 | [diff] [blame] | 4885 | "Couldn't register notifier rc=%d\n", rc); |
| 4886 | return rc; |
Harry Yang | 58a9e7a | 2016-06-23 14:54:43 -0700 | [diff] [blame] | 4887 | } |
| 4888 | |
Harry Yang | 995b742 | 2016-08-29 16:06:50 -0700 | [diff] [blame] | 4889 | chg->bms_psy = power_supply_get_by_name("bms"); |
Ashay Jaiswal | 8afedfc | 2017-02-03 11:18:22 +0530 | [diff] [blame] | 4890 | chg->pl.psy = power_supply_get_by_name("parallel"); |
Harry Yang | 166b15d | 2017-08-28 13:00:40 -0700 | [diff] [blame^] | 4891 | if (chg->pl.psy) { |
| 4892 | rc = smblib_stat_sw_override_cfg(chg, false); |
| 4893 | if (rc < 0) { |
| 4894 | smblib_err(chg, |
| 4895 | "Couldn't config stat sw rc=%d\n", rc); |
| 4896 | return rc; |
| 4897 | } |
| 4898 | } |
Nicholas Troast | 320839e | 2016-06-03 10:18:00 -0700 | [diff] [blame] | 4899 | break; |
| 4900 | case PARALLEL_SLAVE: |
| 4901 | break; |
| 4902 | default: |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 4903 | smblib_err(chg, "Unsupported mode %d\n", chg->mode); |
Nicholas Troast | 320839e | 2016-06-03 10:18:00 -0700 | [diff] [blame] | 4904 | return -EINVAL; |
| 4905 | } |
| 4906 | |
| 4907 | return rc; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 4908 | } |
Abhijeet Dharmapurikar | 8e9e757 | 2016-06-06 16:13:14 -0700 | [diff] [blame] | 4909 | |
| 4910 | int smblib_deinit(struct smb_charger *chg) |
| 4911 | { |
Harry Yang | ba874ce | 2016-08-19 14:17:01 -0700 | [diff] [blame] | 4912 | switch (chg->mode) { |
| 4913 | case PARALLEL_MASTER: |
Harry Yang | 0c35ff6 | 2017-04-06 00:02:30 -0700 | [diff] [blame] | 4914 | cancel_work_sync(&chg->bms_update_work); |
Harry Yang | 166b15d | 2017-08-28 13:00:40 -0700 | [diff] [blame^] | 4915 | cancel_work_sync(&chg->pl_update_work); |
Harry Yang | 0c35ff6 | 2017-04-06 00:02:30 -0700 | [diff] [blame] | 4916 | cancel_work_sync(&chg->rdstd_cc2_detach_work); |
| 4917 | cancel_delayed_work_sync(&chg->hvdcp_detect_work); |
Harry Yang | 0c35ff6 | 2017-04-06 00:02:30 -0700 | [diff] [blame] | 4918 | cancel_delayed_work_sync(&chg->clear_hdc_work); |
| 4919 | cancel_work_sync(&chg->otg_oc_work); |
| 4920 | cancel_work_sync(&chg->vconn_oc_work); |
| 4921 | cancel_delayed_work_sync(&chg->otg_ss_done_work); |
| 4922 | cancel_delayed_work_sync(&chg->icl_change_work); |
| 4923 | cancel_delayed_work_sync(&chg->pl_enable_work); |
| 4924 | cancel_work_sync(&chg->legacy_detection_work); |
Ashay Jaiswal | 1bf4133 | 2017-05-03 15:10:25 +0530 | [diff] [blame] | 4925 | cancel_delayed_work_sync(&chg->uusb_otg_work); |
Anirudh Ghayal | 36feefe | 2017-05-26 09:41:25 +0530 | [diff] [blame] | 4926 | cancel_delayed_work_sync(&chg->bb_removal_work); |
Harry Yang | ba874ce | 2016-08-19 14:17:01 -0700 | [diff] [blame] | 4927 | power_supply_unreg_notifier(&chg->nb); |
| 4928 | smblib_destroy_votables(chg); |
Anirudh Ghayal | 9d0196d | 2017-07-23 23:02:48 +0530 | [diff] [blame] | 4929 | qcom_step_chg_deinit(); |
Harry Yang | 0c35ff6 | 2017-04-06 00:02:30 -0700 | [diff] [blame] | 4930 | qcom_batt_deinit(); |
Harry Yang | ba874ce | 2016-08-19 14:17:01 -0700 | [diff] [blame] | 4931 | break; |
| 4932 | case PARALLEL_SLAVE: |
| 4933 | break; |
| 4934 | default: |
Abhijeet Dharmapurikar | 31b98f3 | 2016-10-14 12:25:23 -0700 | [diff] [blame] | 4935 | smblib_err(chg, "Unsupported mode %d\n", chg->mode); |
Harry Yang | ba874ce | 2016-08-19 14:17:01 -0700 | [diff] [blame] | 4936 | return -EINVAL; |
| 4937 | } |
Abhijeet Dharmapurikar | 8e9e757 | 2016-06-06 16:13:14 -0700 | [diff] [blame] | 4938 | |
Harry Yang | ba874ce | 2016-08-19 14:17:01 -0700 | [diff] [blame] | 4939 | smblib_iio_deinit(chg); |
Harry Yang | 5e1a522 | 2016-07-26 15:16:04 -0700 | [diff] [blame] | 4940 | |
Abhijeet Dharmapurikar | 8e9e757 | 2016-06-06 16:13:14 -0700 | [diff] [blame] | 4941 | return 0; |
| 4942 | } |