1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for TI TPS6598x USB Power Delivery controller family
5 * Copyright (C) 2017, Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
11 #include <linux/i2c.h>
12 #include <linux/acpi.h>
13 #include <linux/module.h>
14 #include <linux/power_supply.h>
15 #include <linux/regmap.h>
16 #include <linux/interrupt.h>
17 #include <linux/usb/typec.h>
18 #include <linux/usb/role.h>
20 #define CREATE_TRACE_POINTS
21 #include "tps6598x_trace.h"
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_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
40 /* TPS_REG_STATUS bits */
41 #define TPS_STATUS_PLUG_PRESENT BIT(0)
42 #define TPS_STATUS_ORIENTATION BIT(4)
43 #define TPS_STATUS_PORTROLE(s) (!!((s) & BIT(5)))
44 #define TPS_STATUS_DATAROLE(s) (!!((s) & BIT(6)))
45 #define TPS_STATUS_VCONN(s) (!!((s) & BIT(7)))
47 /* TPS_REG_SYSTEM_CONF bits */
48 #define TPS_SYSCONF_PORTINFO(c) ((c) & 7)
52 TPS_PORTINFO_SINK_ACCESSORY,
54 TPS_PORTINFO_DRP_UFP_DRD,
56 TPS_PORTINFO_DRP_DFP_DRD,
60 /* TPS_REG_POWER_STATUS bits */
61 #define TPS_POWER_STATUS_CONNECTION BIT(0)
62 #define TPS_POWER_STATUS_SOURCESINK BIT(1)
63 #define TPS_POWER_STATUS_PWROPMODE(p) (((p) & GENMASK(3, 2)) >> 2)
65 /* TPS_REG_RX_IDENTITY_SOP */
66 struct tps6598x_rx_identity_reg {
68 struct usb_pd_identity identity;
72 /* Standard Task return codes */
73 #define TPS_TASK_TIMEOUT 1
74 #define TPS_TASK_REJECTED 3
83 static const char *const modes[] = {
84 [TPS_MODE_APP] = "APP ",
85 [TPS_MODE_BOOT] = "BOOT",
86 [TPS_MODE_BIST] = "BIST",
87 [TPS_MODE_DISC] = "DISC",
90 /* Unrecognized commands will be replaced with "!CMD" */
91 #define INVALID_CMD(_cmd_) (_cmd_ == 0x444d4321)
95 struct regmap *regmap;
96 struct mutex lock; /* device lock */
99 struct typec_port *port;
100 struct typec_partner *partner;
101 struct usb_pd_identity partner_identity;
102 struct usb_role_switch *role_sw;
103 struct typec_capability typec_cap;
105 struct power_supply *psy;
106 struct power_supply_desc psy_desc;
107 enum power_supply_usb_type usb_type;
110 static enum power_supply_property tps6598x_psy_props[] = {
111 POWER_SUPPLY_PROP_USB_TYPE,
112 POWER_SUPPLY_PROP_ONLINE,
115 static enum power_supply_usb_type tps6598x_psy_usb_types[] = {
116 POWER_SUPPLY_USB_TYPE_C,
117 POWER_SUPPLY_USB_TYPE_PD,
120 static const char *tps6598x_psy_name_prefix = "tps6598x-source-psy-";
123 * Max data bytes for Data1, Data2, and other registers. See ch 1.3.2:
124 * https://www.ti.com/lit/ug/slvuan1a/slvuan1a.pdf
126 #define TPS_MAX_LEN 64
129 tps6598x_block_read(struct tps6598x *tps, u8 reg, void *val, size_t len)
131 u8 data[TPS_MAX_LEN + 1];
134 if (WARN_ON(len + 1 > sizeof(data)))
137 if (!tps->i2c_protocol)
138 return regmap_raw_read(tps->regmap, reg, val, len);
140 ret = regmap_raw_read(tps->regmap, reg, data, sizeof(data));
147 memcpy(val, &data[1], len);
151 static int tps6598x_block_write(struct tps6598x *tps, u8 reg,
152 const void *val, size_t len)
154 u8 data[TPS_MAX_LEN + 1];
156 if (!tps->i2c_protocol)
157 return regmap_raw_write(tps->regmap, reg, val, len);
160 memcpy(&data[1], val, len);
162 return regmap_raw_write(tps->regmap, reg, data, sizeof(data));
165 static inline int tps6598x_read16(struct tps6598x *tps, u8 reg, u16 *val)
167 return tps6598x_block_read(tps, reg, val, sizeof(u16));
170 static inline int tps6598x_read32(struct tps6598x *tps, u8 reg, u32 *val)
172 return tps6598x_block_read(tps, reg, val, sizeof(u32));
175 static inline int tps6598x_read64(struct tps6598x *tps, u8 reg, u64 *val)
177 return tps6598x_block_read(tps, reg, val, sizeof(u64));
180 static inline int tps6598x_write16(struct tps6598x *tps, u8 reg, u16 val)
182 return tps6598x_block_write(tps, reg, &val, sizeof(u16));
185 static inline int tps6598x_write32(struct tps6598x *tps, u8 reg, u32 val)
187 return tps6598x_block_write(tps, reg, &val, sizeof(u32));
190 static inline int tps6598x_write64(struct tps6598x *tps, u8 reg, u64 val)
192 return tps6598x_block_write(tps, reg, &val, sizeof(u64));
196 tps6598x_write_4cc(struct tps6598x *tps, u8 reg, const char *val)
198 return tps6598x_block_write(tps, reg, val, 4);
201 static int tps6598x_read_partner_identity(struct tps6598x *tps)
203 struct tps6598x_rx_identity_reg id;
206 ret = tps6598x_block_read(tps, TPS_REG_RX_IDENTITY_SOP,
211 tps->partner_identity = id.identity;
216 static void tps6598x_set_data_role(struct tps6598x *tps,
217 enum typec_data_role role, bool connected)
219 enum usb_role role_val;
221 if (role == TYPEC_HOST)
222 role_val = USB_ROLE_HOST;
224 role_val = USB_ROLE_DEVICE;
227 role_val = USB_ROLE_NONE;
229 usb_role_switch_set_role(tps->role_sw, role_val);
230 typec_set_data_role(tps->port, role);
233 static int tps6598x_connect(struct tps6598x *tps, u32 status)
235 struct typec_partner_desc desc;
236 enum typec_pwr_opmode mode;
243 ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
247 mode = TPS_POWER_STATUS_PWROPMODE(pwr_status);
249 desc.usb_pd = mode == TYPEC_PWR_MODE_PD;
250 desc.accessory = TYPEC_ACCESSORY_NONE; /* XXX: handle accessories */
251 desc.identity = NULL;
254 ret = tps6598x_read_partner_identity(tps);
257 desc.identity = &tps->partner_identity;
260 typec_set_pwr_opmode(tps->port, mode);
261 typec_set_pwr_role(tps->port, TPS_STATUS_PORTROLE(status));
262 typec_set_vconn_role(tps->port, TPS_STATUS_VCONN(status));
263 tps6598x_set_data_role(tps, TPS_STATUS_DATAROLE(status), true);
265 tps->partner = typec_register_partner(tps->port, &desc);
266 if (IS_ERR(tps->partner))
267 return PTR_ERR(tps->partner);
270 typec_partner_set_identity(tps->partner);
272 power_supply_changed(tps->psy);
277 static void tps6598x_disconnect(struct tps6598x *tps, u32 status)
279 if (!IS_ERR(tps->partner))
280 typec_unregister_partner(tps->partner);
282 typec_set_pwr_opmode(tps->port, TYPEC_PWR_MODE_USB);
283 typec_set_pwr_role(tps->port, TPS_STATUS_PORTROLE(status));
284 typec_set_vconn_role(tps->port, TPS_STATUS_VCONN(status));
285 tps6598x_set_data_role(tps, TPS_STATUS_DATAROLE(status), false);
286 power_supply_changed(tps->psy);
289 static int tps6598x_exec_cmd(struct tps6598x *tps, const char *cmd,
290 size_t in_len, u8 *in_data,
291 size_t out_len, u8 *out_data)
293 unsigned long timeout;
297 ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
300 if (val && !INVALID_CMD(val))
304 ret = tps6598x_block_write(tps, TPS_REG_DATA1,
310 ret = tps6598x_write_4cc(tps, TPS_REG_CMD1, cmd);
314 /* XXX: Using 1s for now, but it may not be enough for every command. */
315 timeout = jiffies + msecs_to_jiffies(1000);
318 ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
321 if (INVALID_CMD(val))
324 if (time_is_before_jiffies(timeout))
329 ret = tps6598x_block_read(tps, TPS_REG_DATA1,
335 ret = tps6598x_block_read(tps, TPS_REG_DATA1, &val, sizeof(u8));
341 case TPS_TASK_TIMEOUT:
343 case TPS_TASK_REJECTED:
352 static int tps6598x_dr_set(struct typec_port *port, enum typec_data_role role)
354 const char *cmd = (role == TYPEC_DEVICE) ? "SWUF" : "SWDF";
355 struct tps6598x *tps = typec_get_drvdata(port);
359 mutex_lock(&tps->lock);
361 ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
365 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
369 if (role != TPS_STATUS_DATAROLE(status)) {
374 tps6598x_set_data_role(tps, role, true);
377 mutex_unlock(&tps->lock);
382 static int tps6598x_pr_set(struct typec_port *port, enum typec_role role)
384 const char *cmd = (role == TYPEC_SINK) ? "SWSk" : "SWSr";
385 struct tps6598x *tps = typec_get_drvdata(port);
389 mutex_lock(&tps->lock);
391 ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
395 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
399 if (role != TPS_STATUS_PORTROLE(status)) {
404 typec_set_pwr_role(tps->port, role);
407 mutex_unlock(&tps->lock);
412 static const struct typec_operations tps6598x_ops = {
413 .dr_set = tps6598x_dr_set,
414 .pr_set = tps6598x_pr_set,
417 static irqreturn_t tps6598x_interrupt(int irq, void *data)
419 struct tps6598x *tps = data;
425 mutex_lock(&tps->lock);
427 ret = tps6598x_read64(tps, TPS_REG_INT_EVENT1, &event1);
428 ret |= tps6598x_read64(tps, TPS_REG_INT_EVENT2, &event2);
430 dev_err(tps->dev, "%s: failed to read events\n", __func__);
433 trace_tps6598x_irq(event1, event2);
435 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
437 dev_err(tps->dev, "%s: failed to read status\n", __func__);
441 /* Handle plug insert or removal */
442 if ((event1 | event2) & TPS_REG_INT_PLUG_EVENT) {
443 if (status & TPS_STATUS_PLUG_PRESENT) {
444 ret = tps6598x_connect(tps, status);
447 "failed to register partner\n");
449 tps6598x_disconnect(tps, status);
454 tps6598x_write64(tps, TPS_REG_INT_CLEAR1, event1);
455 tps6598x_write64(tps, TPS_REG_INT_CLEAR2, event2);
458 mutex_unlock(&tps->lock);
463 static int tps6598x_check_mode(struct tps6598x *tps)
468 ret = tps6598x_read32(tps, TPS_REG_MODE, (void *)mode);
472 switch (match_string(modes, ARRAY_SIZE(modes), mode)) {
476 dev_warn(tps->dev, "dead-battery condition\n");
481 dev_err(tps->dev, "controller in unsupported mode \"%s\"\n",
489 static const struct regmap_config tps6598x_regmap_config = {
492 .max_register = 0x7F,
495 static int tps6598x_psy_get_online(struct tps6598x *tps,
496 union power_supply_propval *val)
501 ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
505 if ((pwr_status & TPS_POWER_STATUS_CONNECTION) &&
506 (pwr_status & TPS_POWER_STATUS_SOURCESINK)) {
514 static int tps6598x_psy_get_prop(struct power_supply *psy,
515 enum power_supply_property psp,
516 union power_supply_propval *val)
518 struct tps6598x *tps = power_supply_get_drvdata(psy);
523 case POWER_SUPPLY_PROP_USB_TYPE:
524 ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
527 if (TPS_POWER_STATUS_PWROPMODE(pwr_status) == TYPEC_PWR_MODE_PD)
528 val->intval = POWER_SUPPLY_USB_TYPE_PD;
530 val->intval = POWER_SUPPLY_USB_TYPE_C;
532 case POWER_SUPPLY_PROP_ONLINE:
533 ret = tps6598x_psy_get_online(tps, val);
543 static int devm_tps6598_psy_register(struct tps6598x *tps)
545 struct power_supply_config psy_cfg = {};
546 const char *port_dev_name = dev_name(tps->dev);
549 psy_cfg.drv_data = tps;
550 psy_cfg.fwnode = dev_fwnode(tps->dev);
552 psy_name = devm_kasprintf(tps->dev, GFP_KERNEL, "%s%s", tps6598x_psy_name_prefix,
557 tps->psy_desc.name = psy_name;
558 tps->psy_desc.type = POWER_SUPPLY_TYPE_USB;
559 tps->psy_desc.usb_types = tps6598x_psy_usb_types;
560 tps->psy_desc.num_usb_types = ARRAY_SIZE(tps6598x_psy_usb_types);
561 tps->psy_desc.properties = tps6598x_psy_props;
562 tps->psy_desc.num_properties = ARRAY_SIZE(tps6598x_psy_props);
563 tps->psy_desc.get_property = tps6598x_psy_get_prop;
565 tps->usb_type = POWER_SUPPLY_USB_TYPE_C;
567 tps->psy = devm_power_supply_register(tps->dev, &tps->psy_desc,
569 return PTR_ERR_OR_ZERO(tps->psy);
572 static int tps6598x_probe(struct i2c_client *client)
574 struct typec_capability typec_cap = { };
575 struct tps6598x *tps;
576 struct fwnode_handle *fwnode;
582 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
586 mutex_init(&tps->lock);
587 tps->dev = &client->dev;
589 tps->regmap = devm_regmap_init_i2c(client, &tps6598x_regmap_config);
590 if (IS_ERR(tps->regmap))
591 return PTR_ERR(tps->regmap);
593 ret = tps6598x_read32(tps, TPS_REG_VID, &vid);
598 * Checking can the adapter handle SMBus protocol. If it can not, the
599 * driver needs to take care of block reads separately.
601 * FIXME: Testing with I2C_FUNC_I2C. regmap-i2c uses I2C protocol
602 * unconditionally if the adapter has I2C_FUNC_I2C set.
604 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
605 tps->i2c_protocol = true;
607 /* Make sure the controller has application firmware running */
608 ret = tps6598x_check_mode(tps);
612 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
616 ret = tps6598x_read32(tps, TPS_REG_SYSTEM_CONF, &conf);
620 fwnode = device_get_named_child_node(&client->dev, "connector");
622 return PTR_ERR(fwnode);
624 tps->role_sw = fwnode_usb_role_switch_get(fwnode);
625 if (IS_ERR(tps->role_sw)) {
626 ret = PTR_ERR(tps->role_sw);
630 typec_cap.revision = USB_TYPEC_REV_1_2;
631 typec_cap.pd_revision = 0x200;
632 typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
633 typec_cap.driver_data = tps;
634 typec_cap.ops = &tps6598x_ops;
635 typec_cap.fwnode = fwnode;
637 switch (TPS_SYSCONF_PORTINFO(conf)) {
638 case TPS_PORTINFO_SINK_ACCESSORY:
639 case TPS_PORTINFO_SINK:
640 typec_cap.type = TYPEC_PORT_SNK;
641 typec_cap.data = TYPEC_PORT_UFP;
643 case TPS_PORTINFO_DRP_UFP_DRD:
644 case TPS_PORTINFO_DRP_DFP_DRD:
645 typec_cap.type = TYPEC_PORT_DRP;
646 typec_cap.data = TYPEC_PORT_DRD;
648 case TPS_PORTINFO_DRP_UFP:
649 typec_cap.type = TYPEC_PORT_DRP;
650 typec_cap.data = TYPEC_PORT_UFP;
652 case TPS_PORTINFO_DRP_DFP:
653 typec_cap.type = TYPEC_PORT_DRP;
654 typec_cap.data = TYPEC_PORT_DFP;
656 case TPS_PORTINFO_SOURCE:
657 typec_cap.type = TYPEC_PORT_SRC;
658 typec_cap.data = TYPEC_PORT_DFP;
665 ret = devm_tps6598_psy_register(tps);
669 tps->port = typec_register_port(&client->dev, &typec_cap);
670 if (IS_ERR(tps->port)) {
671 ret = PTR_ERR(tps->port);
674 fwnode_handle_put(fwnode);
676 if (status & TPS_STATUS_PLUG_PRESENT) {
677 ret = tps6598x_connect(tps, status);
679 dev_err(&client->dev, "failed to register partner\n");
682 ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
684 IRQF_SHARED | IRQF_ONESHOT,
685 dev_name(&client->dev), tps);
687 tps6598x_disconnect(tps, 0);
688 typec_unregister_port(tps->port);
692 i2c_set_clientdata(client, tps);
697 usb_role_switch_put(tps->role_sw);
699 fwnode_handle_put(fwnode);
704 static int tps6598x_remove(struct i2c_client *client)
706 struct tps6598x *tps = i2c_get_clientdata(client);
708 tps6598x_disconnect(tps, 0);
709 typec_unregister_port(tps->port);
710 usb_role_switch_put(tps->role_sw);
715 static const struct of_device_id tps6598x_of_match[] = {
716 { .compatible = "ti,tps6598x", },
719 MODULE_DEVICE_TABLE(of, tps6598x_of_match);
721 static const struct i2c_device_id tps6598x_id[] = {
725 MODULE_DEVICE_TABLE(i2c, tps6598x_id);
727 static struct i2c_driver tps6598x_i2c_driver = {
730 .of_match_table = tps6598x_of_match,
732 .probe_new = tps6598x_probe,
733 .remove = tps6598x_remove,
734 .id_table = tps6598x_id,
736 module_i2c_driver(tps6598x_i2c_driver);
738 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
739 MODULE_LICENSE("GPL v2");
740 MODULE_DESCRIPTION("TI TPS6598x USB Power Delivery Controller Driver");