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>
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>
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
41 /* TPS_REG_SYSTEM_CONF bits */
42 #define TPS_SYSCONF_PORTINFO(c) ((c) & 7)
46 TPS_PORTINFO_SINK_ACCESSORY,
48 TPS_PORTINFO_DRP_UFP_DRD,
50 TPS_PORTINFO_DRP_DFP_DRD,
54 /* TPS_REG_RX_IDENTITY_SOP */
55 struct tps6598x_rx_identity_reg {
57 struct usb_pd_identity identity;
60 /* Standard Task return codes */
61 #define TPS_TASK_TIMEOUT 1
62 #define TPS_TASK_REJECTED 3
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",
78 /* Unrecognized commands will be replaced with "!CMD" */
79 #define INVALID_CMD(_cmd_) (_cmd_ == 0x444d4321)
83 struct regmap *regmap;
84 struct mutex lock; /* device lock */
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;
93 struct power_supply *psy;
94 struct power_supply_desc psy_desc;
95 enum power_supply_usb_type usb_type;
100 static enum power_supply_property tps6598x_psy_props[] = {
101 POWER_SUPPLY_PROP_USB_TYPE,
102 POWER_SUPPLY_PROP_ONLINE,
105 static enum power_supply_usb_type tps6598x_psy_usb_types[] = {
106 POWER_SUPPLY_USB_TYPE_C,
107 POWER_SUPPLY_USB_TYPE_PD,
110 static const char *tps6598x_psy_name_prefix = "tps6598x-source-psy-";
113 * Max data bytes for Data1, Data2, and other registers. See ch 1.3.2:
114 * https://www.ti.com/lit/ug/slvuan1a/slvuan1a.pdf
116 #define TPS_MAX_LEN 64
119 tps6598x_block_read(struct tps6598x *tps, u8 reg, void *val, size_t len)
121 u8 data[TPS_MAX_LEN + 1];
124 if (len + 1 > sizeof(data))
127 if (!tps->i2c_protocol)
128 return regmap_raw_read(tps->regmap, reg, val, len);
130 ret = regmap_raw_read(tps->regmap, reg, data, len + 1);
137 memcpy(val, &data[1], len);
141 static int tps6598x_block_write(struct tps6598x *tps, u8 reg,
142 const void *val, size_t len)
144 u8 data[TPS_MAX_LEN + 1];
146 if (len + 1 > sizeof(data))
149 if (!tps->i2c_protocol)
150 return regmap_raw_write(tps->regmap, reg, val, len);
153 memcpy(&data[1], val, len);
155 return regmap_raw_write(tps->regmap, reg, data, len + 1);
158 static inline int tps6598x_read8(struct tps6598x *tps, u8 reg, u8 *val)
160 return tps6598x_block_read(tps, reg, val, sizeof(u8));
163 static inline int tps6598x_read16(struct tps6598x *tps, u8 reg, u16 *val)
165 return tps6598x_block_read(tps, reg, val, sizeof(u16));
168 static inline int tps6598x_read32(struct tps6598x *tps, u8 reg, u32 *val)
170 return tps6598x_block_read(tps, reg, val, sizeof(u32));
173 static inline int tps6598x_read64(struct tps6598x *tps, u8 reg, u64 *val)
175 return tps6598x_block_read(tps, reg, val, sizeof(u64));
178 static inline int tps6598x_write16(struct tps6598x *tps, u8 reg, u16 val)
180 return tps6598x_block_write(tps, reg, &val, sizeof(u16));
183 static inline int tps6598x_write32(struct tps6598x *tps, u8 reg, u32 val)
185 return tps6598x_block_write(tps, reg, &val, sizeof(u32));
188 static inline int tps6598x_write64(struct tps6598x *tps, u8 reg, u64 val)
190 return tps6598x_block_write(tps, reg, &val, sizeof(u64));
194 tps6598x_write_4cc(struct tps6598x *tps, u8 reg, const char *val)
196 return tps6598x_block_write(tps, reg, val, 4);
199 static int tps6598x_read_partner_identity(struct tps6598x *tps)
201 struct tps6598x_rx_identity_reg id;
204 ret = tps6598x_block_read(tps, TPS_REG_RX_IDENTITY_SOP,
209 tps->partner_identity = id.identity;
214 static void tps6598x_set_data_role(struct tps6598x *tps,
215 enum typec_data_role role, bool connected)
217 enum usb_role role_val;
219 if (role == TYPEC_HOST)
220 role_val = USB_ROLE_HOST;
222 role_val = USB_ROLE_DEVICE;
225 role_val = USB_ROLE_NONE;
227 usb_role_switch_set_role(tps->role_sw, role_val);
228 typec_set_data_role(tps->port, role);
231 static int tps6598x_connect(struct tps6598x *tps, u32 status)
233 struct typec_partner_desc desc;
234 enum typec_pwr_opmode mode;
240 mode = TPS_POWER_STATUS_PWROPMODE(tps->pwr_status);
242 desc.usb_pd = mode == TYPEC_PWR_MODE_PD;
243 desc.accessory = TYPEC_ACCESSORY_NONE; /* XXX: handle accessories */
244 desc.identity = NULL;
247 ret = tps6598x_read_partner_identity(tps);
250 desc.identity = &tps->partner_identity;
253 typec_set_pwr_opmode(tps->port, mode);
254 typec_set_pwr_role(tps->port, TPS_STATUS_TO_TYPEC_PORTROLE(status));
255 typec_set_vconn_role(tps->port, TPS_STATUS_TO_TYPEC_VCONN(status));
256 if (TPS_STATUS_TO_UPSIDE_DOWN(status))
257 typec_set_orientation(tps->port, TYPEC_ORIENTATION_REVERSE);
259 typec_set_orientation(tps->port, TYPEC_ORIENTATION_NORMAL);
260 tps6598x_set_data_role(tps, TPS_STATUS_TO_TYPEC_DATAROLE(status), true);
262 tps->partner = typec_register_partner(tps->port, &desc);
263 if (IS_ERR(tps->partner))
264 return PTR_ERR(tps->partner);
267 typec_partner_set_identity(tps->partner);
269 power_supply_changed(tps->psy);
274 static void tps6598x_disconnect(struct tps6598x *tps, u32 status)
276 if (!IS_ERR(tps->partner))
277 typec_unregister_partner(tps->partner);
279 typec_set_pwr_opmode(tps->port, TYPEC_PWR_MODE_USB);
280 typec_set_pwr_role(tps->port, TPS_STATUS_TO_TYPEC_PORTROLE(status));
281 typec_set_vconn_role(tps->port, TPS_STATUS_TO_TYPEC_VCONN(status));
282 typec_set_orientation(tps->port, TYPEC_ORIENTATION_NONE);
283 tps6598x_set_data_role(tps, TPS_STATUS_TO_TYPEC_DATAROLE(status), false);
285 power_supply_changed(tps->psy);
288 static int tps6598x_exec_cmd(struct tps6598x *tps, const char *cmd,
289 size_t in_len, u8 *in_data,
290 size_t out_len, u8 *out_data)
292 unsigned long timeout;
296 ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
299 if (val && !INVALID_CMD(val))
303 ret = tps6598x_block_write(tps, TPS_REG_DATA1,
309 ret = tps6598x_write_4cc(tps, TPS_REG_CMD1, cmd);
313 /* XXX: Using 1s for now, but it may not be enough for every command. */
314 timeout = jiffies + msecs_to_jiffies(1000);
317 ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
320 if (INVALID_CMD(val))
323 if (time_is_before_jiffies(timeout))
328 ret = tps6598x_block_read(tps, TPS_REG_DATA1,
334 ret = tps6598x_block_read(tps, TPS_REG_DATA1, &val, sizeof(u8));
340 case TPS_TASK_TIMEOUT:
342 case TPS_TASK_REJECTED:
351 static int tps6598x_dr_set(struct typec_port *port, enum typec_data_role role)
353 const char *cmd = (role == TYPEC_DEVICE) ? "SWUF" : "SWDF";
354 struct tps6598x *tps = typec_get_drvdata(port);
358 mutex_lock(&tps->lock);
360 ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
364 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
368 if (role != TPS_STATUS_TO_TYPEC_DATAROLE(status)) {
373 tps6598x_set_data_role(tps, role, true);
376 mutex_unlock(&tps->lock);
381 static int tps6598x_pr_set(struct typec_port *port, enum typec_role role)
383 const char *cmd = (role == TYPEC_SINK) ? "SWSk" : "SWSr";
384 struct tps6598x *tps = typec_get_drvdata(port);
388 mutex_lock(&tps->lock);
390 ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
394 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
398 if (role != TPS_STATUS_TO_TYPEC_PORTROLE(status)) {
403 typec_set_pwr_role(tps->port, role);
406 mutex_unlock(&tps->lock);
411 static const struct typec_operations tps6598x_ops = {
412 .dr_set = tps6598x_dr_set,
413 .pr_set = tps6598x_pr_set,
416 static bool tps6598x_read_status(struct tps6598x *tps, u32 *status)
420 ret = tps6598x_read32(tps, TPS_REG_STATUS, status);
422 dev_err(tps->dev, "%s: failed to read status\n", __func__);
425 trace_tps6598x_status(*status);
430 static bool tps6598x_read_data_status(struct tps6598x *tps)
435 ret = tps6598x_read32(tps, TPS_REG_DATA_STATUS, &data_status);
437 dev_err(tps->dev, "failed to read data status: %d\n", ret);
440 trace_tps6598x_data_status(data_status);
445 static bool tps6598x_read_power_status(struct tps6598x *tps)
450 ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
452 dev_err(tps->dev, "failed to read power status: %d\n", ret);
455 tps->pwr_status = pwr_status;
456 trace_tps6598x_power_status(pwr_status);
461 static void tps6598x_handle_plug_event(struct tps6598x *tps, u32 status)
465 if (status & TPS_STATUS_PLUG_PRESENT) {
466 ret = tps6598x_connect(tps, status);
468 dev_err(tps->dev, "failed to register partner\n");
470 tps6598x_disconnect(tps, status);
474 static irqreturn_t cd321x_interrupt(int irq, void *data)
476 struct tps6598x *tps = data;
481 mutex_lock(&tps->lock);
483 ret = tps6598x_read64(tps, TPS_REG_INT_EVENT1, &event);
485 dev_err(tps->dev, "%s: failed to read events\n", __func__);
488 trace_cd321x_irq(event);
493 if (!tps6598x_read_status(tps, &status))
496 if (event & APPLE_CD_REG_INT_POWER_STATUS_UPDATE)
497 if (!tps6598x_read_power_status(tps))
500 if (event & APPLE_CD_REG_INT_DATA_STATUS_UPDATE)
501 if (!tps6598x_read_data_status(tps))
504 /* Handle plug insert or removal */
505 if (event & APPLE_CD_REG_INT_PLUG_EVENT)
506 tps6598x_handle_plug_event(tps, status);
509 tps6598x_write64(tps, TPS_REG_INT_CLEAR1, event);
512 mutex_unlock(&tps->lock);
519 static irqreturn_t tps6598x_interrupt(int irq, void *data)
521 struct tps6598x *tps = data;
527 mutex_lock(&tps->lock);
529 ret = tps6598x_read64(tps, TPS_REG_INT_EVENT1, &event1);
530 ret |= tps6598x_read64(tps, TPS_REG_INT_EVENT2, &event2);
532 dev_err(tps->dev, "%s: failed to read events\n", __func__);
535 trace_tps6598x_irq(event1, event2);
537 if (!(event1 | event2))
540 if (!tps6598x_read_status(tps, &status))
543 if ((event1 | event2) & TPS_REG_INT_POWER_STATUS_UPDATE)
544 if (!tps6598x_read_power_status(tps))
547 if ((event1 | event2) & TPS_REG_INT_DATA_STATUS_UPDATE)
548 if (!tps6598x_read_data_status(tps))
551 /* Handle plug insert or removal */
552 if ((event1 | event2) & TPS_REG_INT_PLUG_EVENT)
553 tps6598x_handle_plug_event(tps, status);
556 tps6598x_write64(tps, TPS_REG_INT_CLEAR1, event1);
557 tps6598x_write64(tps, TPS_REG_INT_CLEAR2, event2);
560 mutex_unlock(&tps->lock);
567 static int tps6598x_check_mode(struct tps6598x *tps)
572 ret = tps6598x_read32(tps, TPS_REG_MODE, (void *)mode);
576 switch (match_string(modes, ARRAY_SIZE(modes), mode)) {
580 dev_warn(tps->dev, "dead-battery condition\n");
585 dev_err(tps->dev, "controller in unsupported mode \"%s\"\n",
593 static const struct regmap_config tps6598x_regmap_config = {
596 .max_register = 0x7F,
599 static int tps6598x_psy_get_online(struct tps6598x *tps,
600 union power_supply_propval *val)
602 if (TPS_POWER_STATUS_CONNECTION(tps->pwr_status) &&
603 TPS_POWER_STATUS_SOURCESINK(tps->pwr_status)) {
611 static int tps6598x_psy_get_prop(struct power_supply *psy,
612 enum power_supply_property psp,
613 union power_supply_propval *val)
615 struct tps6598x *tps = power_supply_get_drvdata(psy);
619 case POWER_SUPPLY_PROP_USB_TYPE:
620 if (TPS_POWER_STATUS_PWROPMODE(tps->pwr_status) == TYPEC_PWR_MODE_PD)
621 val->intval = POWER_SUPPLY_USB_TYPE_PD;
623 val->intval = POWER_SUPPLY_USB_TYPE_C;
625 case POWER_SUPPLY_PROP_ONLINE:
626 ret = tps6598x_psy_get_online(tps, val);
636 static int cd321x_switch_power_state(struct tps6598x *tps, u8 target_state)
641 ret = tps6598x_read8(tps, TPS_REG_SYSTEM_POWER_STATE, &state);
645 if (state == target_state)
648 ret = tps6598x_exec_cmd(tps, "SSPS", sizeof(u8), &target_state, 0, NULL);
652 ret = tps6598x_read8(tps, TPS_REG_SYSTEM_POWER_STATE, &state);
656 if (state != target_state)
662 static int devm_tps6598_psy_register(struct tps6598x *tps)
664 struct power_supply_config psy_cfg = {};
665 const char *port_dev_name = dev_name(tps->dev);
668 psy_cfg.drv_data = tps;
669 psy_cfg.fwnode = dev_fwnode(tps->dev);
671 psy_name = devm_kasprintf(tps->dev, GFP_KERNEL, "%s%s", tps6598x_psy_name_prefix,
676 tps->psy_desc.name = psy_name;
677 tps->psy_desc.type = POWER_SUPPLY_TYPE_USB;
678 tps->psy_desc.usb_types = tps6598x_psy_usb_types;
679 tps->psy_desc.num_usb_types = ARRAY_SIZE(tps6598x_psy_usb_types);
680 tps->psy_desc.properties = tps6598x_psy_props;
681 tps->psy_desc.num_properties = ARRAY_SIZE(tps6598x_psy_props);
682 tps->psy_desc.get_property = tps6598x_psy_get_prop;
684 tps->usb_type = POWER_SUPPLY_USB_TYPE_C;
686 tps->psy = devm_power_supply_register(tps->dev, &tps->psy_desc,
688 return PTR_ERR_OR_ZERO(tps->psy);
691 static int tps6598x_probe(struct i2c_client *client)
693 irq_handler_t irq_handler = tps6598x_interrupt;
694 struct device_node *np = client->dev.of_node;
695 struct typec_capability typec_cap = { };
696 struct tps6598x *tps;
697 struct fwnode_handle *fwnode;
704 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
708 mutex_init(&tps->lock);
709 tps->dev = &client->dev;
711 tps->regmap = devm_regmap_init_i2c(client, &tps6598x_regmap_config);
712 if (IS_ERR(tps->regmap))
713 return PTR_ERR(tps->regmap);
715 ret = tps6598x_read32(tps, TPS_REG_VID, &vid);
720 * Checking can the adapter handle SMBus protocol. If it can not, the
721 * driver needs to take care of block reads separately.
723 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
724 tps->i2c_protocol = true;
726 if (np && of_device_is_compatible(np, "apple,cd321x")) {
727 /* Switch CD321X chips to the correct system power state */
728 ret = cd321x_switch_power_state(tps, TPS_SYSTEM_POWER_STATE_S0);
732 /* CD321X chips have all interrupts masked initially */
733 mask1 = APPLE_CD_REG_INT_POWER_STATUS_UPDATE |
734 APPLE_CD_REG_INT_DATA_STATUS_UPDATE |
735 APPLE_CD_REG_INT_PLUG_EVENT;
737 irq_handler = cd321x_interrupt;
739 /* Enable power status, data status and plug event interrupts */
740 mask1 = TPS_REG_INT_POWER_STATUS_UPDATE |
741 TPS_REG_INT_DATA_STATUS_UPDATE |
742 TPS_REG_INT_PLUG_EVENT;
745 /* Make sure the controller has application firmware running */
746 ret = tps6598x_check_mode(tps);
750 ret = tps6598x_write64(tps, TPS_REG_INT_MASK1, mask1);
754 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
757 trace_tps6598x_status(status);
759 ret = tps6598x_read32(tps, TPS_REG_SYSTEM_CONF, &conf);
764 * This fwnode has a "compatible" property, but is never populated as a
765 * struct device. Instead we simply parse it to read the properties.
766 * This breaks fw_devlink=on. To maintain backward compatibility
767 * with existing DT files, we work around this by deleting any
768 * fwnode_links to/from this fwnode.
770 fwnode = device_get_named_child_node(&client->dev, "connector");
772 fw_devlink_purge_absent_suppliers(fwnode);
774 tps->role_sw = fwnode_usb_role_switch_get(fwnode);
775 if (IS_ERR(tps->role_sw)) {
776 ret = PTR_ERR(tps->role_sw);
780 typec_cap.revision = USB_TYPEC_REV_1_2;
781 typec_cap.pd_revision = 0x200;
782 typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
783 typec_cap.driver_data = tps;
784 typec_cap.ops = &tps6598x_ops;
785 typec_cap.fwnode = fwnode;
787 switch (TPS_SYSCONF_PORTINFO(conf)) {
788 case TPS_PORTINFO_SINK_ACCESSORY:
789 case TPS_PORTINFO_SINK:
790 typec_cap.type = TYPEC_PORT_SNK;
791 typec_cap.data = TYPEC_PORT_UFP;
793 case TPS_PORTINFO_DRP_UFP_DRD:
794 case TPS_PORTINFO_DRP_DFP_DRD:
795 typec_cap.type = TYPEC_PORT_DRP;
796 typec_cap.data = TYPEC_PORT_DRD;
798 case TPS_PORTINFO_DRP_UFP:
799 typec_cap.type = TYPEC_PORT_DRP;
800 typec_cap.data = TYPEC_PORT_UFP;
802 case TPS_PORTINFO_DRP_DFP:
803 typec_cap.type = TYPEC_PORT_DRP;
804 typec_cap.data = TYPEC_PORT_DFP;
806 case TPS_PORTINFO_SOURCE:
807 typec_cap.type = TYPEC_PORT_SRC;
808 typec_cap.data = TYPEC_PORT_DFP;
815 ret = devm_tps6598_psy_register(tps);
819 tps->port = typec_register_port(&client->dev, &typec_cap);
820 if (IS_ERR(tps->port)) {
821 ret = PTR_ERR(tps->port);
825 if (status & TPS_STATUS_PLUG_PRESENT) {
826 ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &tps->pwr_status);
828 dev_err(tps->dev, "failed to read power status: %d\n", ret);
831 ret = tps6598x_connect(tps, status);
833 dev_err(&client->dev, "failed to register partner\n");
836 ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
838 IRQF_SHARED | IRQF_ONESHOT,
839 dev_name(&client->dev), tps);
841 tps6598x_disconnect(tps, 0);
842 typec_unregister_port(tps->port);
846 i2c_set_clientdata(client, tps);
847 fwnode_handle_put(fwnode);
852 usb_role_switch_put(tps->role_sw);
854 fwnode_handle_put(fwnode);
856 tps6598x_write64(tps, TPS_REG_INT_MASK1, 0);
860 static void tps6598x_remove(struct i2c_client *client)
862 struct tps6598x *tps = i2c_get_clientdata(client);
864 tps6598x_disconnect(tps, 0);
865 typec_unregister_port(tps->port);
866 usb_role_switch_put(tps->role_sw);
869 static const struct of_device_id tps6598x_of_match[] = {
870 { .compatible = "ti,tps6598x", },
871 { .compatible = "apple,cd321x", },
874 MODULE_DEVICE_TABLE(of, tps6598x_of_match);
876 static const struct i2c_device_id tps6598x_id[] = {
880 MODULE_DEVICE_TABLE(i2c, tps6598x_id);
882 static struct i2c_driver tps6598x_i2c_driver = {
885 .of_match_table = tps6598x_of_match,
887 .probe_new = tps6598x_probe,
888 .remove = tps6598x_remove,
889 .id_table = tps6598x_id,
891 module_i2c_driver(tps6598x_i2c_driver);
893 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
894 MODULE_LICENSE("GPL v2");
895 MODULE_DESCRIPTION("TI TPS6598x USB Power Delivery Controller Driver");