1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for TI TPS6598x USB Power Delivery controller family
5 * Copyright (C) 2017, Intel Corporation
10 #include <linux/acpi.h>
11 #include <linux/module.h>
12 #include <linux/regmap.h>
13 #include <linux/interrupt.h>
14 #include <linux/usb/typec.h>
15 #include <linux/usb/role.h>
17 /* Register offsets */
18 #define TPS_REG_VID 0x00
19 #define TPS_REG_MODE 0x03
20 #define TPS_REG_CMD1 0x08
21 #define TPS_REG_DATA1 0x09
22 #define TPS_REG_INT_EVENT1 0x14
23 #define TPS_REG_INT_EVENT2 0x15
24 #define TPS_REG_INT_MASK1 0x16
25 #define TPS_REG_INT_MASK2 0x17
26 #define TPS_REG_INT_CLEAR1 0x18
27 #define TPS_REG_INT_CLEAR2 0x19
28 #define TPS_REG_STATUS 0x1a
29 #define TPS_REG_SYSTEM_CONF 0x28
30 #define TPS_REG_CTRL_CONF 0x29
31 #define TPS_REG_POWER_STATUS 0x3f
32 #define TPS_REG_RX_IDENTITY_SOP 0x48
34 /* TPS_REG_INT_* bits */
35 #define TPS_REG_INT_PLUG_EVENT BIT(3)
37 /* TPS_REG_STATUS bits */
38 #define TPS_STATUS_PLUG_PRESENT BIT(0)
39 #define TPS_STATUS_ORIENTATION BIT(4)
40 #define TPS_STATUS_PORTROLE(s) (!!((s) & BIT(5)))
41 #define TPS_STATUS_DATAROLE(s) (!!((s) & BIT(6)))
42 #define TPS_STATUS_VCONN(s) (!!((s) & BIT(7)))
44 /* TPS_REG_SYSTEM_CONF bits */
45 #define TPS_SYSCONF_PORTINFO(c) ((c) & 7)
49 TPS_PORTINFO_SINK_ACCESSORY,
51 TPS_PORTINFO_DRP_UFP_DRD,
53 TPS_PORTINFO_DRP_DFP_DRD,
57 /* TPS_REG_POWER_STATUS bits */
58 #define TPS_POWER_STATUS_SOURCESINK BIT(1)
59 #define TPS_POWER_STATUS_PWROPMODE(p) (((p) & GENMASK(3, 2)) >> 2)
61 /* TPS_REG_RX_IDENTITY_SOP */
62 struct tps6598x_rx_identity_reg {
64 struct usb_pd_identity identity;
68 /* Standard Task return codes */
69 #define TPS_TASK_TIMEOUT 1
70 #define TPS_TASK_REJECTED 3
79 static const char *const modes[] = {
80 [TPS_MODE_APP] = "APP ",
81 [TPS_MODE_BOOT] = "BOOT",
82 [TPS_MODE_BIST] = "BIST",
83 [TPS_MODE_DISC] = "DISC",
86 /* Unrecognized commands will be replaced with "!CMD" */
87 #define INVALID_CMD(_cmd_) (_cmd_ == 0x444d4321)
91 struct regmap *regmap;
92 struct mutex lock; /* device lock */
95 struct typec_port *port;
96 struct typec_partner *partner;
97 struct usb_pd_identity partner_identity;
98 struct usb_role_switch *role_sw;
102 * Max data bytes for Data1, Data2, and other registers. See ch 1.3.2:
103 * https://www.ti.com/lit/ug/slvuan1a/slvuan1a.pdf
105 #define TPS_MAX_LEN 64
108 tps6598x_block_read(struct tps6598x *tps, u8 reg, void *val, size_t len)
110 u8 data[TPS_MAX_LEN + 1];
113 if (WARN_ON(len + 1 > sizeof(data)))
116 if (!tps->i2c_protocol)
117 return regmap_raw_read(tps->regmap, reg, val, len);
119 ret = regmap_raw_read(tps->regmap, reg, data, sizeof(data));
126 memcpy(val, &data[1], len);
130 static int tps6598x_block_write(struct tps6598x *tps, u8 reg,
131 const void *val, size_t len)
133 u8 data[TPS_MAX_LEN + 1];
135 if (!tps->i2c_protocol)
136 return regmap_raw_write(tps->regmap, reg, val, len);
139 memcpy(&data[1], val, len);
141 return regmap_raw_write(tps->regmap, reg, data, sizeof(data));
144 static inline int tps6598x_read16(struct tps6598x *tps, u8 reg, u16 *val)
146 return tps6598x_block_read(tps, reg, val, sizeof(u16));
149 static inline int tps6598x_read32(struct tps6598x *tps, u8 reg, u32 *val)
151 return tps6598x_block_read(tps, reg, val, sizeof(u32));
154 static inline int tps6598x_read64(struct tps6598x *tps, u8 reg, u64 *val)
156 return tps6598x_block_read(tps, reg, val, sizeof(u64));
159 static inline int tps6598x_write16(struct tps6598x *tps, u8 reg, u16 val)
161 return tps6598x_block_write(tps, reg, &val, sizeof(u16));
164 static inline int tps6598x_write32(struct tps6598x *tps, u8 reg, u32 val)
166 return tps6598x_block_write(tps, reg, &val, sizeof(u32));
169 static inline int tps6598x_write64(struct tps6598x *tps, u8 reg, u64 val)
171 return tps6598x_block_write(tps, reg, &val, sizeof(u64));
175 tps6598x_write_4cc(struct tps6598x *tps, u8 reg, const char *val)
177 return tps6598x_block_write(tps, reg, val, 4);
180 static int tps6598x_read_partner_identity(struct tps6598x *tps)
182 struct tps6598x_rx_identity_reg id;
185 ret = tps6598x_block_read(tps, TPS_REG_RX_IDENTITY_SOP,
190 tps->partner_identity = id.identity;
195 static void tps6598x_set_data_role(struct tps6598x *tps,
196 enum typec_data_role role, bool connected)
198 enum usb_role role_val;
200 if (role == TYPEC_HOST)
201 role_val = USB_ROLE_HOST;
203 role_val = USB_ROLE_DEVICE;
206 role_val = USB_ROLE_NONE;
208 usb_role_switch_set_role(tps->role_sw, role_val);
209 typec_set_data_role(tps->port, role);
212 static int tps6598x_connect(struct tps6598x *tps, u32 status)
214 struct typec_partner_desc desc;
215 enum typec_pwr_opmode mode;
222 ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
226 mode = TPS_POWER_STATUS_PWROPMODE(pwr_status);
228 desc.usb_pd = mode == TYPEC_PWR_MODE_PD;
229 desc.accessory = TYPEC_ACCESSORY_NONE; /* XXX: handle accessories */
230 desc.identity = NULL;
233 ret = tps6598x_read_partner_identity(tps);
236 desc.identity = &tps->partner_identity;
239 typec_set_pwr_opmode(tps->port, mode);
240 typec_set_pwr_role(tps->port, TPS_STATUS_PORTROLE(status));
241 typec_set_vconn_role(tps->port, TPS_STATUS_VCONN(status));
242 tps6598x_set_data_role(tps, TPS_STATUS_DATAROLE(status), true);
244 tps->partner = typec_register_partner(tps->port, &desc);
245 if (IS_ERR(tps->partner))
246 return PTR_ERR(tps->partner);
249 typec_partner_set_identity(tps->partner);
254 static void tps6598x_disconnect(struct tps6598x *tps, u32 status)
256 if (!IS_ERR(tps->partner))
257 typec_unregister_partner(tps->partner);
259 typec_set_pwr_opmode(tps->port, TYPEC_PWR_MODE_USB);
260 typec_set_pwr_role(tps->port, TPS_STATUS_PORTROLE(status));
261 typec_set_vconn_role(tps->port, TPS_STATUS_VCONN(status));
262 tps6598x_set_data_role(tps, TPS_STATUS_DATAROLE(status), false);
265 static int tps6598x_exec_cmd(struct tps6598x *tps, const char *cmd,
266 size_t in_len, u8 *in_data,
267 size_t out_len, u8 *out_data)
269 unsigned long timeout;
273 ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
276 if (val && !INVALID_CMD(val))
280 ret = tps6598x_block_write(tps, TPS_REG_DATA1,
286 ret = tps6598x_write_4cc(tps, TPS_REG_CMD1, cmd);
290 /* XXX: Using 1s for now, but it may not be enough for every command. */
291 timeout = jiffies + msecs_to_jiffies(1000);
294 ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
297 if (INVALID_CMD(val))
300 if (time_is_before_jiffies(timeout))
305 ret = tps6598x_block_read(tps, TPS_REG_DATA1,
311 ret = tps6598x_block_read(tps, TPS_REG_DATA1, &val, sizeof(u8));
317 case TPS_TASK_TIMEOUT:
319 case TPS_TASK_REJECTED:
328 static int tps6598x_dr_set(struct typec_port *port, enum typec_data_role role)
330 const char *cmd = (role == TYPEC_DEVICE) ? "SWUF" : "SWDF";
331 struct tps6598x *tps = typec_get_drvdata(port);
335 mutex_lock(&tps->lock);
337 ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
341 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
345 if (role != TPS_STATUS_DATAROLE(status)) {
350 tps6598x_set_data_role(tps, role, true);
353 mutex_unlock(&tps->lock);
358 static int tps6598x_pr_set(struct typec_port *port, enum typec_role role)
360 const char *cmd = (role == TYPEC_SINK) ? "SWSk" : "SWSr";
361 struct tps6598x *tps = typec_get_drvdata(port);
365 mutex_lock(&tps->lock);
367 ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
371 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
375 if (role != TPS_STATUS_PORTROLE(status)) {
380 typec_set_pwr_role(tps->port, role);
383 mutex_unlock(&tps->lock);
388 static const struct typec_operations tps6598x_ops = {
389 .dr_set = tps6598x_dr_set,
390 .pr_set = tps6598x_pr_set,
393 static irqreturn_t tps6598x_interrupt(int irq, void *data)
395 struct tps6598x *tps = data;
401 mutex_lock(&tps->lock);
403 ret = tps6598x_read64(tps, TPS_REG_INT_EVENT1, &event1);
404 ret |= tps6598x_read64(tps, TPS_REG_INT_EVENT2, &event2);
406 dev_err(tps->dev, "%s: failed to read events\n", __func__);
410 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
412 dev_err(tps->dev, "%s: failed to read status\n", __func__);
416 /* Handle plug insert or removal */
417 if ((event1 | event2) & TPS_REG_INT_PLUG_EVENT) {
418 if (status & TPS_STATUS_PLUG_PRESENT) {
419 ret = tps6598x_connect(tps, status);
422 "failed to register partner\n");
424 tps6598x_disconnect(tps, status);
429 tps6598x_write64(tps, TPS_REG_INT_CLEAR1, event1);
430 tps6598x_write64(tps, TPS_REG_INT_CLEAR2, event2);
433 mutex_unlock(&tps->lock);
438 static int tps6598x_check_mode(struct tps6598x *tps)
443 ret = tps6598x_read32(tps, TPS_REG_MODE, (void *)mode);
447 switch (match_string(modes, ARRAY_SIZE(modes), mode)) {
451 dev_warn(tps->dev, "dead-battery condition\n");
456 dev_err(tps->dev, "controller in unsupported mode \"%s\"\n",
464 static const struct regmap_config tps6598x_regmap_config = {
467 .max_register = 0x7F,
470 static int tps6598x_probe(struct i2c_client *client)
472 struct typec_capability typec_cap = { };
473 struct tps6598x *tps;
474 struct fwnode_handle *fwnode;
480 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
484 mutex_init(&tps->lock);
485 tps->dev = &client->dev;
487 tps->regmap = devm_regmap_init_i2c(client, &tps6598x_regmap_config);
488 if (IS_ERR(tps->regmap))
489 return PTR_ERR(tps->regmap);
491 ret = tps6598x_read32(tps, TPS_REG_VID, &vid);
496 * Checking can the adapter handle SMBus protocol. If it can not, the
497 * driver needs to take care of block reads separately.
499 * FIXME: Testing with I2C_FUNC_I2C. regmap-i2c uses I2C protocol
500 * unconditionally if the adapter has I2C_FUNC_I2C set.
502 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
503 tps->i2c_protocol = true;
505 /* Make sure the controller has application firmware running */
506 ret = tps6598x_check_mode(tps);
510 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
514 ret = tps6598x_read32(tps, TPS_REG_SYSTEM_CONF, &conf);
518 fwnode = device_get_named_child_node(&client->dev, "connector");
520 return PTR_ERR(fwnode);
522 tps->role_sw = fwnode_usb_role_switch_get(fwnode);
523 if (IS_ERR(tps->role_sw)) {
524 ret = PTR_ERR(tps->role_sw);
528 typec_cap.revision = USB_TYPEC_REV_1_2;
529 typec_cap.pd_revision = 0x200;
530 typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
531 typec_cap.driver_data = tps;
532 typec_cap.ops = &tps6598x_ops;
533 typec_cap.fwnode = fwnode;
535 switch (TPS_SYSCONF_PORTINFO(conf)) {
536 case TPS_PORTINFO_SINK_ACCESSORY:
537 case TPS_PORTINFO_SINK:
538 typec_cap.type = TYPEC_PORT_SNK;
539 typec_cap.data = TYPEC_PORT_UFP;
541 case TPS_PORTINFO_DRP_UFP_DRD:
542 case TPS_PORTINFO_DRP_DFP_DRD:
543 typec_cap.type = TYPEC_PORT_DRP;
544 typec_cap.data = TYPEC_PORT_DRD;
546 case TPS_PORTINFO_DRP_UFP:
547 typec_cap.type = TYPEC_PORT_DRP;
548 typec_cap.data = TYPEC_PORT_UFP;
550 case TPS_PORTINFO_DRP_DFP:
551 typec_cap.type = TYPEC_PORT_DRP;
552 typec_cap.data = TYPEC_PORT_DFP;
554 case TPS_PORTINFO_SOURCE:
555 typec_cap.type = TYPEC_PORT_SRC;
556 typec_cap.data = TYPEC_PORT_DFP;
563 tps->port = typec_register_port(&client->dev, &typec_cap);
564 if (IS_ERR(tps->port)) {
565 ret = PTR_ERR(tps->port);
568 fwnode_handle_put(fwnode);
570 if (status & TPS_STATUS_PLUG_PRESENT) {
571 ret = tps6598x_connect(tps, status);
573 dev_err(&client->dev, "failed to register partner\n");
576 ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
578 IRQF_SHARED | IRQF_ONESHOT,
579 dev_name(&client->dev), tps);
581 tps6598x_disconnect(tps, 0);
582 typec_unregister_port(tps->port);
586 i2c_set_clientdata(client, tps);
591 usb_role_switch_put(tps->role_sw);
593 fwnode_handle_put(fwnode);
598 static int tps6598x_remove(struct i2c_client *client)
600 struct tps6598x *tps = i2c_get_clientdata(client);
602 tps6598x_disconnect(tps, 0);
603 typec_unregister_port(tps->port);
604 usb_role_switch_put(tps->role_sw);
609 static const struct of_device_id tps6598x_of_match[] = {
610 { .compatible = "ti,tps6598x", },
613 MODULE_DEVICE_TABLE(of, tps6598x_of_match);
615 static const struct i2c_device_id tps6598x_id[] = {
619 MODULE_DEVICE_TABLE(i2c, tps6598x_id);
621 static struct i2c_driver tps6598x_i2c_driver = {
624 .of_match_table = tps6598x_of_match,
626 .probe_new = tps6598x_probe,
627 .remove = tps6598x_remove,
628 .id_table = tps6598x_id,
630 module_i2c_driver(tps6598x_i2c_driver);
633 MODULE_LICENSE("GPL v2");
634 MODULE_DESCRIPTION("TI TPS6598x USB Power Delivery Controller Driver");