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>
10 #include <linux/acpi.h>
11 #include <linux/module.h>
12 #include <linux/power_supply.h>
13 #include <linux/regmap.h>
14 #include <linux/interrupt.h>
15 #include <linux/usb/typec.h>
16 #include <linux/usb/role.h>
21 /* Register offsets */
22 #define TPS_REG_VID 0x00
23 #define TPS_REG_MODE 0x03
24 #define TPS_REG_CMD1 0x08
25 #define TPS_REG_DATA1 0x09
26 #define TPS_REG_INT_EVENT1 0x14
27 #define TPS_REG_INT_EVENT2 0x15
28 #define TPS_REG_INT_MASK1 0x16
29 #define TPS_REG_INT_MASK2 0x17
30 #define TPS_REG_INT_CLEAR1 0x18
31 #define TPS_REG_INT_CLEAR2 0x19
32 #define TPS_REG_STATUS 0x1a
33 #define TPS_REG_SYSTEM_CONF 0x28
34 #define TPS_REG_CTRL_CONF 0x29
35 #define TPS_REG_POWER_STATUS 0x3f
36 #define TPS_REG_RX_IDENTITY_SOP 0x48
37 #define TPS_REG_DATA_STATUS 0x5f
39 /* TPS_REG_SYSTEM_CONF bits */
40 #define TPS_SYSCONF_PORTINFO(c) ((c) & 7)
44 TPS_PORTINFO_SINK_ACCESSORY,
46 TPS_PORTINFO_DRP_UFP_DRD,
48 TPS_PORTINFO_DRP_DFP_DRD,
52 /* TPS_REG_RX_IDENTITY_SOP */
53 struct tps6598x_rx_identity_reg {
55 struct usb_pd_identity identity;
58 /* Standard Task return codes */
59 #define TPS_TASK_TIMEOUT 1
60 #define TPS_TASK_REJECTED 3
69 static const char *const modes[] = {
70 [TPS_MODE_APP] = "APP ",
71 [TPS_MODE_BOOT] = "BOOT",
72 [TPS_MODE_BIST] = "BIST",
73 [TPS_MODE_DISC] = "DISC",
76 /* Unrecognized commands will be replaced with "!CMD" */
77 #define INVALID_CMD(_cmd_) (_cmd_ == 0x444d4321)
81 struct regmap *regmap;
82 struct mutex lock; /* device lock */
85 struct typec_port *port;
86 struct typec_partner *partner;
87 struct usb_pd_identity partner_identity;
88 struct usb_role_switch *role_sw;
89 struct typec_capability typec_cap;
91 struct power_supply *psy;
92 struct power_supply_desc psy_desc;
93 enum power_supply_usb_type usb_type;
96 static enum power_supply_property tps6598x_psy_props[] = {
97 POWER_SUPPLY_PROP_USB_TYPE,
98 POWER_SUPPLY_PROP_ONLINE,
101 static enum power_supply_usb_type tps6598x_psy_usb_types[] = {
102 POWER_SUPPLY_USB_TYPE_C,
103 POWER_SUPPLY_USB_TYPE_PD,
106 static const char *tps6598x_psy_name_prefix = "tps6598x-source-psy-";
109 * Max data bytes for Data1, Data2, and other registers. See ch 1.3.2:
110 * https://www.ti.com/lit/ug/slvuan1a/slvuan1a.pdf
112 #define TPS_MAX_LEN 64
115 tps6598x_block_read(struct tps6598x *tps, u8 reg, void *val, size_t len)
117 u8 data[TPS_MAX_LEN + 1];
120 if (WARN_ON(len + 1 > sizeof(data)))
123 if (!tps->i2c_protocol)
124 return regmap_raw_read(tps->regmap, reg, val, len);
126 ret = regmap_raw_read(tps->regmap, reg, data, sizeof(data));
133 memcpy(val, &data[1], len);
137 static int tps6598x_block_write(struct tps6598x *tps, u8 reg,
138 const void *val, size_t len)
140 u8 data[TPS_MAX_LEN + 1];
142 if (!tps->i2c_protocol)
143 return regmap_raw_write(tps->regmap, reg, val, len);
146 memcpy(&data[1], val, len);
148 return regmap_raw_write(tps->regmap, reg, data, sizeof(data));
151 static inline int tps6598x_read16(struct tps6598x *tps, u8 reg, u16 *val)
153 return tps6598x_block_read(tps, reg, val, sizeof(u16));
156 static inline int tps6598x_read32(struct tps6598x *tps, u8 reg, u32 *val)
158 return tps6598x_block_read(tps, reg, val, sizeof(u32));
161 static inline int tps6598x_read64(struct tps6598x *tps, u8 reg, u64 *val)
163 return tps6598x_block_read(tps, reg, val, sizeof(u64));
166 static inline int tps6598x_write16(struct tps6598x *tps, u8 reg, u16 val)
168 return tps6598x_block_write(tps, reg, &val, sizeof(u16));
171 static inline int tps6598x_write32(struct tps6598x *tps, u8 reg, u32 val)
173 return tps6598x_block_write(tps, reg, &val, sizeof(u32));
176 static inline int tps6598x_write64(struct tps6598x *tps, u8 reg, u64 val)
178 return tps6598x_block_write(tps, reg, &val, sizeof(u64));
182 tps6598x_write_4cc(struct tps6598x *tps, u8 reg, const char *val)
184 return tps6598x_block_write(tps, reg, val, 4);
187 static int tps6598x_read_partner_identity(struct tps6598x *tps)
189 struct tps6598x_rx_identity_reg id;
192 ret = tps6598x_block_read(tps, TPS_REG_RX_IDENTITY_SOP,
197 tps->partner_identity = id.identity;
202 static void tps6598x_set_data_role(struct tps6598x *tps,
203 enum typec_data_role role, bool connected)
205 enum usb_role role_val;
207 if (role == TYPEC_HOST)
208 role_val = USB_ROLE_HOST;
210 role_val = USB_ROLE_DEVICE;
213 role_val = USB_ROLE_NONE;
215 usb_role_switch_set_role(tps->role_sw, role_val);
216 typec_set_data_role(tps->port, role);
219 static int tps6598x_connect(struct tps6598x *tps, u32 status)
221 struct typec_partner_desc desc;
222 enum typec_pwr_opmode mode;
229 ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
233 mode = TPS_POWER_STATUS_PWROPMODE(pwr_status);
235 desc.usb_pd = mode == TYPEC_PWR_MODE_PD;
236 desc.accessory = TYPEC_ACCESSORY_NONE; /* XXX: handle accessories */
237 desc.identity = NULL;
240 ret = tps6598x_read_partner_identity(tps);
243 desc.identity = &tps->partner_identity;
246 typec_set_pwr_opmode(tps->port, mode);
247 typec_set_pwr_role(tps->port, TPS_STATUS_TO_TYPEC_PORTROLE(status));
248 typec_set_vconn_role(tps->port, TPS_STATUS_TO_TYPEC_VCONN(status));
249 tps6598x_set_data_role(tps, TPS_STATUS_TO_TYPEC_DATAROLE(status), true);
251 tps->partner = typec_register_partner(tps->port, &desc);
252 if (IS_ERR(tps->partner))
253 return PTR_ERR(tps->partner);
256 typec_partner_set_identity(tps->partner);
258 power_supply_changed(tps->psy);
263 static void tps6598x_disconnect(struct tps6598x *tps, u32 status)
265 if (!IS_ERR(tps->partner))
266 typec_unregister_partner(tps->partner);
268 typec_set_pwr_opmode(tps->port, TYPEC_PWR_MODE_USB);
269 typec_set_pwr_role(tps->port, TPS_STATUS_TO_TYPEC_PORTROLE(status));
270 typec_set_vconn_role(tps->port, TPS_STATUS_TO_TYPEC_VCONN(status));
271 tps6598x_set_data_role(tps, TPS_STATUS_TO_TYPEC_DATAROLE(status), false);
273 power_supply_changed(tps->psy);
276 static int tps6598x_exec_cmd(struct tps6598x *tps, const char *cmd,
277 size_t in_len, u8 *in_data,
278 size_t out_len, u8 *out_data)
280 unsigned long timeout;
284 ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
287 if (val && !INVALID_CMD(val))
291 ret = tps6598x_block_write(tps, TPS_REG_DATA1,
297 ret = tps6598x_write_4cc(tps, TPS_REG_CMD1, cmd);
301 /* XXX: Using 1s for now, but it may not be enough for every command. */
302 timeout = jiffies + msecs_to_jiffies(1000);
305 ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
308 if (INVALID_CMD(val))
311 if (time_is_before_jiffies(timeout))
316 ret = tps6598x_block_read(tps, TPS_REG_DATA1,
322 ret = tps6598x_block_read(tps, TPS_REG_DATA1, &val, sizeof(u8));
328 case TPS_TASK_TIMEOUT:
330 case TPS_TASK_REJECTED:
339 static int tps6598x_dr_set(struct typec_port *port, enum typec_data_role role)
341 const char *cmd = (role == TYPEC_DEVICE) ? "SWUF" : "SWDF";
342 struct tps6598x *tps = typec_get_drvdata(port);
346 mutex_lock(&tps->lock);
348 ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
352 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
356 if (role != TPS_STATUS_TO_TYPEC_DATAROLE(status)) {
361 tps6598x_set_data_role(tps, role, true);
364 mutex_unlock(&tps->lock);
369 static int tps6598x_pr_set(struct typec_port *port, enum typec_role role)
371 const char *cmd = (role == TYPEC_SINK) ? "SWSk" : "SWSr";
372 struct tps6598x *tps = typec_get_drvdata(port);
376 mutex_lock(&tps->lock);
378 ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
382 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
386 if (role != TPS_STATUS_TO_TYPEC_PORTROLE(status)) {
391 typec_set_pwr_role(tps->port, role);
394 mutex_unlock(&tps->lock);
399 static const struct typec_operations tps6598x_ops = {
400 .dr_set = tps6598x_dr_set,
401 .pr_set = tps6598x_pr_set,
404 static irqreturn_t tps6598x_interrupt(int irq, void *data)
406 struct tps6598x *tps = data;
409 u32 status, data_status;
413 mutex_lock(&tps->lock);
415 ret = tps6598x_read64(tps, TPS_REG_INT_EVENT1, &event1);
416 ret |= tps6598x_read64(tps, TPS_REG_INT_EVENT2, &event2);
418 dev_err(tps->dev, "%s: failed to read events\n", __func__);
421 trace_tps6598x_irq(event1, event2);
423 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
425 dev_err(tps->dev, "%s: failed to read status\n", __func__);
428 trace_tps6598x_status(status);
430 if ((event1 | event2) & TPS_REG_INT_POWER_STATUS_UPDATE) {
431 ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
433 dev_err(tps->dev, "failed to read power status: %d\n", ret);
436 trace_tps6598x_power_status(pwr_status);
439 if ((event1 | event2) & TPS_REG_INT_DATA_STATUS_UPDATE) {
440 ret = tps6598x_read32(tps, TPS_REG_DATA_STATUS, &data_status);
442 dev_err(tps->dev, "failed to read data status: %d\n", ret);
445 trace_tps6598x_data_status(data_status);
448 /* Handle plug insert or removal */
449 if ((event1 | event2) & TPS_REG_INT_PLUG_EVENT) {
450 if (status & TPS_STATUS_PLUG_PRESENT) {
451 ret = tps6598x_connect(tps, status);
454 "failed to register partner\n");
456 tps6598x_disconnect(tps, status);
461 tps6598x_write64(tps, TPS_REG_INT_CLEAR1, event1);
462 tps6598x_write64(tps, TPS_REG_INT_CLEAR2, event2);
465 mutex_unlock(&tps->lock);
470 static int tps6598x_check_mode(struct tps6598x *tps)
475 ret = tps6598x_read32(tps, TPS_REG_MODE, (void *)mode);
479 switch (match_string(modes, ARRAY_SIZE(modes), mode)) {
483 dev_warn(tps->dev, "dead-battery condition\n");
488 dev_err(tps->dev, "controller in unsupported mode \"%s\"\n",
496 static const struct regmap_config tps6598x_regmap_config = {
499 .max_register = 0x7F,
502 static int tps6598x_psy_get_online(struct tps6598x *tps,
503 union power_supply_propval *val)
508 ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
512 if (TPS_POWER_STATUS_CONNECTION(pwr_status) &&
513 TPS_POWER_STATUS_SOURCESINK(pwr_status)) {
521 static int tps6598x_psy_get_prop(struct power_supply *psy,
522 enum power_supply_property psp,
523 union power_supply_propval *val)
525 struct tps6598x *tps = power_supply_get_drvdata(psy);
530 case POWER_SUPPLY_PROP_USB_TYPE:
531 ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
534 if (TPS_POWER_STATUS_PWROPMODE(pwr_status) == TYPEC_PWR_MODE_PD)
535 val->intval = POWER_SUPPLY_USB_TYPE_PD;
537 val->intval = POWER_SUPPLY_USB_TYPE_C;
539 case POWER_SUPPLY_PROP_ONLINE:
540 ret = tps6598x_psy_get_online(tps, val);
550 static int devm_tps6598_psy_register(struct tps6598x *tps)
552 struct power_supply_config psy_cfg = {};
553 const char *port_dev_name = dev_name(tps->dev);
556 psy_cfg.drv_data = tps;
557 psy_cfg.fwnode = dev_fwnode(tps->dev);
559 psy_name = devm_kasprintf(tps->dev, GFP_KERNEL, "%s%s", tps6598x_psy_name_prefix,
564 tps->psy_desc.name = psy_name;
565 tps->psy_desc.type = POWER_SUPPLY_TYPE_USB;
566 tps->psy_desc.usb_types = tps6598x_psy_usb_types;
567 tps->psy_desc.num_usb_types = ARRAY_SIZE(tps6598x_psy_usb_types);
568 tps->psy_desc.properties = tps6598x_psy_props;
569 tps->psy_desc.num_properties = ARRAY_SIZE(tps6598x_psy_props);
570 tps->psy_desc.get_property = tps6598x_psy_get_prop;
572 tps->usb_type = POWER_SUPPLY_USB_TYPE_C;
574 tps->psy = devm_power_supply_register(tps->dev, &tps->psy_desc,
576 return PTR_ERR_OR_ZERO(tps->psy);
579 static int tps6598x_probe(struct i2c_client *client)
581 struct typec_capability typec_cap = { };
582 struct tps6598x *tps;
583 struct fwnode_handle *fwnode;
589 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
593 mutex_init(&tps->lock);
594 tps->dev = &client->dev;
596 tps->regmap = devm_regmap_init_i2c(client, &tps6598x_regmap_config);
597 if (IS_ERR(tps->regmap))
598 return PTR_ERR(tps->regmap);
600 ret = tps6598x_read32(tps, TPS_REG_VID, &vid);
605 * Checking can the adapter handle SMBus protocol. If it can not, the
606 * driver needs to take care of block reads separately.
608 * FIXME: Testing with I2C_FUNC_I2C. regmap-i2c uses I2C protocol
609 * unconditionally if the adapter has I2C_FUNC_I2C set.
611 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
612 tps->i2c_protocol = true;
614 /* Make sure the controller has application firmware running */
615 ret = tps6598x_check_mode(tps);
619 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
622 trace_tps6598x_status(status);
624 ret = tps6598x_read32(tps, TPS_REG_SYSTEM_CONF, &conf);
628 fwnode = device_get_named_child_node(&client->dev, "connector");
632 tps->role_sw = fwnode_usb_role_switch_get(fwnode);
633 if (IS_ERR(tps->role_sw)) {
634 ret = PTR_ERR(tps->role_sw);
638 typec_cap.revision = USB_TYPEC_REV_1_2;
639 typec_cap.pd_revision = 0x200;
640 typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
641 typec_cap.driver_data = tps;
642 typec_cap.ops = &tps6598x_ops;
643 typec_cap.fwnode = fwnode;
645 switch (TPS_SYSCONF_PORTINFO(conf)) {
646 case TPS_PORTINFO_SINK_ACCESSORY:
647 case TPS_PORTINFO_SINK:
648 typec_cap.type = TYPEC_PORT_SNK;
649 typec_cap.data = TYPEC_PORT_UFP;
651 case TPS_PORTINFO_DRP_UFP_DRD:
652 case TPS_PORTINFO_DRP_DFP_DRD:
653 typec_cap.type = TYPEC_PORT_DRP;
654 typec_cap.data = TYPEC_PORT_DRD;
656 case TPS_PORTINFO_DRP_UFP:
657 typec_cap.type = TYPEC_PORT_DRP;
658 typec_cap.data = TYPEC_PORT_UFP;
660 case TPS_PORTINFO_DRP_DFP:
661 typec_cap.type = TYPEC_PORT_DRP;
662 typec_cap.data = TYPEC_PORT_DFP;
664 case TPS_PORTINFO_SOURCE:
665 typec_cap.type = TYPEC_PORT_SRC;
666 typec_cap.data = TYPEC_PORT_DFP;
673 ret = devm_tps6598_psy_register(tps);
677 tps->port = typec_register_port(&client->dev, &typec_cap);
678 if (IS_ERR(tps->port)) {
679 ret = PTR_ERR(tps->port);
682 fwnode_handle_put(fwnode);
684 if (status & TPS_STATUS_PLUG_PRESENT) {
685 ret = tps6598x_connect(tps, status);
687 dev_err(&client->dev, "failed to register partner\n");
690 ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
692 IRQF_SHARED | IRQF_ONESHOT,
693 dev_name(&client->dev), tps);
695 tps6598x_disconnect(tps, 0);
696 typec_unregister_port(tps->port);
700 i2c_set_clientdata(client, tps);
705 usb_role_switch_put(tps->role_sw);
707 fwnode_handle_put(fwnode);
712 static int tps6598x_remove(struct i2c_client *client)
714 struct tps6598x *tps = i2c_get_clientdata(client);
716 tps6598x_disconnect(tps, 0);
717 typec_unregister_port(tps->port);
718 usb_role_switch_put(tps->role_sw);
723 static const struct of_device_id tps6598x_of_match[] = {
724 { .compatible = "ti,tps6598x", },
727 MODULE_DEVICE_TABLE(of, tps6598x_of_match);
729 static const struct i2c_device_id tps6598x_id[] = {
733 MODULE_DEVICE_TABLE(i2c, tps6598x_id);
735 static struct i2c_driver tps6598x_i2c_driver = {
738 .of_match_table = tps6598x_of_match,
740 .probe_new = tps6598x_probe,
741 .remove = tps6598x_remove,
742 .id_table = tps6598x_id,
744 module_i2c_driver(tps6598x_i2c_driver);
746 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
747 MODULE_LICENSE("GPL v2");
748 MODULE_DESCRIPTION("TI TPS6598x USB Power Delivery Controller Driver");