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 u16 pwr_status; 99 }; 100 101 static enum power_supply_property tps6598x_psy_props[] = { 102 POWER_SUPPLY_PROP_USB_TYPE, 103 POWER_SUPPLY_PROP_ONLINE, 104 }; 105 106 static enum power_supply_usb_type tps6598x_psy_usb_types[] = { 107 POWER_SUPPLY_USB_TYPE_C, 108 POWER_SUPPLY_USB_TYPE_PD, 109 }; 110 111 static const char *tps6598x_psy_name_prefix = "tps6598x-source-psy-"; 112 113 /* 114 * Max data bytes for Data1, Data2, and other registers. See ch 1.3.2: 115 * https://www.ti.com/lit/ug/slvuan1a/slvuan1a.pdf 116 */ 117 #define TPS_MAX_LEN 64 118 119 static int 120 tps6598x_block_read(struct tps6598x *tps, u8 reg, void *val, size_t len) 121 { 122 u8 data[TPS_MAX_LEN + 1]; 123 int ret; 124 125 if (len + 1 > sizeof(data)) 126 return -EINVAL; 127 128 if (!tps->i2c_protocol) 129 return regmap_raw_read(tps->regmap, reg, val, len); 130 131 ret = regmap_raw_read(tps->regmap, reg, data, len + 1); 132 if (ret) 133 return ret; 134 135 if (data[0] < len) 136 return -EIO; 137 138 memcpy(val, &data[1], len); 139 return 0; 140 } 141 142 static int tps6598x_block_write(struct tps6598x *tps, u8 reg, 143 const void *val, size_t len) 144 { 145 u8 data[TPS_MAX_LEN + 1]; 146 147 if (len + 1 > sizeof(data)) 148 return -EINVAL; 149 150 if (!tps->i2c_protocol) 151 return regmap_raw_write(tps->regmap, reg, val, len); 152 153 data[0] = len; 154 memcpy(&data[1], val, len); 155 156 return regmap_raw_write(tps->regmap, reg, data, len + 1); 157 } 158 159 static inline int tps6598x_read8(struct tps6598x *tps, u8 reg, u8 *val) 160 { 161 return tps6598x_block_read(tps, reg, val, sizeof(u8)); 162 } 163 164 static inline int tps6598x_read16(struct tps6598x *tps, u8 reg, u16 *val) 165 { 166 return tps6598x_block_read(tps, reg, val, sizeof(u16)); 167 } 168 169 static inline int tps6598x_read32(struct tps6598x *tps, u8 reg, u32 *val) 170 { 171 return tps6598x_block_read(tps, reg, val, sizeof(u32)); 172 } 173 174 static inline int tps6598x_read64(struct tps6598x *tps, u8 reg, u64 *val) 175 { 176 return tps6598x_block_read(tps, reg, val, sizeof(u64)); 177 } 178 179 static inline int tps6598x_write16(struct tps6598x *tps, u8 reg, u16 val) 180 { 181 return tps6598x_block_write(tps, reg, &val, sizeof(u16)); 182 } 183 184 static inline int tps6598x_write32(struct tps6598x *tps, u8 reg, u32 val) 185 { 186 return tps6598x_block_write(tps, reg, &val, sizeof(u32)); 187 } 188 189 static inline int tps6598x_write64(struct tps6598x *tps, u8 reg, u64 val) 190 { 191 return tps6598x_block_write(tps, reg, &val, sizeof(u64)); 192 } 193 194 static inline int 195 tps6598x_write_4cc(struct tps6598x *tps, u8 reg, const char *val) 196 { 197 return tps6598x_block_write(tps, reg, val, 4); 198 } 199 200 static int tps6598x_read_partner_identity(struct tps6598x *tps) 201 { 202 struct tps6598x_rx_identity_reg id; 203 int ret; 204 205 ret = tps6598x_block_read(tps, TPS_REG_RX_IDENTITY_SOP, 206 &id, sizeof(id)); 207 if (ret) 208 return ret; 209 210 tps->partner_identity = id.identity; 211 212 return 0; 213 } 214 215 static void tps6598x_set_data_role(struct tps6598x *tps, 216 enum typec_data_role role, bool connected) 217 { 218 enum usb_role role_val; 219 220 if (role == TYPEC_HOST) 221 role_val = USB_ROLE_HOST; 222 else 223 role_val = USB_ROLE_DEVICE; 224 225 if (!connected) 226 role_val = USB_ROLE_NONE; 227 228 usb_role_switch_set_role(tps->role_sw, role_val); 229 typec_set_data_role(tps->port, role); 230 } 231 232 static int tps6598x_connect(struct tps6598x *tps, u32 status) 233 { 234 struct typec_partner_desc desc; 235 enum typec_pwr_opmode mode; 236 int ret; 237 238 if (tps->partner) 239 return 0; 240 241 mode = TPS_POWER_STATUS_PWROPMODE(tps->pwr_status); 242 243 desc.usb_pd = mode == TYPEC_PWR_MODE_PD; 244 desc.accessory = TYPEC_ACCESSORY_NONE; /* XXX: handle accessories */ 245 desc.identity = NULL; 246 247 if (desc.usb_pd) { 248 ret = tps6598x_read_partner_identity(tps); 249 if (ret) 250 return ret; 251 desc.identity = &tps->partner_identity; 252 } 253 254 typec_set_pwr_opmode(tps->port, mode); 255 typec_set_pwr_role(tps->port, TPS_STATUS_TO_TYPEC_PORTROLE(status)); 256 typec_set_vconn_role(tps->port, TPS_STATUS_TO_TYPEC_VCONN(status)); 257 if (TPS_STATUS_TO_UPSIDE_DOWN(status)) 258 typec_set_orientation(tps->port, TYPEC_ORIENTATION_REVERSE); 259 else 260 typec_set_orientation(tps->port, TYPEC_ORIENTATION_NORMAL); 261 typec_set_mode(tps->port, TYPEC_STATE_USB); 262 tps6598x_set_data_role(tps, TPS_STATUS_TO_TYPEC_DATAROLE(status), true); 263 264 tps->partner = typec_register_partner(tps->port, &desc); 265 if (IS_ERR(tps->partner)) 266 return PTR_ERR(tps->partner); 267 268 if (desc.identity) 269 typec_partner_set_identity(tps->partner); 270 271 power_supply_changed(tps->psy); 272 273 return 0; 274 } 275 276 static void tps6598x_disconnect(struct tps6598x *tps, u32 status) 277 { 278 if (!IS_ERR(tps->partner)) 279 typec_unregister_partner(tps->partner); 280 tps->partner = NULL; 281 typec_set_pwr_opmode(tps->port, TYPEC_PWR_MODE_USB); 282 typec_set_pwr_role(tps->port, TPS_STATUS_TO_TYPEC_PORTROLE(status)); 283 typec_set_vconn_role(tps->port, TPS_STATUS_TO_TYPEC_VCONN(status)); 284 typec_set_orientation(tps->port, TYPEC_ORIENTATION_NONE); 285 typec_set_mode(tps->port, TYPEC_STATE_SAFE); 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 tps->pwr_status = pwr_status; 459 trace_tps6598x_power_status(pwr_status); 460 461 return true; 462 } 463 464 static void tps6598x_handle_plug_event(struct tps6598x *tps, u32 status) 465 { 466 int ret; 467 468 if (status & TPS_STATUS_PLUG_PRESENT) { 469 ret = tps6598x_connect(tps, status); 470 if (ret) 471 dev_err(tps->dev, "failed to register partner\n"); 472 } else { 473 tps6598x_disconnect(tps, status); 474 } 475 } 476 477 static irqreturn_t cd321x_interrupt(int irq, void *data) 478 { 479 struct tps6598x *tps = data; 480 u64 event = 0; 481 u32 status; 482 int ret; 483 484 mutex_lock(&tps->lock); 485 486 ret = tps6598x_read64(tps, TPS_REG_INT_EVENT1, &event); 487 if (ret) { 488 dev_err(tps->dev, "%s: failed to read events\n", __func__); 489 goto err_unlock; 490 } 491 trace_cd321x_irq(event); 492 493 if (!event) 494 goto err_unlock; 495 496 if (!tps6598x_read_status(tps, &status)) 497 goto err_clear_ints; 498 499 if (event & APPLE_CD_REG_INT_POWER_STATUS_UPDATE) 500 if (!tps6598x_read_power_status(tps)) 501 goto err_clear_ints; 502 503 if (event & APPLE_CD_REG_INT_DATA_STATUS_UPDATE) 504 if (!tps6598x_read_data_status(tps)) 505 goto err_clear_ints; 506 507 /* Handle plug insert or removal */ 508 if (event & APPLE_CD_REG_INT_PLUG_EVENT) 509 tps6598x_handle_plug_event(tps, status); 510 511 err_clear_ints: 512 tps6598x_write64(tps, TPS_REG_INT_CLEAR1, event); 513 514 err_unlock: 515 mutex_unlock(&tps->lock); 516 517 if (event) 518 return IRQ_HANDLED; 519 return IRQ_NONE; 520 } 521 522 static irqreturn_t tps6598x_interrupt(int irq, void *data) 523 { 524 struct tps6598x *tps = data; 525 u64 event1 = 0; 526 u64 event2 = 0; 527 u32 status; 528 int ret; 529 530 mutex_lock(&tps->lock); 531 532 ret = tps6598x_read64(tps, TPS_REG_INT_EVENT1, &event1); 533 ret |= tps6598x_read64(tps, TPS_REG_INT_EVENT2, &event2); 534 if (ret) { 535 dev_err(tps->dev, "%s: failed to read events\n", __func__); 536 goto err_unlock; 537 } 538 trace_tps6598x_irq(event1, event2); 539 540 if (!(event1 | event2)) 541 goto err_unlock; 542 543 if (!tps6598x_read_status(tps, &status)) 544 goto err_clear_ints; 545 546 if ((event1 | event2) & TPS_REG_INT_POWER_STATUS_UPDATE) 547 if (!tps6598x_read_power_status(tps)) 548 goto err_clear_ints; 549 550 if ((event1 | event2) & TPS_REG_INT_DATA_STATUS_UPDATE) 551 if (!tps6598x_read_data_status(tps)) 552 goto err_clear_ints; 553 554 /* Handle plug insert or removal */ 555 if ((event1 | event2) & TPS_REG_INT_PLUG_EVENT) 556 tps6598x_handle_plug_event(tps, status); 557 558 err_clear_ints: 559 tps6598x_write64(tps, TPS_REG_INT_CLEAR1, event1); 560 tps6598x_write64(tps, TPS_REG_INT_CLEAR2, event2); 561 562 err_unlock: 563 mutex_unlock(&tps->lock); 564 565 if (event1 | event2) 566 return IRQ_HANDLED; 567 return IRQ_NONE; 568 } 569 570 static int tps6598x_check_mode(struct tps6598x *tps) 571 { 572 char mode[5] = { }; 573 int ret; 574 575 ret = tps6598x_read32(tps, TPS_REG_MODE, (void *)mode); 576 if (ret) 577 return ret; 578 579 switch (match_string(modes, ARRAY_SIZE(modes), mode)) { 580 case TPS_MODE_APP: 581 return 0; 582 case TPS_MODE_BOOT: 583 dev_warn(tps->dev, "dead-battery condition\n"); 584 return 0; 585 case TPS_MODE_BIST: 586 case TPS_MODE_DISC: 587 default: 588 dev_err(tps->dev, "controller in unsupported mode \"%s\"\n", 589 mode); 590 break; 591 } 592 593 return -ENODEV; 594 } 595 596 static const struct regmap_config tps6598x_regmap_config = { 597 .reg_bits = 8, 598 .val_bits = 8, 599 .max_register = 0x7F, 600 }; 601 602 static int tps6598x_psy_get_online(struct tps6598x *tps, 603 union power_supply_propval *val) 604 { 605 if (TPS_POWER_STATUS_CONNECTION(tps->pwr_status) && 606 TPS_POWER_STATUS_SOURCESINK(tps->pwr_status)) { 607 val->intval = 1; 608 } else { 609 val->intval = 0; 610 } 611 return 0; 612 } 613 614 static int tps6598x_psy_get_prop(struct power_supply *psy, 615 enum power_supply_property psp, 616 union power_supply_propval *val) 617 { 618 struct tps6598x *tps = power_supply_get_drvdata(psy); 619 int ret = 0; 620 621 switch (psp) { 622 case POWER_SUPPLY_PROP_USB_TYPE: 623 if (TPS_POWER_STATUS_PWROPMODE(tps->pwr_status) == TYPEC_PWR_MODE_PD) 624 val->intval = POWER_SUPPLY_USB_TYPE_PD; 625 else 626 val->intval = POWER_SUPPLY_USB_TYPE_C; 627 break; 628 case POWER_SUPPLY_PROP_ONLINE: 629 ret = tps6598x_psy_get_online(tps, val); 630 break; 631 default: 632 ret = -EINVAL; 633 break; 634 } 635 636 return ret; 637 } 638 639 static int cd321x_switch_power_state(struct tps6598x *tps, u8 target_state) 640 { 641 u8 state; 642 int ret; 643 644 ret = tps6598x_read8(tps, TPS_REG_SYSTEM_POWER_STATE, &state); 645 if (ret) 646 return ret; 647 648 if (state == target_state) 649 return 0; 650 651 ret = tps6598x_exec_cmd(tps, "SSPS", sizeof(u8), &target_state, 0, NULL); 652 if (ret) 653 return ret; 654 655 ret = tps6598x_read8(tps, TPS_REG_SYSTEM_POWER_STATE, &state); 656 if (ret) 657 return ret; 658 659 if (state != target_state) 660 return -EINVAL; 661 662 return 0; 663 } 664 665 static int devm_tps6598_psy_register(struct tps6598x *tps) 666 { 667 struct power_supply_config psy_cfg = {}; 668 const char *port_dev_name = dev_name(tps->dev); 669 char *psy_name; 670 671 psy_cfg.drv_data = tps; 672 psy_cfg.fwnode = dev_fwnode(tps->dev); 673 674 psy_name = devm_kasprintf(tps->dev, GFP_KERNEL, "%s%s", tps6598x_psy_name_prefix, 675 port_dev_name); 676 if (!psy_name) 677 return -ENOMEM; 678 679 tps->psy_desc.name = psy_name; 680 tps->psy_desc.type = POWER_SUPPLY_TYPE_USB; 681 tps->psy_desc.usb_types = tps6598x_psy_usb_types; 682 tps->psy_desc.num_usb_types = ARRAY_SIZE(tps6598x_psy_usb_types); 683 tps->psy_desc.properties = tps6598x_psy_props; 684 tps->psy_desc.num_properties = ARRAY_SIZE(tps6598x_psy_props); 685 tps->psy_desc.get_property = tps6598x_psy_get_prop; 686 687 tps->usb_type = POWER_SUPPLY_USB_TYPE_C; 688 689 tps->psy = devm_power_supply_register(tps->dev, &tps->psy_desc, 690 &psy_cfg); 691 return PTR_ERR_OR_ZERO(tps->psy); 692 } 693 694 static int tps6598x_probe(struct i2c_client *client) 695 { 696 irq_handler_t irq_handler = tps6598x_interrupt; 697 struct device_node *np = client->dev.of_node; 698 struct typec_capability typec_cap = { }; 699 struct tps6598x *tps; 700 struct fwnode_handle *fwnode; 701 u32 status; 702 u32 conf; 703 u32 vid; 704 int ret; 705 u64 mask1; 706 707 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL); 708 if (!tps) 709 return -ENOMEM; 710 711 mutex_init(&tps->lock); 712 tps->dev = &client->dev; 713 714 tps->regmap = devm_regmap_init_i2c(client, &tps6598x_regmap_config); 715 if (IS_ERR(tps->regmap)) 716 return PTR_ERR(tps->regmap); 717 718 ret = tps6598x_read32(tps, TPS_REG_VID, &vid); 719 if (ret < 0 || !vid) 720 return -ENODEV; 721 722 /* 723 * Checking can the adapter handle SMBus protocol. If it can not, the 724 * driver needs to take care of block reads separately. 725 */ 726 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) 727 tps->i2c_protocol = true; 728 729 if (np && of_device_is_compatible(np, "apple,cd321x")) { 730 /* Switch CD321X chips to the correct system power state */ 731 ret = cd321x_switch_power_state(tps, TPS_SYSTEM_POWER_STATE_S0); 732 if (ret) 733 return ret; 734 735 /* CD321X chips have all interrupts masked initially */ 736 mask1 = APPLE_CD_REG_INT_POWER_STATUS_UPDATE | 737 APPLE_CD_REG_INT_DATA_STATUS_UPDATE | 738 APPLE_CD_REG_INT_PLUG_EVENT; 739 740 irq_handler = cd321x_interrupt; 741 } else { 742 /* Enable power status, data status and plug event interrupts */ 743 mask1 = TPS_REG_INT_POWER_STATUS_UPDATE | 744 TPS_REG_INT_DATA_STATUS_UPDATE | 745 TPS_REG_INT_PLUG_EVENT; 746 } 747 748 /* Make sure the controller has application firmware running */ 749 ret = tps6598x_check_mode(tps); 750 if (ret) 751 return ret; 752 753 ret = tps6598x_write64(tps, TPS_REG_INT_MASK1, mask1); 754 if (ret) 755 return ret; 756 757 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status); 758 if (ret < 0) 759 goto err_clear_mask; 760 trace_tps6598x_status(status); 761 762 ret = tps6598x_read32(tps, TPS_REG_SYSTEM_CONF, &conf); 763 if (ret < 0) 764 goto err_clear_mask; 765 766 /* 767 * This fwnode has a "compatible" property, but is never populated as a 768 * struct device. Instead we simply parse it to read the properties. 769 * This breaks fw_devlink=on. To maintain backward compatibility 770 * with existing DT files, we work around this by deleting any 771 * fwnode_links to/from this fwnode. 772 */ 773 fwnode = device_get_named_child_node(&client->dev, "connector"); 774 if (fwnode) 775 fw_devlink_purge_absent_suppliers(fwnode); 776 777 tps->role_sw = fwnode_usb_role_switch_get(fwnode); 778 if (IS_ERR(tps->role_sw)) { 779 ret = PTR_ERR(tps->role_sw); 780 goto err_fwnode_put; 781 } 782 783 typec_cap.revision = USB_TYPEC_REV_1_2; 784 typec_cap.pd_revision = 0x200; 785 typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE; 786 typec_cap.driver_data = tps; 787 typec_cap.ops = &tps6598x_ops; 788 typec_cap.fwnode = fwnode; 789 790 switch (TPS_SYSCONF_PORTINFO(conf)) { 791 case TPS_PORTINFO_SINK_ACCESSORY: 792 case TPS_PORTINFO_SINK: 793 typec_cap.type = TYPEC_PORT_SNK; 794 typec_cap.data = TYPEC_PORT_UFP; 795 break; 796 case TPS_PORTINFO_DRP_UFP_DRD: 797 case TPS_PORTINFO_DRP_DFP_DRD: 798 typec_cap.type = TYPEC_PORT_DRP; 799 typec_cap.data = TYPEC_PORT_DRD; 800 break; 801 case TPS_PORTINFO_DRP_UFP: 802 typec_cap.type = TYPEC_PORT_DRP; 803 typec_cap.data = TYPEC_PORT_UFP; 804 break; 805 case TPS_PORTINFO_DRP_DFP: 806 typec_cap.type = TYPEC_PORT_DRP; 807 typec_cap.data = TYPEC_PORT_DFP; 808 break; 809 case TPS_PORTINFO_SOURCE: 810 typec_cap.type = TYPEC_PORT_SRC; 811 typec_cap.data = TYPEC_PORT_DFP; 812 break; 813 default: 814 ret = -ENODEV; 815 goto err_role_put; 816 } 817 818 ret = devm_tps6598_psy_register(tps); 819 if (ret) 820 goto err_role_put; 821 822 tps->port = typec_register_port(&client->dev, &typec_cap); 823 if (IS_ERR(tps->port)) { 824 ret = PTR_ERR(tps->port); 825 goto err_role_put; 826 } 827 828 if (status & TPS_STATUS_PLUG_PRESENT) { 829 ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &tps->pwr_status); 830 if (ret < 0) { 831 dev_err(tps->dev, "failed to read power status: %d\n", ret); 832 goto err_unregister_port; 833 } 834 ret = tps6598x_connect(tps, status); 835 if (ret) 836 dev_err(&client->dev, "failed to register partner\n"); 837 } 838 839 ret = devm_request_threaded_irq(&client->dev, client->irq, NULL, 840 irq_handler, 841 IRQF_SHARED | IRQF_ONESHOT, 842 dev_name(&client->dev), tps); 843 if (ret) 844 goto err_disconnect; 845 846 i2c_set_clientdata(client, tps); 847 fwnode_handle_put(fwnode); 848 849 return 0; 850 851 err_disconnect: 852 tps6598x_disconnect(tps, 0); 853 err_unregister_port: 854 typec_unregister_port(tps->port); 855 err_role_put: 856 usb_role_switch_put(tps->role_sw); 857 err_fwnode_put: 858 fwnode_handle_put(fwnode); 859 err_clear_mask: 860 tps6598x_write64(tps, TPS_REG_INT_MASK1, 0); 861 return ret; 862 } 863 864 static void tps6598x_remove(struct i2c_client *client) 865 { 866 struct tps6598x *tps = i2c_get_clientdata(client); 867 868 tps6598x_disconnect(tps, 0); 869 typec_unregister_port(tps->port); 870 usb_role_switch_put(tps->role_sw); 871 } 872 873 static const struct of_device_id tps6598x_of_match[] = { 874 { .compatible = "ti,tps6598x", }, 875 { .compatible = "apple,cd321x", }, 876 {} 877 }; 878 MODULE_DEVICE_TABLE(of, tps6598x_of_match); 879 880 static const struct i2c_device_id tps6598x_id[] = { 881 { "tps6598x" }, 882 { } 883 }; 884 MODULE_DEVICE_TABLE(i2c, tps6598x_id); 885 886 static struct i2c_driver tps6598x_i2c_driver = { 887 .driver = { 888 .name = "tps6598x", 889 .of_match_table = tps6598x_of_match, 890 }, 891 .probe_new = tps6598x_probe, 892 .remove = tps6598x_remove, 893 .id_table = tps6598x_id, 894 }; 895 module_i2c_driver(tps6598x_i2c_driver); 896 897 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>"); 898 MODULE_LICENSE("GPL v2"); 899 MODULE_DESCRIPTION("TI TPS6598x USB Power Delivery Controller Driver"); 900