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