| 1 |
// SPDX-License-Identifier: GPL-2.0+ |
| 2 |
/* |
| 3 |
* Copyright 2015-2017 Google, Inc |
| 4 |
* |
| 5 |
* USB Type-C Port Controller Interface. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <linux/bitfield.h> |
| 9 |
#include <linux/delay.h> |
| 10 |
#include <linux/kernel.h> |
| 11 |
#include <linux/module.h> |
| 12 |
#include <linux/i2c.h> |
| 13 |
#include <linux/interrupt.h> |
| 14 |
#include <linux/property.h> |
| 15 |
#include <linux/regmap.h> |
| 16 |
#include <linux/usb/pd.h> |
| 17 |
#include <linux/usb/tcpci.h> |
| 18 |
#include <linux/usb/tcpm.h> |
| 19 |
#include <linux/usb/typec.h> |
| 20 |
#include <linux/regulator/consumer.h> |
| 21 |
|
| 22 |
#define PD_RETRY_COUNT_DEFAULT 3 |
| 23 |
#define PD_RETRY_COUNT_3_0_OR_HIGHER 2 |
| 24 |
#define AUTO_DISCHARGE_DEFAULT_THRESHOLD_MV 3500 |
| 25 |
#define VSINKPD_MIN_IR_DROP_MV 750 |
| 26 |
#define VSRC_NEW_MIN_PERCENT 95 |
| 27 |
#define VSRC_VALID_MIN_MV 500 |
| 28 |
#define VPPS_NEW_MIN_PERCENT 95 |
| 29 |
#define VPPS_VALID_MIN_MV 100 |
| 30 |
#define VSINKDISCONNECT_PD_MIN_PERCENT 90 |
| 31 |
#define VPPS_SHUTDOWN_MIN_PERCENT 85 |
| 32 |
|
| 33 |
struct tcpci { |
| 34 |
struct device *dev; |
| 35 |
|
| 36 |
struct tcpm_port *port; |
| 37 |
|
| 38 |
struct regmap *regmap; |
| 39 |
unsigned int alert_mask; |
| 40 |
|
| 41 |
bool controls_vbus; |
| 42 |
|
| 43 |
struct tcpc_dev tcpc; |
| 44 |
struct tcpci_data *data; |
| 45 |
}; |
| 46 |
|
| 47 |
struct tcpci_chip { |
| 48 |
struct tcpci *tcpci; |
| 49 |
struct tcpci_data data; |
| 50 |
}; |
| 51 |
|
| 52 |
struct tcpm_port *tcpci_get_tcpm_port(struct tcpci *tcpci) |
| 53 |
{ |
| 54 |
return tcpci->port; |
| 55 |
} |
| 56 |
EXPORT_SYMBOL_GPL(tcpci_get_tcpm_port); |
| 57 |
|
| 58 |
static inline struct tcpci *tcpc_to_tcpci(struct tcpc_dev *tcpc) |
| 59 |
{ |
| 60 |
return container_of(tcpc, struct tcpci, tcpc); |
| 61 |
} |
| 62 |
|
| 63 |
static int tcpci_read16(struct tcpci *tcpci, unsigned int reg, u16 *val) |
| 64 |
{ |
| 65 |
return regmap_raw_read(tcpci->regmap, reg, val, sizeof(u16)); |
| 66 |
} |
| 67 |
|
| 68 |
static int tcpci_write16(struct tcpci *tcpci, unsigned int reg, u16 val) |
| 69 |
{ |
| 70 |
return regmap_raw_write(tcpci->regmap, reg, &val, sizeof(u16)); |
| 71 |
} |
| 72 |
|
| 73 |
static int tcpci_check_std_output_cap(struct regmap *regmap, u8 mask) |
| 74 |
{ |
| 75 |
unsigned int reg; |
| 76 |
int ret; |
| 77 |
|
| 78 |
ret = regmap_read(regmap, TCPC_STD_OUTPUT_CAP, ®); |
| 79 |
if (ret < 0) |
| 80 |
return ret; |
| 81 |
|
| 82 |
return (reg & mask) == mask; |
| 83 |
} |
| 84 |
|
| 85 |
static int tcpci_set_cc(struct tcpc_dev *tcpc, enum typec_cc_status cc) |
| 86 |
{ |
| 87 |
struct tcpci *tcpci = tcpc_to_tcpci(tcpc); |
| 88 |
bool vconn_pres; |
| 89 |
enum typec_cc_polarity polarity = TYPEC_POLARITY_CC1; |
| 90 |
unsigned int reg; |
| 91 |
int ret; |
| 92 |
|
| 93 |
ret = regmap_read(tcpci->regmap, TCPC_POWER_STATUS, ®); |
| 94 |
if (ret < 0) |
| 95 |
return ret; |
| 96 |
|
| 97 |
vconn_pres = !!(reg & TCPC_POWER_STATUS_VCONN_PRES); |
| 98 |
if (vconn_pres) { |
| 99 |
ret = regmap_read(tcpci->regmap, TCPC_TCPC_CTRL, ®); |
| 100 |
if (ret < 0) |
| 101 |
return ret; |
| 102 |
|
| 103 |
if (reg & TCPC_TCPC_CTRL_ORIENTATION) |
| 104 |
polarity = TYPEC_POLARITY_CC2; |
| 105 |
} |
| 106 |
|
| 107 |
switch (cc) { |
| 108 |
case TYPEC_CC_RA: |
| 109 |
reg = (FIELD_PREP(TCPC_ROLE_CTRL_CC1, TCPC_ROLE_CTRL_CC_RA) |
| 110 |
| FIELD_PREP(TCPC_ROLE_CTRL_CC2, TCPC_ROLE_CTRL_CC_RA)); |
| 111 |
break; |
| 112 |
case TYPEC_CC_RD: |
| 113 |
reg = (FIELD_PREP(TCPC_ROLE_CTRL_CC1, TCPC_ROLE_CTRL_CC_RD) |
| 114 |
| FIELD_PREP(TCPC_ROLE_CTRL_CC2, TCPC_ROLE_CTRL_CC_RD)); |
| 115 |
break; |
| 116 |
case TYPEC_CC_RP_DEF: |
| 117 |
reg = (FIELD_PREP(TCPC_ROLE_CTRL_CC1, TCPC_ROLE_CTRL_CC_RP) |
| 118 |
| FIELD_PREP(TCPC_ROLE_CTRL_CC2, TCPC_ROLE_CTRL_CC_RP) |
| 119 |
| FIELD_PREP(TCPC_ROLE_CTRL_RP_VAL, |
| 120 |
TCPC_ROLE_CTRL_RP_VAL_DEF)); |
| 121 |
break; |
| 122 |
case TYPEC_CC_RP_1_5: |
| 123 |
reg = (FIELD_PREP(TCPC_ROLE_CTRL_CC1, TCPC_ROLE_CTRL_CC_RP) |
| 124 |
| FIELD_PREP(TCPC_ROLE_CTRL_CC2, TCPC_ROLE_CTRL_CC_RP) |
| 125 |
| FIELD_PREP(TCPC_ROLE_CTRL_RP_VAL, |
| 126 |
TCPC_ROLE_CTRL_RP_VAL_1_5)); |
| 127 |
break; |
| 128 |
case TYPEC_CC_RP_3_0: |
| 129 |
reg = (FIELD_PREP(TCPC_ROLE_CTRL_CC1, TCPC_ROLE_CTRL_CC_RP) |
| 130 |
| FIELD_PREP(TCPC_ROLE_CTRL_CC2, TCPC_ROLE_CTRL_CC_RP) |
| 131 |
| FIELD_PREP(TCPC_ROLE_CTRL_RP_VAL, |
| 132 |
TCPC_ROLE_CTRL_RP_VAL_3_0)); |
| 133 |
break; |
| 134 |
case TYPEC_CC_OPEN: |
| 135 |
default: |
| 136 |
reg = (FIELD_PREP(TCPC_ROLE_CTRL_CC1, TCPC_ROLE_CTRL_CC_OPEN) |
| 137 |
| FIELD_PREP(TCPC_ROLE_CTRL_CC2, TCPC_ROLE_CTRL_CC_OPEN)); |
| 138 |
break; |
| 139 |
} |
| 140 |
|
| 141 |
if (vconn_pres) { |
| 142 |
if (polarity == TYPEC_POLARITY_CC2) { |
| 143 |
reg &= ~TCPC_ROLE_CTRL_CC1; |
| 144 |
reg |= FIELD_PREP(TCPC_ROLE_CTRL_CC1, TCPC_ROLE_CTRL_CC_OPEN); |
| 145 |
} else { |
| 146 |
reg &= ~TCPC_ROLE_CTRL_CC2; |
| 147 |
reg |= FIELD_PREP(TCPC_ROLE_CTRL_CC2, TCPC_ROLE_CTRL_CC_OPEN); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
ret = regmap_write(tcpci->regmap, TCPC_ROLE_CTRL, reg); |
| 152 |
if (ret < 0) |
| 153 |
return ret; |
| 154 |
|
| 155 |
return 0; |
| 156 |
} |
| 157 |
|
| 158 |
static int tcpci_apply_rc(struct tcpc_dev *tcpc, enum typec_cc_status cc, |
| 159 |
enum typec_cc_polarity polarity) |
| 160 |
{ |
| 161 |
struct tcpci *tcpci = tcpc_to_tcpci(tcpc); |
| 162 |
unsigned int reg; |
| 163 |
int ret; |
| 164 |
|
| 165 |
ret = regmap_read(tcpci->regmap, TCPC_ROLE_CTRL, ®); |
| 166 |
if (ret < 0) |
| 167 |
return ret; |
| 168 |
|
| 169 |
/* |
| 170 |
* APPLY_RC state is when ROLE_CONTROL.CC1 != ROLE_CONTROL.CC2 and vbus autodischarge on |
| 171 |
* disconnect is disabled. Bail out when ROLE_CONTROL.CC1 != ROLE_CONTROL.CC2. |
| 172 |
*/ |
| 173 |
if (FIELD_GET(TCPC_ROLE_CTRL_CC2, reg) != FIELD_GET(TCPC_ROLE_CTRL_CC1, reg)) |
| 174 |
return 0; |
| 175 |
|
| 176 |
return regmap_update_bits(tcpci->regmap, TCPC_ROLE_CTRL, polarity == TYPEC_POLARITY_CC1 ? |
| 177 |
TCPC_ROLE_CTRL_CC2 : TCPC_ROLE_CTRL_CC1, |
| 178 |
TCPC_ROLE_CTRL_CC_OPEN); |
| 179 |
} |
| 180 |
|
| 181 |
static int tcpci_start_toggling(struct tcpc_dev *tcpc, |
| 182 |
enum typec_port_type port_type, |
| 183 |
enum typec_cc_status cc) |
| 184 |
{ |
| 185 |
int ret; |
| 186 |
struct tcpci *tcpci = tcpc_to_tcpci(tcpc); |
| 187 |
unsigned int reg = TCPC_ROLE_CTRL_DRP; |
| 188 |
|
| 189 |
if (port_type != TYPEC_PORT_DRP) |
| 190 |
return -EOPNOTSUPP; |
| 191 |
|
| 192 |
/* Handle vendor drp toggling */ |
| 193 |
if (tcpci->data->start_drp_toggling) { |
| 194 |
ret = tcpci->data->start_drp_toggling(tcpci, tcpci->data, cc); |
| 195 |
if (ret < 0) |
| 196 |
return ret; |
| 197 |
} |
| 198 |
|
| 199 |
switch (cc) { |
| 200 |
default: |
| 201 |
case TYPEC_CC_RP_DEF: |
| 202 |
reg |= FIELD_PREP(TCPC_ROLE_CTRL_RP_VAL, |
| 203 |
TCPC_ROLE_CTRL_RP_VAL_DEF); |
| 204 |
break; |
| 205 |
case TYPEC_CC_RP_1_5: |
| 206 |
reg |= FIELD_PREP(TCPC_ROLE_CTRL_RP_VAL, |
| 207 |
TCPC_ROLE_CTRL_RP_VAL_1_5); |
| 208 |
break; |
| 209 |
case TYPEC_CC_RP_3_0: |
| 210 |
reg |= FIELD_PREP(TCPC_ROLE_CTRL_RP_VAL, |
| 211 |
TCPC_ROLE_CTRL_RP_VAL_3_0); |
| 212 |
break; |
| 213 |
} |
| 214 |
|
| 215 |
if (cc == TYPEC_CC_RD) |
| 216 |
reg |= (FIELD_PREP(TCPC_ROLE_CTRL_CC1, TCPC_ROLE_CTRL_CC_RD) |
| 217 |
| FIELD_PREP(TCPC_ROLE_CTRL_CC2, TCPC_ROLE_CTRL_CC_RD)); |
| 218 |
else |
| 219 |
reg |= (FIELD_PREP(TCPC_ROLE_CTRL_CC1, TCPC_ROLE_CTRL_CC_RP) |
| 220 |
| FIELD_PREP(TCPC_ROLE_CTRL_CC2, TCPC_ROLE_CTRL_CC_RP)); |
| 221 |
ret = regmap_write(tcpci->regmap, TCPC_ROLE_CTRL, reg); |
| 222 |
if (ret < 0) |
| 223 |
return ret; |
| 224 |
return regmap_write(tcpci->regmap, TCPC_COMMAND, |
| 225 |
TCPC_CMD_LOOK4CONNECTION); |
| 226 |
} |
| 227 |
|
| 228 |
static int tcpci_get_cc(struct tcpc_dev *tcpc, |
| 229 |
enum typec_cc_status *cc1, enum typec_cc_status *cc2) |
| 230 |
{ |
| 231 |
struct tcpci *tcpci = tcpc_to_tcpci(tcpc); |
| 232 |
unsigned int reg, role_control; |
| 233 |
int ret; |
| 234 |
|
| 235 |
ret = regmap_read(tcpci->regmap, TCPC_ROLE_CTRL, &role_control); |
| 236 |
if (ret < 0) |
| 237 |
return ret; |
| 238 |
|
| 239 |
ret = regmap_read(tcpci->regmap, TCPC_CC_STATUS, ®); |
| 240 |
if (ret < 0) |
| 241 |
return ret; |
| 242 |
|
| 243 |
*cc1 = tcpci_to_typec_cc(FIELD_GET(TCPC_CC_STATUS_CC1, reg), |
| 244 |
reg & TCPC_CC_STATUS_TERM || |
| 245 |
tcpc_presenting_rd(role_control, CC1)); |
| 246 |
*cc2 = tcpci_to_typec_cc(FIELD_GET(TCPC_CC_STATUS_CC2, reg), |
| 247 |
reg & TCPC_CC_STATUS_TERM || |
| 248 |
tcpc_presenting_rd(role_control, CC2)); |
| 249 |
|
| 250 |
return 0; |
| 251 |
} |
| 252 |
|
| 253 |
static int tcpci_set_polarity(struct tcpc_dev *tcpc, |
| 254 |
enum typec_cc_polarity polarity) |
| 255 |
{ |
| 256 |
struct tcpci *tcpci = tcpc_to_tcpci(tcpc); |
| 257 |
unsigned int reg; |
| 258 |
int ret; |
| 259 |
enum typec_cc_status cc1, cc2; |
| 260 |
|
| 261 |
/* Obtain Rp setting from role control */ |
| 262 |
ret = regmap_read(tcpci->regmap, TCPC_ROLE_CTRL, ®); |
| 263 |
if (ret < 0) |
| 264 |
return ret; |
| 265 |
|
| 266 |
ret = tcpci_get_cc(tcpc, &cc1, &cc2); |
| 267 |
if (ret < 0) |
| 268 |
return ret; |
| 269 |
|
| 270 |
/* |
| 271 |
* When port has drp toggling enabled, ROLE_CONTROL would only have the initial |
| 272 |
* terminations for the toggling and does not indicate the final cc |
| 273 |
* terminations when ConnectionResult is 0 i.e. drp toggling stops and |
| 274 |
* the connection is resolved. Infer port role from TCPC_CC_STATUS based on the |
| 275 |
* terminations seen. The port role is then used to set the cc terminations. |
| 276 |
*/ |
| 277 |
if (reg & TCPC_ROLE_CTRL_DRP) { |
| 278 |
/* Disable DRP for the OPEN setting to take effect */ |
| 279 |
reg = reg & ~TCPC_ROLE_CTRL_DRP; |
| 280 |
|
| 281 |
if (polarity == TYPEC_POLARITY_CC2) { |
| 282 |
reg &= ~TCPC_ROLE_CTRL_CC2; |
| 283 |
/* Local port is source */ |
| 284 |
if (cc2 == TYPEC_CC_RD) |
| 285 |
/* Role control would have the Rp setting when DRP was enabled */ |
| 286 |
reg |= FIELD_PREP(TCPC_ROLE_CTRL_CC2, TCPC_ROLE_CTRL_CC_RP); |
| 287 |
else if (cc2 >= TYPEC_CC_RP_DEF) |
| 288 |
reg |= FIELD_PREP(TCPC_ROLE_CTRL_CC2, TCPC_ROLE_CTRL_CC_RD); |
| 289 |
} else { |
| 290 |
reg &= ~TCPC_ROLE_CTRL_CC1; |
| 291 |
/* Local port is source */ |
| 292 |
if (cc1 == TYPEC_CC_RD) |
| 293 |
/* Role control would have the Rp setting when DRP was enabled */ |
| 294 |
reg |= FIELD_PREP(TCPC_ROLE_CTRL_CC1, TCPC_ROLE_CTRL_CC_RP); |
| 295 |
else if (cc1 >= TYPEC_CC_RP_DEF) |
| 296 |
reg |= FIELD_PREP(TCPC_ROLE_CTRL_CC1, TCPC_ROLE_CTRL_CC_RD); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
if (polarity == TYPEC_POLARITY_CC2) |
| 301 |
reg |= FIELD_PREP(TCPC_ROLE_CTRL_CC1, TCPC_ROLE_CTRL_CC_OPEN); |
| 302 |
else |
| 303 |
reg |= FIELD_PREP(TCPC_ROLE_CTRL_CC2, TCPC_ROLE_CTRL_CC_OPEN); |
| 304 |
ret = regmap_write(tcpci->regmap, TCPC_ROLE_CTRL, reg); |
| 305 |
if (ret < 0) |
| 306 |
return ret; |
| 307 |
|
| 308 |
return regmap_write(tcpci->regmap, TCPC_TCPC_CTRL, |
| 309 |
(polarity == TYPEC_POLARITY_CC2) ? |
| 310 |
TCPC_TCPC_CTRL_ORIENTATION : 0); |
| 311 |
} |
| 312 |
|
| 313 |
static int tcpci_set_orientation(struct tcpc_dev *tcpc, |
| 314 |
enum typec_orientation orientation) |
| 315 |
{ |
| 316 |
struct tcpci *tcpci = tcpc_to_tcpci(tcpc); |
| 317 |
unsigned int reg; |
| 318 |
|
| 319 |
switch (orientation) { |
| 320 |
case TYPEC_ORIENTATION_NONE: |
| 321 |
/* We can't put a single output into high impedance */ |
| 322 |
fallthrough; |
| 323 |
case TYPEC_ORIENTATION_NORMAL: |
| 324 |
reg = TCPC_CONFIG_STD_OUTPUT_ORIENTATION_NORMAL; |
| 325 |
break; |
| 326 |
case TYPEC_ORIENTATION_REVERSE: |
| 327 |
reg = TCPC_CONFIG_STD_OUTPUT_ORIENTATION_FLIPPED; |
| 328 |
break; |
| 329 |
} |
| 330 |
|
| 331 |
return regmap_update_bits(tcpci->regmap, TCPC_CONFIG_STD_OUTPUT, |
| 332 |
TCPC_CONFIG_STD_OUTPUT_ORIENTATION_MASK, reg); |
| 333 |
} |
| 334 |
|
| 335 |
static void tcpci_set_partner_usb_comm_capable(struct tcpc_dev *tcpc, bool capable) |
| 336 |
{ |
| 337 |
struct tcpci *tcpci = tcpc_to_tcpci(tcpc); |
| 338 |
|
| 339 |
if (tcpci->data->set_partner_usb_comm_capable) |
| 340 |
tcpci->data->set_partner_usb_comm_capable(tcpci, tcpci->data, capable); |
| 341 |
} |
| 342 |
|
| 343 |
static int tcpci_set_vconn(struct tcpc_dev *tcpc, bool enable) |
| 344 |
{ |
| 345 |
struct tcpci *tcpci = tcpc_to_tcpci(tcpc); |
| 346 |
int ret; |
| 347 |
|
| 348 |
/* Handle vendor set vconn */ |
| 349 |
if (tcpci->data->set_vconn) { |
| 350 |
ret = tcpci->data->set_vconn(tcpci, tcpci->data, enable); |
| 351 |
if (ret < 0) |
| 352 |
return ret; |
| 353 |
} |
| 354 |
|
| 355 |
return regmap_update_bits(tcpci->regmap, TCPC_POWER_CTRL, |
| 356 |
TCPC_POWER_CTRL_VCONN_ENABLE, |
| 357 |
enable ? TCPC_POWER_CTRL_VCONN_ENABLE : 0); |
| 358 |
} |
| 359 |
|
| 360 |
static int tcpci_enable_auto_vbus_discharge(struct tcpc_dev *dev, bool enable) |
| 361 |
{ |
| 362 |
struct tcpci *tcpci = tcpc_to_tcpci(dev); |
| 363 |
int ret; |
| 364 |
|
| 365 |
ret = regmap_update_bits(tcpci->regmap, TCPC_POWER_CTRL, TCPC_POWER_CTRL_AUTO_DISCHARGE, |
| 366 |
enable ? TCPC_POWER_CTRL_AUTO_DISCHARGE : 0); |
| 367 |
return ret; |
| 368 |
} |
| 369 |
|
| 370 |
static int tcpci_set_auto_vbus_discharge_threshold(struct tcpc_dev *dev, enum typec_pwr_opmode mode, |
| 371 |
bool pps_active, u32 requested_vbus_voltage_mv, |
| 372 |
u32 apdo_min_voltage_mv) |
| 373 |
{ |
| 374 |
struct tcpci *tcpci = tcpc_to_tcpci(dev); |
| 375 |
unsigned int pwr_ctrl, threshold = 0; |
| 376 |
int ret; |
| 377 |
|
| 378 |
/* |
| 379 |
* Indicates that vbus is going to go away due PR_SWAP, hard reset etc. |
| 380 |
* Do not discharge vbus here. |
| 381 |
*/ |
| 382 |
if (requested_vbus_voltage_mv == 0) |
| 383 |
goto write_thresh; |
| 384 |
|
| 385 |
ret = regmap_read(tcpci->regmap, TCPC_POWER_CTRL, &pwr_ctrl); |
| 386 |
if (ret < 0) |
| 387 |
return ret; |
| 388 |
|
| 389 |
if (pwr_ctrl & TCPC_FAST_ROLE_SWAP_EN) { |
| 390 |
/* To prevent disconnect when the source is fast role swap is capable. */ |
| 391 |
threshold = AUTO_DISCHARGE_DEFAULT_THRESHOLD_MV; |
| 392 |
} else if (mode == TYPEC_PWR_MODE_PD) { |
| 393 |
if (pps_active) |
| 394 |
/* |
| 395 |
* To prevent disconnect when the source is in Current Limit Mode. |
| 396 |
* Set the threshold to the lowest possible voltage vPpsShutdown (min) |
| 397 |
*/ |
| 398 |
threshold = VPPS_SHUTDOWN_MIN_PERCENT * apdo_min_voltage_mv / 100 - |
| 399 |
VSINKPD_MIN_IR_DROP_MV; |
| 400 |
else |
| 401 |
threshold = ((VSRC_NEW_MIN_PERCENT * requested_vbus_voltage_mv / 100) - |
| 402 |
VSINKPD_MIN_IR_DROP_MV - VSRC_VALID_MIN_MV) * |
| 403 |
VSINKDISCONNECT_PD_MIN_PERCENT / 100; |
| 404 |
} else { |
| 405 |
/* 3.5V for non-pd sink */ |
| 406 |
threshold = AUTO_DISCHARGE_DEFAULT_THRESHOLD_MV; |
| 407 |
} |
| 408 |
|
| 409 |
threshold = threshold / TCPC_VBUS_SINK_DISCONNECT_THRESH_LSB_MV; |
| 410 |
|
| 411 |
if (threshold > TCPC_VBUS_SINK_DISCONNECT_THRESH_MAX) |
| 412 |
return -EINVAL; |
| 413 |
|
| 414 |
write_thresh: |
| 415 |
return tcpci_write16(tcpci, TCPC_VBUS_SINK_DISCONNECT_THRESH, threshold); |
| 416 |
} |
| 417 |
|
| 418 |
static int tcpci_enable_frs(struct tcpc_dev *dev, bool enable) |
| 419 |
{ |
| 420 |
struct tcpci *tcpci = tcpc_to_tcpci(dev); |
| 421 |
int ret; |
| 422 |
|
| 423 |
/* To prevent disconnect during FRS, set disconnect threshold to 3.5V */ |
| 424 |
ret = tcpci_write16(tcpci, TCPC_VBUS_SINK_DISCONNECT_THRESH, enable ? 0 : 0x8c); |
| 425 |
if (ret < 0) |
| 426 |
return ret; |
| 427 |
|
| 428 |
ret = regmap_update_bits(tcpci->regmap, TCPC_POWER_CTRL, TCPC_FAST_ROLE_SWAP_EN, enable ? |
| 429 |
TCPC_FAST_ROLE_SWAP_EN : 0); |
| 430 |
|
| 431 |
return ret; |
| 432 |
} |
| 433 |
|
| 434 |
static void tcpci_frs_sourcing_vbus(struct tcpc_dev *dev) |
| 435 |
{ |
| 436 |
struct tcpci *tcpci = tcpc_to_tcpci(dev); |
| 437 |
|
| 438 |
if (tcpci->data->frs_sourcing_vbus) |
| 439 |
tcpci->data->frs_sourcing_vbus(tcpci, tcpci->data); |
| 440 |
} |
| 441 |
|
| 442 |
static void tcpci_check_contaminant(struct tcpc_dev *dev) |
| 443 |
{ |
| 444 |
struct tcpci *tcpci = tcpc_to_tcpci(dev); |
| 445 |
|
| 446 |
if (tcpci->data->check_contaminant) |
| 447 |
tcpci->data->check_contaminant(tcpci, tcpci->data); |
| 448 |
} |
| 449 |
|
| 450 |
static int tcpci_set_bist_data(struct tcpc_dev *tcpc, bool enable) |
| 451 |
{ |
| 452 |
struct tcpci *tcpci = tcpc_to_tcpci(tcpc); |
| 453 |
|
| 454 |
return regmap_update_bits(tcpci->regmap, TCPC_TCPC_CTRL, TCPC_TCPC_CTRL_BIST_TM, |
| 455 |
enable ? TCPC_TCPC_CTRL_BIST_TM : 0); |
| 456 |
} |
| 457 |
|
| 458 |
static int tcpci_set_roles(struct tcpc_dev *tcpc, bool attached, |
| 459 |
enum typec_role role, enum typec_data_role data) |
| 460 |
{ |
| 461 |
struct tcpci *tcpci = tcpc_to_tcpci(tcpc); |
| 462 |
unsigned int reg; |
| 463 |
int ret; |
| 464 |
|
| 465 |
reg = FIELD_PREP(TCPC_MSG_HDR_INFO_REV, PD_REV20); |
| 466 |
if (role == TYPEC_SOURCE) |
| 467 |
reg |= TCPC_MSG_HDR_INFO_PWR_ROLE; |
| 468 |
if (data == TYPEC_HOST) |
| 469 |
reg |= TCPC_MSG_HDR_INFO_DATA_ROLE; |
| 470 |
ret = regmap_write(tcpci->regmap, TCPC_MSG_HDR_INFO, reg); |
| 471 |
if (ret < 0) |
| 472 |
return ret; |
| 473 |
|
| 474 |
return 0; |
| 475 |
} |
| 476 |
|
| 477 |
static int tcpci_set_pd_rx(struct tcpc_dev *tcpc, bool enable) |
| 478 |
{ |
| 479 |
struct tcpci *tcpci = tcpc_to_tcpci(tcpc); |
| 480 |
unsigned int reg = 0; |
| 481 |
int ret; |
| 482 |
|
| 483 |
if (enable) { |
| 484 |
reg = TCPC_RX_DETECT_SOP | TCPC_RX_DETECT_HARD_RESET; |
| 485 |
if (tcpci->data->cable_comm_capable) |
| 486 |
reg |= TCPC_RX_DETECT_SOP1; |
| 487 |
} |
| 488 |
ret = regmap_write(tcpci->regmap, TCPC_RX_DETECT, reg); |
| 489 |
if (ret < 0) |
| 490 |
return ret; |
| 491 |
|
| 492 |
return 0; |
| 493 |
} |
| 494 |
|
| 495 |
static int tcpci_get_vbus(struct tcpc_dev *tcpc) |
| 496 |
{ |
| 497 |
struct tcpci *tcpci = tcpc_to_tcpci(tcpc); |
| 498 |
unsigned int reg; |
| 499 |
int ret; |
| 500 |
|
| 501 |
ret = regmap_read(tcpci->regmap, TCPC_POWER_STATUS, ®); |
| 502 |
if (ret < 0) |
| 503 |
return ret; |
| 504 |
|
| 505 |
return !!(reg & TCPC_POWER_STATUS_VBUS_PRES); |
| 506 |
} |
| 507 |
|
| 508 |
static bool tcpci_is_vbus_vsafe0v(struct tcpc_dev *tcpc) |
| 509 |
{ |
| 510 |
struct tcpci *tcpci = tcpc_to_tcpci(tcpc); |
| 511 |
unsigned int reg; |
| 512 |
int ret; |
| 513 |
|
| 514 |
ret = regmap_read(tcpci->regmap, TCPC_EXTENDED_STATUS, ®); |
| 515 |
if (ret < 0) |
| 516 |
return false; |
| 517 |
|
| 518 |
return !!(reg & TCPC_EXTENDED_STATUS_VSAFE0V); |
| 519 |
} |
| 520 |
|
| 521 |
static int tcpci_set_vbus(struct tcpc_dev *tcpc, bool source, bool sink) |
| 522 |
{ |
| 523 |
struct tcpci *tcpci = tcpc_to_tcpci(tcpc); |
| 524 |
int ret; |
| 525 |
|
| 526 |
if (tcpci->data->set_vbus) { |
| 527 |
ret = tcpci->data->set_vbus(tcpci, tcpci->data, source, sink); |
| 528 |
/* Bypass when ret > 0 */ |
| 529 |
if (ret != 0) |
| 530 |
return ret < 0 ? ret : 0; |
| 531 |
} |
| 532 |
|
| 533 |
/* Disable both source and sink first before enabling anything */ |
| 534 |
|
| 535 |
if (!source) { |
| 536 |
ret = regmap_write(tcpci->regmap, TCPC_COMMAND, |
| 537 |
TCPC_CMD_DISABLE_SRC_VBUS); |
| 538 |
if (ret < 0) |
| 539 |
return ret; |
| 540 |
} |
| 541 |
|
| 542 |
if (!sink) { |
| 543 |
ret = regmap_write(tcpci->regmap, TCPC_COMMAND, |
| 544 |
TCPC_CMD_DISABLE_SINK_VBUS); |
| 545 |
if (ret < 0) |
| 546 |
return ret; |
| 547 |
} |
| 548 |
|
| 549 |
if (source) { |
| 550 |
ret = regmap_write(tcpci->regmap, TCPC_COMMAND, |
| 551 |
TCPC_CMD_SRC_VBUS_DEFAULT); |
| 552 |
if (ret < 0) |
| 553 |
return ret; |
| 554 |
} |
| 555 |
|
| 556 |
if (sink) { |
| 557 |
ret = regmap_write(tcpci->regmap, TCPC_COMMAND, |
| 558 |
TCPC_CMD_SINK_VBUS); |
| 559 |
if (ret < 0) |
| 560 |
return ret; |
| 561 |
} |
| 562 |
|
| 563 |
return 0; |
| 564 |
} |
| 565 |
|
| 566 |
static int tcpci_pd_transmit(struct tcpc_dev *tcpc, enum tcpm_transmit_type type, |
| 567 |
const struct pd_message *msg, unsigned int negotiated_rev) |
| 568 |
{ |
| 569 |
struct tcpci *tcpci = tcpc_to_tcpci(tcpc); |
| 570 |
u16 header = msg ? le16_to_cpu(msg->header) : 0; |
| 571 |
unsigned int reg, cnt; |
| 572 |
int ret; |
| 573 |
|
| 574 |
cnt = msg ? pd_header_cnt(header) * 4 : 0; |
| 575 |
/** |
| 576 |
* TCPCI spec forbids direct access of TCPC_TX_DATA. |
| 577 |
* But, since some of the chipsets offer this capability, |
| 578 |
* it's fair to support both. |
| 579 |
*/ |
| 580 |
if (tcpci->data->TX_BUF_BYTE_x_hidden) { |
| 581 |
u8 buf[TCPC_TRANSMIT_BUFFER_MAX_LEN] = {0,}; |
| 582 |
u8 pos = 0; |
| 583 |
|
| 584 |
/* Payload + header + TCPC_TX_BYTE_CNT */ |
| 585 |
buf[pos++] = cnt + 2; |
| 586 |
|
| 587 |
if (msg) |
| 588 |
memcpy(&buf[pos], &msg->header, sizeof(msg->header)); |
| 589 |
|
| 590 |
pos += sizeof(header); |
| 591 |
|
| 592 |
if (cnt > 0) |
| 593 |
memcpy(&buf[pos], msg->payload, cnt); |
| 594 |
|
| 595 |
pos += cnt; |
| 596 |
ret = regmap_raw_write(tcpci->regmap, TCPC_TX_BYTE_CNT, buf, pos); |
| 597 |
if (ret < 0) |
| 598 |
return ret; |
| 599 |
} else { |
| 600 |
ret = regmap_write(tcpci->regmap, TCPC_TX_BYTE_CNT, cnt + 2); |
| 601 |
if (ret < 0) |
| 602 |
return ret; |
| 603 |
|
| 604 |
ret = tcpci_write16(tcpci, TCPC_TX_HDR, header); |
| 605 |
if (ret < 0) |
| 606 |
return ret; |
| 607 |
|
| 608 |
if (cnt > 0) { |
| 609 |
ret = regmap_raw_write(tcpci->regmap, TCPC_TX_DATA, &msg->payload, cnt); |
| 610 |
if (ret < 0) |
| 611 |
return ret; |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
/* nRetryCount is 3 in PD2.0 spec where 2 in PD3.0 spec */ |
| 616 |
reg = FIELD_PREP(TCPC_TRANSMIT_RETRY, |
| 617 |
(negotiated_rev > PD_REV20 |
| 618 |
? PD_RETRY_COUNT_3_0_OR_HIGHER |
| 619 |
: PD_RETRY_COUNT_DEFAULT)); |
| 620 |
reg |= FIELD_PREP(TCPC_TRANSMIT_TYPE, type); |
| 621 |
ret = regmap_write(tcpci->regmap, TCPC_TRANSMIT, reg); |
| 622 |
if (ret < 0) |
| 623 |
return ret; |
| 624 |
|
| 625 |
return 0; |
| 626 |
} |
| 627 |
|
| 628 |
static bool tcpci_cable_comm_capable(struct tcpc_dev *tcpc) |
| 629 |
{ |
| 630 |
struct tcpci *tcpci = tcpc_to_tcpci(tcpc); |
| 631 |
|
| 632 |
return tcpci->data->cable_comm_capable; |
| 633 |
} |
| 634 |
|
| 635 |
static bool tcpci_attempt_vconn_swap_discovery(struct tcpc_dev *tcpc) |
| 636 |
{ |
| 637 |
struct tcpci *tcpci = tcpc_to_tcpci(tcpc); |
| 638 |
|
| 639 |
if (tcpci->data->attempt_vconn_swap_discovery) |
| 640 |
return tcpci->data->attempt_vconn_swap_discovery(tcpci, tcpci->data); |
| 641 |
|
| 642 |
return false; |
| 643 |
} |
| 644 |
|
| 645 |
static int tcpci_init(struct tcpc_dev *tcpc) |
| 646 |
{ |
| 647 |
struct tcpci *tcpci = tcpc_to_tcpci(tcpc); |
| 648 |
unsigned long timeout = jiffies + msecs_to_jiffies(2000); /* XXX */ |
| 649 |
unsigned int reg; |
| 650 |
int ret; |
| 651 |
|
| 652 |
while (time_before_eq(jiffies, timeout)) { |
| 653 |
ret = regmap_read(tcpci->regmap, TCPC_POWER_STATUS, ®); |
| 654 |
if (ret < 0) |
| 655 |
return ret; |
| 656 |
if (!(reg & TCPC_POWER_STATUS_UNINIT)) |
| 657 |
break; |
| 658 |
usleep_range(10000, 20000); |
| 659 |
} |
| 660 |
if (time_after(jiffies, timeout)) |
| 661 |
return -ETIMEDOUT; |
| 662 |
|
| 663 |
ret = tcpci_write16(tcpci, TCPC_FAULT_STATUS, TCPC_FAULT_STATUS_ALL_REG_RST_TO_DEFAULT); |
| 664 |
if (ret < 0) |
| 665 |
return ret; |
| 666 |
|
| 667 |
/* Handle vendor init */ |
| 668 |
if (tcpci->data->init) { |
| 669 |
ret = tcpci->data->init(tcpci, tcpci->data); |
| 670 |
if (ret < 0) |
| 671 |
return ret; |
| 672 |
} |
| 673 |
|
| 674 |
/* Clear all events */ |
| 675 |
ret = tcpci_write16(tcpci, TCPC_ALERT, 0xffff); |
| 676 |
if (ret < 0) |
| 677 |
return ret; |
| 678 |
|
| 679 |
if (tcpci->controls_vbus) |
| 680 |
reg = TCPC_POWER_STATUS_VBUS_PRES; |
| 681 |
else |
| 682 |
reg = 0; |
| 683 |
ret = regmap_write(tcpci->regmap, TCPC_POWER_STATUS_MASK, reg); |
| 684 |
if (ret < 0) |
| 685 |
return ret; |
| 686 |
|
| 687 |
/* Enable Vbus detection */ |
| 688 |
ret = regmap_write(tcpci->regmap, TCPC_COMMAND, |
| 689 |
TCPC_CMD_ENABLE_VBUS_DETECT); |
| 690 |
if (ret < 0) |
| 691 |
return ret; |
| 692 |
|
| 693 |
reg = TCPC_ALERT_TX_SUCCESS | TCPC_ALERT_TX_FAILED | |
| 694 |
TCPC_ALERT_TX_DISCARDED | TCPC_ALERT_RX_STATUS | |
| 695 |
TCPC_ALERT_RX_HARD_RST | TCPC_ALERT_CC_STATUS; |
| 696 |
if (tcpci->controls_vbus) |
| 697 |
reg |= TCPC_ALERT_POWER_STATUS; |
| 698 |
/* Enable VSAFE0V status interrupt when detecting VSAFE0V is supported */ |
| 699 |
if (tcpci->data->vbus_vsafe0v) { |
| 700 |
reg |= TCPC_ALERT_EXTENDED_STATUS; |
| 701 |
ret = regmap_write(tcpci->regmap, TCPC_EXTENDED_STATUS_MASK, |
| 702 |
TCPC_EXTENDED_STATUS_VSAFE0V); |
| 703 |
if (ret < 0) |
| 704 |
return ret; |
| 705 |
} |
| 706 |
|
| 707 |
tcpci->alert_mask = reg; |
| 708 |
|
| 709 |
return 0; |
| 710 |
} |
| 711 |
|
| 712 |
irqreturn_t tcpci_irq(struct tcpci *tcpci) |
| 713 |
{ |
| 714 |
u16 status; |
| 715 |
int ret; |
| 716 |
int irq_ret; |
| 717 |
unsigned int raw; |
| 718 |
|
| 719 |
tcpci_read16(tcpci, TCPC_ALERT, &status); |
| 720 |
irq_ret = status & tcpci->alert_mask; |
| 721 |
|
| 722 |
process_status: |
| 723 |
/* |
| 724 |
* Clear alert status for everything except RX_STATUS, which shouldn't |
| 725 |
* be cleared until we have successfully retrieved message. |
| 726 |
*/ |
| 727 |
if (status & ~TCPC_ALERT_RX_STATUS) |
| 728 |
tcpci_write16(tcpci, TCPC_ALERT, |
| 729 |
status & ~TCPC_ALERT_RX_STATUS); |
| 730 |
|
| 731 |
if (status & TCPC_ALERT_CC_STATUS) |
| 732 |
tcpm_cc_change(tcpci->port); |
| 733 |
|
| 734 |
if (status & TCPC_ALERT_POWER_STATUS) { |
| 735 |
regmap_read(tcpci->regmap, TCPC_POWER_STATUS_MASK, &raw); |
| 736 |
/* |
| 737 |
* If power status mask has been reset, then the TCPC |
| 738 |
* has reset. |
| 739 |
*/ |
| 740 |
if (raw == 0xff) |
| 741 |
tcpm_tcpc_reset(tcpci->port); |
| 742 |
else |
| 743 |
tcpm_vbus_change(tcpci->port); |
| 744 |
} |
| 745 |
|
| 746 |
if (status & TCPC_ALERT_RX_STATUS) { |
| 747 |
struct pd_message msg; |
| 748 |
unsigned int cnt, payload_cnt; |
| 749 |
u16 header; |
| 750 |
|
| 751 |
regmap_read(tcpci->regmap, TCPC_RX_BYTE_CNT, &cnt); |
| 752 |
/* |
| 753 |
* 'cnt' corresponds to READABLE_BYTE_COUNT in section 4.4.14 |
| 754 |
* of the TCPCI spec [Rev 2.0 Ver 1.0 October 2017] and is |
| 755 |
* defined in table 4-36 as one greater than the number of |
| 756 |
* bytes received. And that number includes the header. So: |
| 757 |
*/ |
| 758 |
if (cnt > 3) |
| 759 |
payload_cnt = cnt - (1 + sizeof(msg.header)); |
| 760 |
else |
| 761 |
payload_cnt = 0; |
| 762 |
|
| 763 |
tcpci_read16(tcpci, TCPC_RX_HDR, &header); |
| 764 |
msg.header = cpu_to_le16(header); |
| 765 |
|
| 766 |
if (WARN_ON(payload_cnt > sizeof(msg.payload))) |
| 767 |
payload_cnt = sizeof(msg.payload); |
| 768 |
|
| 769 |
if (payload_cnt > 0) |
| 770 |
regmap_raw_read(tcpci->regmap, TCPC_RX_DATA, |
| 771 |
&msg.payload, payload_cnt); |
| 772 |
|
| 773 |
/* Read complete, clear RX status alert bit */ |
| 774 |
tcpci_write16(tcpci, TCPC_ALERT, TCPC_ALERT_RX_STATUS); |
| 775 |
|
| 776 |
tcpm_pd_receive(tcpci->port, &msg, TCPC_TX_SOP); |
| 777 |
} |
| 778 |
|
| 779 |
if (tcpci->data->vbus_vsafe0v && (status & TCPC_ALERT_EXTENDED_STATUS)) { |
| 780 |
ret = regmap_read(tcpci->regmap, TCPC_EXTENDED_STATUS, &raw); |
| 781 |
if (!ret && (raw & TCPC_EXTENDED_STATUS_VSAFE0V)) |
| 782 |
tcpm_vbus_change(tcpci->port); |
| 783 |
} |
| 784 |
|
| 785 |
if (status & TCPC_ALERT_RX_HARD_RST) |
| 786 |
tcpm_pd_hard_reset(tcpci->port); |
| 787 |
|
| 788 |
if (status & TCPC_ALERT_TX_SUCCESS) |
| 789 |
tcpm_pd_transmit_complete(tcpci->port, TCPC_TX_SUCCESS); |
| 790 |
else if (status & TCPC_ALERT_TX_DISCARDED) |
| 791 |
tcpm_pd_transmit_complete(tcpci->port, TCPC_TX_DISCARDED); |
| 792 |
else if (status & TCPC_ALERT_TX_FAILED) |
| 793 |
tcpm_pd_transmit_complete(tcpci->port, TCPC_TX_FAILED); |
| 794 |
|
| 795 |
tcpci_read16(tcpci, TCPC_ALERT, &status); |
| 796 |
|
| 797 |
if (status & tcpci->alert_mask) |
| 798 |
goto process_status; |
| 799 |
|
| 800 |
return IRQ_RETVAL(irq_ret); |
| 801 |
} |
| 802 |
EXPORT_SYMBOL_GPL(tcpci_irq); |
| 803 |
|
| 804 |
static irqreturn_t _tcpci_irq(int irq, void *dev_id) |
| 805 |
{ |
| 806 |
struct tcpci_chip *chip = dev_id; |
| 807 |
|
| 808 |
return tcpci_irq(chip->tcpci); |
| 809 |
} |
| 810 |
|
| 811 |
static const struct regmap_config tcpci_regmap_config = { |
| 812 |
.reg_bits = 8, |
| 813 |
.val_bits = 8, |
| 814 |
|
| 815 |
.max_register = 0x7F, /* 0x80 .. 0xFF are vendor defined */ |
| 816 |
}; |
| 817 |
|
| 818 |
static int tcpci_parse_config(struct tcpci *tcpci) |
| 819 |
{ |
| 820 |
tcpci->controls_vbus = true; /* XXX */ |
| 821 |
|
| 822 |
tcpci->tcpc.fwnode = device_get_named_child_node(tcpci->dev, |
| 823 |
"connector"); |
| 824 |
if (!tcpci->tcpc.fwnode) { |
| 825 |
dev_err(tcpci->dev, "Can't find connector node.\n"); |
| 826 |
return -EINVAL; |
| 827 |
} |
| 828 |
|
| 829 |
return 0; |
| 830 |
} |
| 831 |
|
| 832 |
struct tcpci *tcpci_register_port(struct device *dev, struct tcpci_data *data) |
| 833 |
{ |
| 834 |
struct tcpci *tcpci; |
| 835 |
int err; |
| 836 |
|
| 837 |
tcpci = devm_kzalloc(dev, sizeof(*tcpci), GFP_KERNEL); |
| 838 |
if (!tcpci) |
| 839 |
return ERR_PTR(-ENOMEM); |
| 840 |
|
| 841 |
tcpci->dev = dev; |
| 842 |
tcpci->data = data; |
| 843 |
tcpci->regmap = data->regmap; |
| 844 |
|
| 845 |
tcpci->tcpc.init = tcpci_init; |
| 846 |
tcpci->tcpc.get_vbus = tcpci_get_vbus; |
| 847 |
tcpci->tcpc.set_vbus = tcpci_set_vbus; |
| 848 |
tcpci->tcpc.set_cc = tcpci_set_cc; |
| 849 |
tcpci->tcpc.apply_rc = tcpci_apply_rc; |
| 850 |
tcpci->tcpc.get_cc = tcpci_get_cc; |
| 851 |
tcpci->tcpc.set_polarity = tcpci_set_polarity; |
| 852 |
tcpci->tcpc.set_vconn = tcpci_set_vconn; |
| 853 |
tcpci->tcpc.start_toggling = tcpci_start_toggling; |
| 854 |
|
| 855 |
tcpci->tcpc.set_pd_rx = tcpci_set_pd_rx; |
| 856 |
tcpci->tcpc.set_roles = tcpci_set_roles; |
| 857 |
tcpci->tcpc.pd_transmit = tcpci_pd_transmit; |
| 858 |
tcpci->tcpc.set_bist_data = tcpci_set_bist_data; |
| 859 |
tcpci->tcpc.enable_frs = tcpci_enable_frs; |
| 860 |
tcpci->tcpc.frs_sourcing_vbus = tcpci_frs_sourcing_vbus; |
| 861 |
tcpci->tcpc.set_partner_usb_comm_capable = tcpci_set_partner_usb_comm_capable; |
| 862 |
tcpci->tcpc.cable_comm_capable = tcpci_cable_comm_capable; |
| 863 |
tcpci->tcpc.attempt_vconn_swap_discovery = tcpci_attempt_vconn_swap_discovery; |
| 864 |
|
| 865 |
if (tcpci->data->check_contaminant) |
| 866 |
tcpci->tcpc.check_contaminant = tcpci_check_contaminant; |
| 867 |
|
| 868 |
if (tcpci->data->auto_discharge_disconnect) { |
| 869 |
tcpci->tcpc.enable_auto_vbus_discharge = tcpci_enable_auto_vbus_discharge; |
| 870 |
tcpci->tcpc.set_auto_vbus_discharge_threshold = |
| 871 |
tcpci_set_auto_vbus_discharge_threshold; |
| 872 |
regmap_update_bits(tcpci->regmap, TCPC_POWER_CTRL, TCPC_POWER_CTRL_BLEED_DISCHARGE, |
| 873 |
TCPC_POWER_CTRL_BLEED_DISCHARGE); |
| 874 |
} |
| 875 |
|
| 876 |
if (tcpci->data->vbus_vsafe0v) |
| 877 |
tcpci->tcpc.is_vbus_vsafe0v = tcpci_is_vbus_vsafe0v; |
| 878 |
|
| 879 |
if (tcpci->data->set_orientation) |
| 880 |
tcpci->tcpc.set_orientation = tcpci_set_orientation; |
| 881 |
|
| 882 |
err = tcpci_parse_config(tcpci); |
| 883 |
if (err < 0) |
| 884 |
return ERR_PTR(err); |
| 885 |
|
| 886 |
tcpci->port = tcpm_register_port(tcpci->dev, &tcpci->tcpc); |
| 887 |
if (IS_ERR(tcpci->port)) { |
| 888 |
fwnode_handle_put(tcpci->tcpc.fwnode); |
| 889 |
return ERR_CAST(tcpci->port); |
| 890 |
} |
| 891 |
|
| 892 |
return tcpci; |
| 893 |
} |
| 894 |
EXPORT_SYMBOL_GPL(tcpci_register_port); |
| 895 |
|
| 896 |
void tcpci_unregister_port(struct tcpci *tcpci) |
| 897 |
{ |
| 898 |
tcpm_unregister_port(tcpci->port); |
| 899 |
fwnode_handle_put(tcpci->tcpc.fwnode); |
| 900 |
} |
| 901 |
EXPORT_SYMBOL_GPL(tcpci_unregister_port); |
| 902 |
|
| 903 |
static int tcpci_probe(struct i2c_client *client) |
| 904 |
{ |
| 905 |
struct tcpci_chip *chip; |
| 906 |
int err; |
| 907 |
u16 val = 0; |
| 908 |
|
| 909 |
err = devm_regulator_get_enable_optional(&client->dev, "vdd"); |
| 910 |
if (err && err != -ENODEV) |
| 911 |
return dev_err_probe(&client->dev, err, "Failed to get regulator\n"); |
| 912 |
|
| 913 |
chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL); |
| 914 |
if (!chip) |
| 915 |
return -ENOMEM; |
| 916 |
|
| 917 |
chip->data.regmap = devm_regmap_init_i2c(client, &tcpci_regmap_config); |
| 918 |
if (IS_ERR(chip->data.regmap)) |
| 919 |
return PTR_ERR(chip->data.regmap); |
| 920 |
|
| 921 |
i2c_set_clientdata(client, chip); |
| 922 |
|
| 923 |
/* Disable chip interrupts before requesting irq */ |
| 924 |
err = regmap_raw_write(chip->data.regmap, TCPC_ALERT_MASK, &val, |
| 925 |
sizeof(u16)); |
| 926 |
if (err < 0) |
| 927 |
return err; |
| 928 |
|
| 929 |
err = tcpci_check_std_output_cap(chip->data.regmap, |
| 930 |
TCPC_STD_OUTPUT_CAP_ORIENTATION); |
| 931 |
if (err < 0) |
| 932 |
return err; |
| 933 |
|
| 934 |
chip->data.set_orientation = err; |
| 935 |
|
| 936 |
chip->tcpci = tcpci_register_port(&client->dev, &chip->data); |
| 937 |
if (IS_ERR(chip->tcpci)) |
| 938 |
return PTR_ERR(chip->tcpci); |
| 939 |
|
| 940 |
err = devm_request_threaded_irq(&client->dev, client->irq, NULL, |
| 941 |
_tcpci_irq, |
| 942 |
IRQF_SHARED | IRQF_ONESHOT, |
| 943 |
dev_name(&client->dev), chip); |
| 944 |
if (err < 0) |
| 945 |
goto unregister_port; |
| 946 |
|
| 947 |
/* Enable chip interrupts at last */ |
| 948 |
err = tcpci_write16(chip->tcpci, TCPC_ALERT_MASK, chip->tcpci->alert_mask); |
| 949 |
if (err < 0) |
| 950 |
goto unregister_port; |
| 951 |
|
| 952 |
device_set_wakeup_capable(chip->tcpci->dev, true); |
| 953 |
|
| 954 |
return 0; |
| 955 |
|
| 956 |
unregister_port: |
| 957 |
tcpci_unregister_port(chip->tcpci); |
| 958 |
return err; |
| 959 |
} |
| 960 |
|
| 961 |
static void tcpci_remove(struct i2c_client *client) |
| 962 |
{ |
| 963 |
struct tcpci_chip *chip = i2c_get_clientdata(client); |
| 964 |
int err; |
| 965 |
|
| 966 |
/* Disable chip interrupts before unregistering port */ |
| 967 |
err = tcpci_write16(chip->tcpci, TCPC_ALERT_MASK, 0); |
| 968 |
if (err < 0) |
| 969 |
dev_warn(&client->dev, "Failed to disable irqs (%pe)\n", ERR_PTR(err)); |
| 970 |
|
| 971 |
tcpci_unregister_port(chip->tcpci); |
| 972 |
} |
| 973 |
|
| 974 |
static int tcpci_suspend(struct device *dev) |
| 975 |
{ |
| 976 |
struct i2c_client *i2c = to_i2c_client(dev); |
| 977 |
struct tcpci_chip *chip = i2c_get_clientdata(i2c); |
| 978 |
int ret; |
| 979 |
|
| 980 |
if (device_may_wakeup(dev)) |
| 981 |
ret = enable_irq_wake(i2c->irq); |
| 982 |
else |
| 983 |
ret = tcpci_write16(chip->tcpci, TCPC_ALERT_MASK, 0); |
| 984 |
|
| 985 |
return ret; |
| 986 |
} |
| 987 |
|
| 988 |
static int tcpci_resume(struct device *dev) |
| 989 |
{ |
| 990 |
struct i2c_client *i2c = to_i2c_client(dev); |
| 991 |
struct tcpci_chip *chip = i2c_get_clientdata(i2c); |
| 992 |
int ret; |
| 993 |
|
| 994 |
if (device_may_wakeup(dev)) |
| 995 |
ret = disable_irq_wake(i2c->irq); |
| 996 |
else |
| 997 |
ret = tcpci_write16(chip->tcpci, TCPC_ALERT_MASK, chip->tcpci->alert_mask); |
| 998 |
|
| 999 |
return ret; |
| 1000 |
} |
| 1001 |
|
| 1002 |
DEFINE_SIMPLE_DEV_PM_OPS(tcpci_pm_ops, tcpci_suspend, tcpci_resume); |
| 1003 |
|
| 1004 |
static const struct i2c_device_id tcpci_id[] = { |
| 1005 |
{ "tcpci" }, |
| 1006 |
{ } |
| 1007 |
}; |
| 1008 |
MODULE_DEVICE_TABLE(i2c, tcpci_id); |
| 1009 |
|
| 1010 |
#ifdef CONFIG_OF |
| 1011 |
static const struct of_device_id tcpci_of_match[] = { |
| 1012 |
{ .compatible = "nxp,ptn5110", }, |
| 1013 |
{ .compatible = "tcpci", }, |
| 1014 |
{}, |
| 1015 |
}; |
| 1016 |
MODULE_DEVICE_TABLE(of, tcpci_of_match); |
| 1017 |
#endif |
| 1018 |
|
| 1019 |
static struct i2c_driver tcpci_i2c_driver = { |
| 1020 |
.driver = { |
| 1021 |
.name = "tcpci", |
| 1022 |
.pm = pm_sleep_ptr(&tcpci_pm_ops), |
| 1023 |
.of_match_table = of_match_ptr(tcpci_of_match), |
| 1024 |
}, |
| 1025 |
.probe = tcpci_probe, |
| 1026 |
.remove = tcpci_remove, |
| 1027 |
.id_table = tcpci_id, |
| 1028 |
}; |
| 1029 |
module_i2c_driver(tcpci_i2c_driver); |
| 1030 |
|
| 1031 |
MODULE_DESCRIPTION("USB Type-C Port Controller Interface driver"); |
| 1032 |
MODULE_LICENSE("GPL"); |