1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Driver for TI TPS6598x USB Power Delivery controller family 4 * 5 * Copyright (C) 2017, Intel Corporation 6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com> 7 */ 8 9 #include <linux/i2c.h> 10 #include <linux/acpi.h> 11 #include <linux/module.h> 12 #include <linux/of.h> 13 #include <linux/power_supply.h> 14 #include <linux/regmap.h> 15 #include <linux/interrupt.h> 16 #include <linux/usb/typec.h> 17 #include <linux/usb/typec_altmode.h> 18 #include <linux/usb/role.h> 19 20 #include "tps6598x.h" 21 #include "trace.h" 22 23 /* Register offsets */ 24 #define TPS_REG_VID 0x00 25 #define TPS_REG_MODE 0x03 26 #define TPS_REG_CMD1 0x08 27 #define TPS_REG_DATA1 0x09 28 #define TPS_REG_INT_EVENT1 0x14 29 #define TPS_REG_INT_EVENT2 0x15 30 #define TPS_REG_INT_MASK1 0x16 31 #define TPS_REG_INT_MASK2 0x17 32 #define TPS_REG_INT_CLEAR1 0x18 33 #define TPS_REG_INT_CLEAR2 0x19 34 #define TPS_REG_SYSTEM_POWER_STATE 0x20 35 #define TPS_REG_STATUS 0x1a 36 #define TPS_REG_SYSTEM_CONF 0x28 37 #define TPS_REG_CTRL_CONF 0x29 38 #define TPS_REG_POWER_STATUS 0x3f 39 #define TPS_REG_RX_IDENTITY_SOP 0x48 40 #define TPS_REG_DATA_STATUS 0x5f 41 42 /* TPS_REG_SYSTEM_CONF bits */ 43 #define TPS_SYSCONF_PORTINFO(c) ((c) & 7) 44 45 enum { 46 TPS_PORTINFO_SINK, 47 TPS_PORTINFO_SINK_ACCESSORY, 48 TPS_PORTINFO_DRP_UFP, 49 TPS_PORTINFO_DRP_UFP_DRD, 50 TPS_PORTINFO_DRP_DFP, 51 TPS_PORTINFO_DRP_DFP_DRD, 52 TPS_PORTINFO_SOURCE, 53 }; 54 55 /* TPS_REG_RX_IDENTITY_SOP */ 56 struct tps6598x_rx_identity_reg { 57 u8 status; 58 struct usb_pd_identity identity; 59 } __packed; 60 61 /* Standard Task return codes */ 62 #define TPS_TASK_TIMEOUT 1 63 #define TPS_TASK_REJECTED 3 64 65 enum { 66 TPS_MODE_APP, 67 TPS_MODE_BOOT, 68 TPS_MODE_BIST, 69 TPS_MODE_DISC, 70 }; 71 72 static const char *const modes[] = { 73 [TPS_MODE_APP] = "APP ", 74 [TPS_MODE_BOOT] = "BOOT", 75 [TPS_MODE_BIST] = "BIST", 76 [TPS_MODE_DISC] = "DISC", 77 }; 78 79 /* Unrecognized commands will be replaced with "!CMD" */ 80 #define INVALID_CMD(_cmd_) (_cmd_ == 0x444d4321) 81 82 struct tps6598x { 83 struct device *dev; 84 struct regmap *regmap; 85 struct mutex lock; /* device lock */ 86 u8 i2c_protocol:1; 87 88 struct typec_port *port; 89 struct typec_partner *partner; 90 struct usb_pd_identity partner_identity; 91 struct usb_role_switch *role_sw; 92 struct typec_capability typec_cap; 93 94 struct power_supply *psy; 95 struct power_supply_desc psy_desc; 96 enum power_supply_usb_type usb_type; 97 98 int wakeup; 99 u16 pwr_status; 100 }; 101 102 static enum power_supply_property tps6598x_psy_props[] = { 103 POWER_SUPPLY_PROP_USB_TYPE, 104 POWER_SUPPLY_PROP_ONLINE, 105 }; 106 107 static enum power_supply_usb_type tps6598x_psy_usb_types[] = { 108 POWER_SUPPLY_USB_TYPE_C, 109 POWER_SUPPLY_USB_TYPE_PD, 110 }; 111 112 static const char *tps6598x_psy_name_prefix = "tps6598x-source-psy-"; 113 114 /* 115 * Max data bytes for Data1, Data2, and other registers. See ch 1.3.2: 116 * https://www.ti.com/lit/ug/slvuan1a/slvuan1a.pdf 117 */ 118 #define TPS_MAX_LEN 64 119 120 static int 121 tps6598x_block_read(struct tps6598x *tps, u8 reg, void *val, size_t len) 122 { 123 u8 data[TPS_MAX_LEN + 1]; 124 int ret; 125 126 if (len + 1 > sizeof(data)) 127 return -EINVAL; 128 129 if (!tps->i2c_protocol) 130 return regmap_raw_read(tps->regmap, reg, val, len); 131 132 ret = regmap_raw_read(tps->regmap, reg, data, len + 1); 133 if (ret) 134 return ret; 135 136 if (data[0] < len) 137 return -EIO; 138 139 memcpy(val, &data[1], len); 140 return 0; 141 } 142 143 static int tps6598x_block_write(struct tps6598x *tps, u8 reg, 144 const void *val, size_t len) 145 { 146 u8 data[TPS_MAX_LEN + 1]; 147 148 if (len + 1 > sizeof(data)) 149 return -EINVAL; 150 151 if (!tps->i2c_protocol) 152 return regmap_raw_write(tps->regmap, reg, val, len); 153 154 data[0] = len; 155 memcpy(&data[1], val, len); 156 157 return regmap_raw_write(tps->regmap, reg, data, len + 1); 158 } 159 160 static inline int tps6598x_read8(struct tps6598x *tps, u8 reg, u8 *val) 161 { 162 return tps6598x_block_read(tps, reg, val, sizeof(u8)); 163 } 164 165 static inline int tps6598x_read16(struct tps6598x *tps, u8 reg, u16 *val) 166 { 167 return tps6598x_block_read(tps, reg, val, sizeof(u16)); 168 } 169 170 static inline int tps6598x_read32(struct tps6598x *tps, u8 reg, u32 *val) 171 { 172 return tps6598x_block_read(tps, reg, val, sizeof(u32)); 173 } 174 175 static inline int tps6598x_read64(struct tps6598x *tps, u8 reg, u64 *val) 176 { 177 return tps6598x_block_read(tps, reg, val, sizeof(u64)); 178 } 179 180 static inline int tps6598x_write16(struct tps6598x *tps, u8 reg, u16 val) 181 { 182 return tps6598x_block_write(tps, reg, &val, sizeof(u16)); 183 } 184 185 static inline int tps6598x_write32(struct tps6598x *tps, u8 reg, u32 val) 186 { 187 return tps6598x_block_write(tps, reg, &val, sizeof(u32)); 188 } 189 190 static inline int tps6598x_write64(struct tps6598x *tps, u8 reg, u64 val) 191 { 192 return tps6598x_block_write(tps, reg, &val, sizeof(u64)); 193 } 194 195 static inline int 196 tps6598x_write_4cc(struct tps6598x *tps, u8 reg, const char *val) 197 { 198 return tps6598x_block_write(tps, reg, val, 4); 199 } 200 201 static int tps6598x_read_partner_identity(struct tps6598x *tps) 202 { 203 struct tps6598x_rx_identity_reg id; 204 int ret; 205 206 ret = tps6598x_block_read(tps, TPS_REG_RX_IDENTITY_SOP, 207 &id, sizeof(id)); 208 if (ret) 209 return ret; 210 211 tps->partner_identity = id.identity; 212 213 return 0; 214 } 215 216 static void tps6598x_set_data_role(struct tps6598x *tps, 217 enum typec_data_role role, bool connected) 218 { 219 enum usb_role role_val; 220 221 if (role == TYPEC_HOST) 222 role_val = USB_ROLE_HOST; 223 else 224 role_val = USB_ROLE_DEVICE; 225 226 if (!connected) 227 role_val = USB_ROLE_NONE; 228 229 usb_role_switch_set_role(tps->role_sw, role_val); 230 typec_set_data_role(tps->port, role); 231 } 232 233 static int tps6598x_connect(struct tps6598x *tps, u32 status) 234 { 235 struct typec_partner_desc desc; 236 enum typec_pwr_opmode mode; 237 int ret; 238 239 if (tps->partner) 240 return 0; 241 242 mode = TPS_POWER_STATUS_PWROPMODE(tps->pwr_status); 243 244 desc.usb_pd = mode == TYPEC_PWR_MODE_PD; 245 desc.accessory = TYPEC_ACCESSORY_NONE; /* XXX: handle accessories */ 246 desc.identity = NULL; 247 248 if (desc.usb_pd) { 249 ret = tps6598x_read_partner_identity(tps); 250 if (ret) 251 return ret; 252 desc.identity = &tps->partner_identity; 253 } 254 255 typec_set_pwr_opmode(tps->port, mode); 256 typec_set_pwr_role(tps->port, TPS_STATUS_TO_TYPEC_PORTROLE(status)); 257 typec_set_vconn_role(tps->port, TPS_STATUS_TO_TYPEC_VCONN(status)); 258 if (TPS_STATUS_TO_UPSIDE_DOWN(status)) 259 typec_set_orientation(tps->port, TYPEC_ORIENTATION_REVERSE); 260 else 261 typec_set_orientation(tps->port, TYPEC_ORIENTATION_NORMAL); 262 typec_set_mode(tps->port, TYPEC_STATE_USB); 263 tps6598x_set_data_role(tps, TPS_STATUS_TO_TYPEC_DATAROLE(status), true); 264 265 tps->partner = typec_register_partner(tps->port, &desc); 266 if (IS_ERR(tps->partner)) 267 return PTR_ERR(tps->partner); 268 269 if (desc.identity) 270 typec_partner_set_identity(tps->partner); 271 272 power_supply_changed(tps->psy); 273 274 return 0; 275 } 276 277 static void tps6598x_disconnect(struct tps6598x *tps, u32 status) 278 { 279 if (!IS_ERR(tps->partner)) 280 typec_unregister_partner(tps->partner); 281 tps->partner = NULL; 282 typec_set_pwr_opmode(tps->port, TYPEC_PWR_MODE_USB); 283 typec_set_pwr_role(tps->port, TPS_STATUS_TO_TYPEC_PORTROLE(status)); 284 typec_set_vconn_role(tps->port, TPS_STATUS_TO_TYPEC_VCONN(status)); 285 typec_set_orientation(tps->port, TYPEC_ORIENTATION_NONE); 286 typec_set_mode(tps->port, TYPEC_STATE_SAFE); 287 tps6598x_set_data_role(tps, TPS_STATUS_TO_TYPEC_DATAROLE(status), false); 288 289 power_supply_changed(tps->psy); 290 } 291 292 static int tps6598x_exec_cmd(struct tps6598x *tps, const char *cmd, 293 size_t in_len, u8 *in_data, 294 size_t out_len, u8 *out_data) 295 { 296 unsigned long timeout; 297 u32 val; 298 int ret; 299 300 ret = tps6598x_read32(tps, TPS_REG_CMD1, &val); 301 if (ret) 302 return ret; 303 if (val && !INVALID_CMD(val)) 304 return -EBUSY; 305 306 if (in_len) { 307 ret = tps6598x_block_write(tps, TPS_REG_DATA1, 308 in_data, in_len); 309 if (ret) 310 return ret; 311 } 312 313 ret = tps6598x_write_4cc(tps, TPS_REG_CMD1, cmd); 314 if (ret < 0) 315 return ret; 316 317 /* XXX: Using 1s for now, but it may not be enough for every command. */ 318 timeout = jiffies + msecs_to_jiffies(1000); 319 320 do { 321 ret = tps6598x_read32(tps, TPS_REG_CMD1, &val); 322 if (ret) 323 return ret; 324 if (INVALID_CMD(val)) 325 return -EINVAL; 326 327 if (time_is_before_jiffies(timeout)) 328 return -ETIMEDOUT; 329 } while (val); 330 331 if (out_len) { 332 ret = tps6598x_block_read(tps, TPS_REG_DATA1, 333 out_data, out_len); 334 if (ret) 335 return ret; 336 val = out_data[0]; 337 } else { 338 ret = tps6598x_block_read(tps, TPS_REG_DATA1, &val, sizeof(u8)); 339 if (ret) 340 return ret; 341 } 342 343 switch (val) { 344 case TPS_TASK_TIMEOUT: 345 return -ETIMEDOUT; 346 case TPS_TASK_REJECTED: 347 return -EPERM; 348 default: 349 break; 350 } 351 352 return 0; 353 } 354 355 static int tps6598x_dr_set(struct typec_port *port, enum typec_data_role role) 356 { 357 const char *cmd = (role == TYPEC_DEVICE) ? "SWUF" : "SWDF"; 358 struct tps6598x *tps = typec_get_drvdata(port); 359 u32 status; 360 int ret; 361 362 mutex_lock(&tps->lock); 363 364 ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL); 365 if (ret) 366 goto out_unlock; 367 368 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status); 369 if (ret) 370 goto out_unlock; 371 372 if (role != TPS_STATUS_TO_TYPEC_DATAROLE(status)) { 373 ret = -EPROTO; 374 goto out_unlock; 375 } 376 377 tps6598x_set_data_role(tps, role, true); 378 379 out_unlock: 380 mutex_unlock(&tps->lock); 381 382 return ret; 383 } 384 385 static int tps6598x_pr_set(struct typec_port *port, enum typec_role role) 386 { 387 const char *cmd = (role == TYPEC_SINK) ? "SWSk" : "SWSr"; 388 struct tps6598x *tps = typec_get_drvdata(port); 389 u32 status; 390 int ret; 391 392 mutex_lock(&tps->lock); 393 394 ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL); 395 if (ret) 396 goto out_unlock; 397 398 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status); 399 if (ret) 400 goto out_unlock; 401 402 if (role != TPS_STATUS_TO_TYPEC_PORTROLE(status)) { 403 ret = -EPROTO; 404 goto out_unlock; 405 } 406 407 typec_set_pwr_role(tps->port, role); 408 409 out_unlock: 410 mutex_unlock(&tps->lock); 411 412 return ret; 413 } 414 415 static const struct typec_operations tps6598x_ops = { 416 .dr_set = tps6598x_dr_set, 417 .pr_set = tps6598x_pr_set, 418 }; 419 420 static bool tps6598x_read_status(struct tps6598x *tps, u32 *status) 421 { 422 int ret; 423 424 ret = tps6598x_read32(tps, TPS_REG_STATUS, status); 425 if (ret) { 426 dev_err(tps->dev, "%s: failed to read status\n", __func__); 427 return false; 428 } 429 trace_tps6598x_status(*status); 430 431 return true; 432 } 433 434 static bool tps6598x_read_data_status(struct tps6598x *tps) 435 { 436 u32 data_status; 437 int ret; 438 439 ret = tps6598x_read32(tps, TPS_REG_DATA_STATUS, &data_status); 440 if (ret < 0) { 441 dev_err(tps->dev, "failed to read data status: %d\n", ret); 442 return false; 443 } 444 trace_tps6598x_data_status(data_status); 445 446 return true; 447 } 448 449 static bool tps6598x_read_power_status(struct tps6598x *tps) 450 { 451 u16 pwr_status; 452 int ret; 453 454 ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status); 455 if (ret < 0) { 456 dev_err(tps->dev, "failed to read power status: %d\n", ret); 457 return false; 458 } 459 tps->pwr_status = pwr_status; 460 trace_tps6598x_power_status(pwr_status); 461 462 return true; 463 } 464 465 static void tps6598x_handle_plug_event(struct tps6598x *tps, u32 status) 466 { 467 int ret; 468 469 if (status & TPS_STATUS_PLUG_PRESENT) { 470 ret = tps6598x_connect(tps, status); 471 if (ret) 472 dev_err(tps->dev, "failed to register partner\n"); 473 } else { 474 tps6598x_disconnect(tps, status); 475 } 476 } 477 478 static irqreturn_t cd321x_interrupt(int irq, void *data) 479 { 480 struct tps6598x *tps = data; 481 u64 event = 0; 482 u32 status; 483 int ret; 484 485 mutex_lock(&tps->lock); 486 487 ret = tps6598x_read64(tps, TPS_REG_INT_EVENT1, &event); 488 if (ret) { 489 dev_err(tps->dev, "%s: failed to read events\n", __func__); 490 goto err_unlock; 491 } 492 trace_cd321x_irq(event); 493 494 if (!event) 495 goto err_unlock; 496 497 if (!tps6598x_read_status(tps, &status)) 498 goto err_clear_ints; 499 500 if (event & APPLE_CD_REG_INT_POWER_STATUS_UPDATE) 501 if (!tps6598x_read_power_status(tps)) 502 goto err_clear_ints; 503 504 if (event & APPLE_CD_REG_INT_DATA_STATUS_UPDATE) 505 if (!tps6598x_read_data_status(tps)) 506 goto err_clear_ints; 507 508 /* Handle plug insert or removal */ 509 if (event & APPLE_CD_REG_INT_PLUG_EVENT) 510 tps6598x_handle_plug_event(tps, status); 511 512 err_clear_ints: 513 tps6598x_write64(tps, TPS_REG_INT_CLEAR1, event); 514 515 err_unlock: 516 mutex_unlock(&tps->lock); 517 518 if (event) 519 return IRQ_HANDLED; 520 return IRQ_NONE; 521 } 522 523 static irqreturn_t tps6598x_interrupt(int irq, void *data) 524 { 525 struct tps6598x *tps = data; 526 u64 event1 = 0; 527 u64 event2 = 0; 528 u32 status; 529 int ret; 530 531 mutex_lock(&tps->lock); 532 533 ret = tps6598x_read64(tps, TPS_REG_INT_EVENT1, &event1); 534 ret |= tps6598x_read64(tps, TPS_REG_INT_EVENT2, &event2); 535 if (ret) { 536 dev_err(tps->dev, "%s: failed to read events\n", __func__); 537 goto err_unlock; 538 } 539 trace_tps6598x_irq(event1, event2); 540 541 if (!(event1 | event2)) 542 goto err_unlock; 543 544 if (!tps6598x_read_status(tps, &status)) 545 goto err_clear_ints; 546 547 if ((event1 | event2) & TPS_REG_INT_POWER_STATUS_UPDATE) 548 if (!tps6598x_read_power_status(tps)) 549 goto err_clear_ints; 550 551 if ((event1 | event2) & TPS_REG_INT_DATA_STATUS_UPDATE) 552 if (!tps6598x_read_data_status(tps)) 553 goto err_clear_ints; 554 555 /* Handle plug insert or removal */ 556 if ((event1 | event2) & TPS_REG_INT_PLUG_EVENT) 557 tps6598x_handle_plug_event(tps, status); 558 559 err_clear_ints: 560 tps6598x_write64(tps, TPS_REG_INT_CLEAR1, event1); 561 tps6598x_write64(tps, TPS_REG_INT_CLEAR2, event2); 562 563 err_unlock: 564 mutex_unlock(&tps->lock); 565 566 if (event1 | event2) 567 return IRQ_HANDLED; 568 return IRQ_NONE; 569 } 570 571 static int tps6598x_check_mode(struct tps6598x *tps) 572 { 573 char mode[5] = { }; 574 int ret; 575 576 ret = tps6598x_read32(tps, TPS_REG_MODE, (void *)mode); 577 if (ret) 578 return ret; 579 580 switch (match_string(modes, ARRAY_SIZE(modes), mode)) { 581 case TPS_MODE_APP: 582 return 0; 583 case TPS_MODE_BOOT: 584 dev_warn(tps->dev, "dead-battery condition\n"); 585 return 0; 586 case TPS_MODE_BIST: 587 case TPS_MODE_DISC: 588 default: 589 dev_err(tps->dev, "controller in unsupported mode \"%s\"\n", 590 mode); 591 break; 592 } 593 594 return -ENODEV; 595 } 596 597 static const struct regmap_config tps6598x_regmap_config = { 598 .reg_bits = 8, 599 .val_bits = 8, 600 .max_register = 0x7F, 601 }; 602 603 static int tps6598x_psy_get_online(struct tps6598x *tps, 604 union power_supply_propval *val) 605 { 606 if (TPS_POWER_STATUS_CONNECTION(tps->pwr_status) && 607 TPS_POWER_STATUS_SOURCESINK(tps->pwr_status)) { 608 val->intval = 1; 609 } else { 610 val->intval = 0; 611 } 612 return 0; 613 } 614 615 static int tps6598x_psy_get_prop(struct power_supply *psy, 616 enum power_supply_property psp, 617 union power_supply_propval *val) 618 { 619 struct tps6598x *tps = power_supply_get_drvdata(psy); 620 int ret = 0; 621 622 switch (psp) { 623 case POWER_SUPPLY_PROP_USB_TYPE: 624 if (TPS_POWER_STATUS_PWROPMODE(tps->pwr_status) == TYPEC_PWR_MODE_PD) 625 val->intval = POWER_SUPPLY_USB_TYPE_PD; 626 else 627 val->intval = POWER_SUPPLY_USB_TYPE_C; 628 break; 629 case POWER_SUPPLY_PROP_ONLINE: 630 ret = tps6598x_psy_get_online(tps, val); 631 break; 632 default: 633 ret = -EINVAL; 634 break; 635 } 636 637 return ret; 638 } 639 640 static int cd321x_switch_power_state(struct tps6598x *tps, u8 target_state) 641 { 642 u8 state; 643 int ret; 644 645 ret = tps6598x_read8(tps, TPS_REG_SYSTEM_POWER_STATE, &state); 646 if (ret) 647 return ret; 648 649 if (state == target_state) 650 return 0; 651 652 ret = tps6598x_exec_cmd(tps, "SSPS", sizeof(u8), &target_state, 0, NULL); 653 if (ret) 654 return ret; 655 656 ret = tps6598x_read8(tps, TPS_REG_SYSTEM_POWER_STATE, &state); 657 if (ret) 658 return ret; 659 660 if (state != target_state) 661 return -EINVAL; 662 663 return 0; 664 } 665 666 static int devm_tps6598_psy_register(struct tps6598x *tps) 667 { 668 struct power_supply_config psy_cfg = {}; 669 const char *port_dev_name = dev_name(tps->dev); 670 char *psy_name; 671 672 psy_cfg.drv_data = tps; 673 psy_cfg.fwnode = dev_fwnode(tps->dev); 674 675 psy_name = devm_kasprintf(tps->dev, GFP_KERNEL, "%s%s", tps6598x_psy_name_prefix, 676 port_dev_name); 677 if (!psy_name) 678 return -ENOMEM; 679 680 tps->psy_desc.name = psy_name; 681 tps->psy_desc.type = POWER_SUPPLY_TYPE_USB; 682 tps->psy_desc.usb_types = tps6598x_psy_usb_types; 683 tps->psy_desc.num_usb_types = ARRAY_SIZE(tps6598x_psy_usb_types); 684 tps->psy_desc.properties = tps6598x_psy_props; 685 tps->psy_desc.num_properties = ARRAY_SIZE(tps6598x_psy_props); 686 tps->psy_desc.get_property = tps6598x_psy_get_prop; 687 688 tps->usb_type = POWER_SUPPLY_USB_TYPE_C; 689 690 tps->psy = devm_power_supply_register(tps->dev, &tps->psy_desc, 691 &psy_cfg); 692 return PTR_ERR_OR_ZERO(tps->psy); 693 } 694 695 static int tps6598x_probe(struct i2c_client *client) 696 { 697 irq_handler_t irq_handler = tps6598x_interrupt; 698 struct device_node *np = client->dev.of_node; 699 struct typec_capability typec_cap = { }; 700 struct tps6598x *tps; 701 struct fwnode_handle *fwnode; 702 u32 status; 703 u32 conf; 704 u32 vid; 705 int ret; 706 u64 mask1; 707 708 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL); 709 if (!tps) 710 return -ENOMEM; 711 712 mutex_init(&tps->lock); 713 tps->dev = &client->dev; 714 715 tps->regmap = devm_regmap_init_i2c(client, &tps6598x_regmap_config); 716 if (IS_ERR(tps->regmap)) 717 return PTR_ERR(tps->regmap); 718 719 ret = tps6598x_read32(tps, TPS_REG_VID, &vid); 720 if (ret < 0 || !vid) 721 return -ENODEV; 722 723 /* 724 * Checking can the adapter handle SMBus protocol. If it can not, the 725 * driver needs to take care of block reads separately. 726 */ 727 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) 728 tps->i2c_protocol = true; 729 730 if (np && of_device_is_compatible(np, "apple,cd321x")) { 731 /* Switch CD321X chips to the correct system power state */ 732 ret = cd321x_switch_power_state(tps, TPS_SYSTEM_POWER_STATE_S0); 733 if (ret) 734 return ret; 735 736 /* CD321X chips have all interrupts masked initially */ 737 mask1 = APPLE_CD_REG_INT_POWER_STATUS_UPDATE | 738 APPLE_CD_REG_INT_DATA_STATUS_UPDATE | 739 APPLE_CD_REG_INT_PLUG_EVENT; 740 741 irq_handler = cd321x_interrupt; 742 } else { 743 /* Enable power status, data status and plug event interrupts */ 744 mask1 = TPS_REG_INT_POWER_STATUS_UPDATE | 745 TPS_REG_INT_DATA_STATUS_UPDATE | 746 TPS_REG_INT_PLUG_EVENT; 747 } 748 749 /* Make sure the controller has application firmware running */ 750 ret = tps6598x_check_mode(tps); 751 if (ret) 752 return ret; 753 754 ret = tps6598x_write64(tps, TPS_REG_INT_MASK1, mask1); 755 if (ret) 756 return ret; 757 758 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status); 759 if (ret < 0) 760 goto err_clear_mask; 761 trace_tps6598x_status(status); 762 763 ret = tps6598x_read32(tps, TPS_REG_SYSTEM_CONF, &conf); 764 if (ret < 0) 765 goto err_clear_mask; 766 767 /* 768 * This fwnode has a "compatible" property, but is never populated as a 769 * struct device. Instead we simply parse it to read the properties. 770 * This breaks fw_devlink=on. To maintain backward compatibility 771 * with existing DT files, we work around this by deleting any 772 * fwnode_links to/from this fwnode. 773 */ 774 fwnode = device_get_named_child_node(&client->dev, "connector"); 775 if (fwnode) 776 fw_devlink_purge_absent_suppliers(fwnode); 777 778 tps->role_sw = fwnode_usb_role_switch_get(fwnode); 779 if (IS_ERR(tps->role_sw)) { 780 ret = PTR_ERR(tps->role_sw); 781 goto err_fwnode_put; 782 } 783 784 typec_cap.revision = USB_TYPEC_REV_1_2; 785 typec_cap.pd_revision = 0x200; 786 typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE; 787 typec_cap.driver_data = tps; 788 typec_cap.ops = &tps6598x_ops; 789 typec_cap.fwnode = fwnode; 790 791 switch (TPS_SYSCONF_PORTINFO(conf)) { 792 case TPS_PORTINFO_SINK_ACCESSORY: 793 case TPS_PORTINFO_SINK: 794 typec_cap.type = TYPEC_PORT_SNK; 795 typec_cap.data = TYPEC_PORT_UFP; 796 break; 797 case TPS_PORTINFO_DRP_UFP_DRD: 798 case TPS_PORTINFO_DRP_DFP_DRD: 799 typec_cap.type = TYPEC_PORT_DRP; 800 typec_cap.data = TYPEC_PORT_DRD; 801 break; 802 case TPS_PORTINFO_DRP_UFP: 803 typec_cap.type = TYPEC_PORT_DRP; 804 typec_cap.data = TYPEC_PORT_UFP; 805 break; 806 case TPS_PORTINFO_DRP_DFP: 807 typec_cap.type = TYPEC_PORT_DRP; 808 typec_cap.data = TYPEC_PORT_DFP; 809 break; 810 case TPS_PORTINFO_SOURCE: 811 typec_cap.type = TYPEC_PORT_SRC; 812 typec_cap.data = TYPEC_PORT_DFP; 813 break; 814 default: 815 ret = -ENODEV; 816 goto err_role_put; 817 } 818 819 ret = devm_tps6598_psy_register(tps); 820 if (ret) 821 goto err_role_put; 822 823 tps->port = typec_register_port(&client->dev, &typec_cap); 824 if (IS_ERR(tps->port)) { 825 ret = PTR_ERR(tps->port); 826 goto err_role_put; 827 } 828 829 if (status & TPS_STATUS_PLUG_PRESENT) { 830 ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &tps->pwr_status); 831 if (ret < 0) { 832 dev_err(tps->dev, "failed to read power status: %d\n", ret); 833 goto err_unregister_port; 834 } 835 ret = tps6598x_connect(tps, status); 836 if (ret) 837 dev_err(&client->dev, "failed to register partner\n"); 838 } 839 840 ret = devm_request_threaded_irq(&client->dev, client->irq, NULL, 841 irq_handler, 842 IRQF_SHARED | IRQF_ONESHOT, 843 dev_name(&client->dev), tps); 844 if (ret) 845 goto err_disconnect; 846 847 i2c_set_clientdata(client, tps); 848 fwnode_handle_put(fwnode); 849 850 tps->wakeup = device_property_read_bool(tps->dev, "wakeup-source"); 851 if (tps->wakeup) { 852 device_init_wakeup(&client->dev, true); 853 enable_irq_wake(client->irq); 854 } 855 856 return 0; 857 858 err_disconnect: 859 tps6598x_disconnect(tps, 0); 860 err_unregister_port: 861 typec_unregister_port(tps->port); 862 err_role_put: 863 usb_role_switch_put(tps->role_sw); 864 err_fwnode_put: 865 fwnode_handle_put(fwnode); 866 err_clear_mask: 867 tps6598x_write64(tps, TPS_REG_INT_MASK1, 0); 868 return ret; 869 } 870 871 static void tps6598x_remove(struct i2c_client *client) 872 { 873 struct tps6598x *tps = i2c_get_clientdata(client); 874 875 tps6598x_disconnect(tps, 0); 876 typec_unregister_port(tps->port); 877 usb_role_switch_put(tps->role_sw); 878 } 879 880 static int __maybe_unused tps6598x_suspend(struct device *dev) 881 { 882 struct i2c_client *client = to_i2c_client(dev); 883 struct tps6598x *tps = i2c_get_clientdata(client); 884 885 if (tps->wakeup) { 886 disable_irq(client->irq); 887 enable_irq_wake(client->irq); 888 } 889 890 return 0; 891 } 892 893 static int __maybe_unused tps6598x_resume(struct device *dev) 894 { 895 struct i2c_client *client = to_i2c_client(dev); 896 struct tps6598x *tps = i2c_get_clientdata(client); 897 898 if (tps->wakeup) { 899 disable_irq_wake(client->irq); 900 enable_irq(client->irq); 901 } 902 903 return 0; 904 } 905 906 static const struct dev_pm_ops tps6598x_pm_ops = { 907 SET_SYSTEM_SLEEP_PM_OPS(tps6598x_suspend, tps6598x_resume) 908 }; 909 910 static const struct of_device_id tps6598x_of_match[] = { 911 { .compatible = "ti,tps6598x", }, 912 { .compatible = "apple,cd321x", }, 913 {} 914 }; 915 MODULE_DEVICE_TABLE(of, tps6598x_of_match); 916 917 static const struct i2c_device_id tps6598x_id[] = { 918 { "tps6598x" }, 919 { } 920 }; 921 MODULE_DEVICE_TABLE(i2c, tps6598x_id); 922 923 static struct i2c_driver tps6598x_i2c_driver = { 924 .driver = { 925 .name = "tps6598x", 926 .pm = &tps6598x_pm_ops, 927 .of_match_table = tps6598x_of_match, 928 }, 929 .probe_new = tps6598x_probe, 930 .remove = tps6598x_remove, 931 .id_table = tps6598x_id, 932 }; 933 module_i2c_driver(tps6598x_i2c_driver); 934 935 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>"); 936 MODULE_LICENSE("GPL v2"); 937 MODULE_DESCRIPTION("TI TPS6598x USB Power Delivery Controller Driver"); 938