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