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/regmap.h>
13 #include <linux/interrupt.h>
14 #include <linux/usb/typec.h>
16 /* Register offsets */
17 #define TPS_REG_CMD1 0x08
18 #define TPS_REG_DATA1 0x09
19 #define TPS_REG_INT_EVENT1 0x14
20 #define TPS_REG_INT_EVENT2 0x15
21 #define TPS_REG_INT_MASK1 0x16
22 #define TPS_REG_INT_MASK2 0x17
23 #define TPS_REG_INT_CLEAR1 0x18
24 #define TPS_REG_INT_CLEAR2 0x19
25 #define TPS_REG_STATUS 0x1a
26 #define TPS_REG_SYSTEM_CONF 0x28
27 #define TPS_REG_CTRL_CONF 0x29
28 #define TPS_REG_POWER_STATUS 0x3f
29 #define TPS_REG_RX_IDENTITY_SOP 0x48
31 /* TPS_REG_INT_* bits */
32 #define TPS_REG_INT_PLUG_EVENT BIT(3)
34 /* TPS_REG_STATUS bits */
35 #define TPS_STATUS_PLUG_PRESENT BIT(0)
36 #define TPS_STATUS_ORIENTATION BIT(4)
37 #define TPS_STATUS_PORTROLE(s) (!!((s) & BIT(5)))
38 #define TPS_STATUS_DATAROLE(s) (!!((s) & BIT(6)))
39 #define TPS_STATUS_VCONN(s) (!!((s) & BIT(7)))
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_POWER_STATUS bits */
55 #define TPS_POWER_STATUS_SOURCESINK BIT(1)
56 #define TPS_POWER_STATUS_PWROPMODE(p) (((p) & GENMASK(3, 2)) >> 2)
58 /* TPS_REG_RX_IDENTITY_SOP */
59 struct tps6598x_rx_identity_reg {
61 struct usb_pd_identity identity;
65 /* Standard Task return codes */
66 #define TPS_TASK_TIMEOUT 1
67 #define TPS_TASK_REJECTED 3
69 /* Unrecognized commands will be replaced with "!CMD" */
70 #define INVALID_CMD(_cmd_) (_cmd_ == 0x444d4321)
74 struct regmap *regmap;
75 struct mutex lock; /* device lock */
78 struct typec_port *port;
79 struct typec_partner *partner;
80 struct usb_pd_identity partner_identity;
81 struct typec_capability typec_cap;
85 * Max data bytes for Data1, Data2, and other registers. See ch 1.3.2:
86 * http://www.ti.com/lit/ug/slvuan1a/slvuan1a.pdf
88 #define TPS_MAX_LEN 64
91 tps6598x_block_read(struct tps6598x *tps, u8 reg, void *val, size_t len)
93 u8 data[TPS_MAX_LEN + 1];
96 if (len + 1 > sizeof(data))
99 if (!tps->i2c_protocol)
100 return regmap_raw_read(tps->regmap, reg, val, len);
102 ret = regmap_raw_read(tps->regmap, reg, data, sizeof(data));
109 memcpy(val, &data[1], len);
113 static int tps6598x_block_write(struct tps6598x *tps, u8 reg,
114 const void *val, size_t len)
116 u8 data[TPS_MAX_LEN + 1];
118 if (!tps->i2c_protocol)
119 return regmap_raw_write(tps->regmap, reg, val, len);
122 memcpy(&data[1], val, len);
124 return regmap_raw_write(tps->regmap, reg, data, sizeof(data));
127 static inline int tps6598x_read16(struct tps6598x *tps, u8 reg, u16 *val)
129 return tps6598x_block_read(tps, reg, val, sizeof(u16));
132 static inline int tps6598x_read32(struct tps6598x *tps, u8 reg, u32 *val)
134 return tps6598x_block_read(tps, reg, val, sizeof(u32));
137 static inline int tps6598x_read64(struct tps6598x *tps, u8 reg, u64 *val)
139 return tps6598x_block_read(tps, reg, val, sizeof(u64));
142 static inline int tps6598x_write16(struct tps6598x *tps, u8 reg, u16 val)
144 return tps6598x_block_write(tps, reg, &val, sizeof(u16));
147 static inline int tps6598x_write32(struct tps6598x *tps, u8 reg, u32 val)
149 return tps6598x_block_write(tps, reg, &val, sizeof(u32));
152 static inline int tps6598x_write64(struct tps6598x *tps, u8 reg, u64 val)
154 return tps6598x_block_write(tps, reg, &val, sizeof(u64));
158 tps6598x_write_4cc(struct tps6598x *tps, u8 reg, const char *val)
160 return tps6598x_block_write(tps, reg, val, 4);
163 static int tps6598x_read_partner_identity(struct tps6598x *tps)
165 struct tps6598x_rx_identity_reg id;
168 ret = tps6598x_block_read(tps, TPS_REG_RX_IDENTITY_SOP,
173 tps->partner_identity = id.identity;
178 static int tps6598x_connect(struct tps6598x *tps, u32 status)
180 struct typec_partner_desc desc;
181 enum typec_pwr_opmode mode;
188 ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
192 mode = TPS_POWER_STATUS_PWROPMODE(pwr_status);
194 desc.usb_pd = mode == TYPEC_PWR_MODE_PD;
195 desc.accessory = TYPEC_ACCESSORY_NONE; /* XXX: handle accessories */
196 desc.identity = NULL;
199 ret = tps6598x_read_partner_identity(tps);
202 desc.identity = &tps->partner_identity;
205 typec_set_pwr_opmode(tps->port, mode);
206 typec_set_pwr_role(tps->port, TPS_STATUS_PORTROLE(status));
207 typec_set_vconn_role(tps->port, TPS_STATUS_VCONN(status));
208 typec_set_data_role(tps->port, TPS_STATUS_DATAROLE(status));
210 tps->partner = typec_register_partner(tps->port, &desc);
211 if (IS_ERR(tps->partner))
212 return PTR_ERR(tps->partner);
215 typec_partner_set_identity(tps->partner);
220 static void tps6598x_disconnect(struct tps6598x *tps, u32 status)
222 if (!IS_ERR(tps->partner))
223 typec_unregister_partner(tps->partner);
225 typec_set_pwr_opmode(tps->port, TYPEC_PWR_MODE_USB);
226 typec_set_pwr_role(tps->port, TPS_STATUS_PORTROLE(status));
227 typec_set_vconn_role(tps->port, TPS_STATUS_VCONN(status));
228 typec_set_data_role(tps->port, TPS_STATUS_DATAROLE(status));
231 static int tps6598x_exec_cmd(struct tps6598x *tps, const char *cmd,
232 size_t in_len, u8 *in_data,
233 size_t out_len, u8 *out_data)
235 unsigned long timeout;
239 ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
242 if (val && !INVALID_CMD(val))
246 ret = tps6598x_block_write(tps, TPS_REG_DATA1,
252 ret = tps6598x_write_4cc(tps, TPS_REG_CMD1, cmd);
256 /* XXX: Using 1s for now, but it may not be enough for every command. */
257 timeout = jiffies + msecs_to_jiffies(1000);
260 ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
263 if (INVALID_CMD(val))
266 if (time_is_before_jiffies(timeout))
271 ret = tps6598x_block_read(tps, TPS_REG_DATA1,
277 ret = tps6598x_block_read(tps, TPS_REG_DATA1, &val, sizeof(u8));
283 case TPS_TASK_TIMEOUT:
285 case TPS_TASK_REJECTED:
295 tps6598x_dr_set(const struct typec_capability *cap, enum typec_data_role role)
297 struct tps6598x *tps = container_of(cap, struct tps6598x, typec_cap);
298 const char *cmd = (role == TYPEC_DEVICE) ? "SWUF" : "SWDF";
302 mutex_lock(&tps->lock);
304 ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
308 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
312 if (role != TPS_STATUS_DATAROLE(status)) {
317 typec_set_data_role(tps->port, role);
320 mutex_unlock(&tps->lock);
326 tps6598x_pr_set(const struct typec_capability *cap, enum typec_role role)
328 struct tps6598x *tps = container_of(cap, struct tps6598x, typec_cap);
329 const char *cmd = (role == TYPEC_SINK) ? "SWSk" : "SWSr";
333 mutex_lock(&tps->lock);
335 ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
339 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
343 if (role != TPS_STATUS_PORTROLE(status)) {
348 typec_set_pwr_role(tps->port, role);
351 mutex_unlock(&tps->lock);
356 static irqreturn_t tps6598x_interrupt(int irq, void *data)
358 struct tps6598x *tps = data;
364 mutex_lock(&tps->lock);
366 ret = tps6598x_read64(tps, TPS_REG_INT_EVENT1, &event1);
367 ret |= tps6598x_read64(tps, TPS_REG_INT_EVENT2, &event2);
369 dev_err(tps->dev, "%s: failed to read events\n", __func__);
373 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
375 dev_err(tps->dev, "%s: failed to read status\n", __func__);
379 /* Handle plug insert or removal */
380 if ((event1 | event2) & TPS_REG_INT_PLUG_EVENT) {
381 if (status & TPS_STATUS_PLUG_PRESENT) {
382 ret = tps6598x_connect(tps, status);
385 "failed to register partner\n");
387 tps6598x_disconnect(tps, status);
392 tps6598x_write64(tps, TPS_REG_INT_CLEAR1, event1);
393 tps6598x_write64(tps, TPS_REG_INT_CLEAR2, event2);
396 mutex_unlock(&tps->lock);
401 static const struct regmap_config tps6598x_regmap_config = {
404 .max_register = 0x7F,
407 static int tps6598x_probe(struct i2c_client *client)
409 struct tps6598x *tps;
415 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
419 mutex_init(&tps->lock);
420 tps->dev = &client->dev;
422 tps->regmap = devm_regmap_init_i2c(client, &tps6598x_regmap_config);
423 if (IS_ERR(tps->regmap))
424 return PTR_ERR(tps->regmap);
426 ret = tps6598x_read32(tps, 0, &vid);
433 * Checking can the adapter handle SMBus protocol. If it can not, the
434 * driver needs to take care of block reads separately.
436 * FIXME: Testing with I2C_FUNC_I2C. regmap-i2c uses I2C protocol
437 * unconditionally if the adapter has I2C_FUNC_I2C set.
439 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
440 tps->i2c_protocol = true;
442 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
446 ret = tps6598x_read32(tps, TPS_REG_SYSTEM_CONF, &conf);
450 tps->typec_cap.revision = USB_TYPEC_REV_1_2;
451 tps->typec_cap.pd_revision = 0x200;
452 tps->typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
453 tps->typec_cap.pr_set = tps6598x_pr_set;
454 tps->typec_cap.dr_set = tps6598x_dr_set;
456 switch (TPS_SYSCONF_PORTINFO(conf)) {
457 case TPS_PORTINFO_SINK_ACCESSORY:
458 case TPS_PORTINFO_SINK:
459 tps->typec_cap.type = TYPEC_PORT_SNK;
460 tps->typec_cap.data = TYPEC_PORT_UFP;
462 case TPS_PORTINFO_DRP_UFP_DRD:
463 case TPS_PORTINFO_DRP_DFP_DRD:
464 tps->typec_cap.type = TYPEC_PORT_DRP;
465 tps->typec_cap.data = TYPEC_PORT_DRD;
467 case TPS_PORTINFO_DRP_UFP:
468 tps->typec_cap.type = TYPEC_PORT_DRP;
469 tps->typec_cap.data = TYPEC_PORT_UFP;
471 case TPS_PORTINFO_DRP_DFP:
472 tps->typec_cap.type = TYPEC_PORT_DRP;
473 tps->typec_cap.data = TYPEC_PORT_DFP;
475 case TPS_PORTINFO_SOURCE:
476 tps->typec_cap.type = TYPEC_PORT_SRC;
477 tps->typec_cap.data = TYPEC_PORT_DFP;
483 tps->port = typec_register_port(&client->dev, &tps->typec_cap);
484 if (IS_ERR(tps->port))
485 return PTR_ERR(tps->port);
487 if (status & TPS_STATUS_PLUG_PRESENT) {
488 ret = tps6598x_connect(tps, status);
490 dev_err(&client->dev, "failed to register partner\n");
493 ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
495 IRQF_SHARED | IRQF_ONESHOT,
496 dev_name(&client->dev), tps);
498 tps6598x_disconnect(tps, 0);
499 typec_unregister_port(tps->port);
503 i2c_set_clientdata(client, tps);
508 static int tps6598x_remove(struct i2c_client *client)
510 struct tps6598x *tps = i2c_get_clientdata(client);
512 tps6598x_disconnect(tps, 0);
513 typec_unregister_port(tps->port);
518 static const struct acpi_device_id tps6598x_acpi_match[] = {
522 MODULE_DEVICE_TABLE(acpi, tps6598x_acpi_match);
524 static struct i2c_driver tps6598x_i2c_driver = {
527 .acpi_match_table = tps6598x_acpi_match,
529 .probe_new = tps6598x_probe,
530 .remove = tps6598x_remove,
532 module_i2c_driver(tps6598x_i2c_driver);
534 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
535 MODULE_LICENSE("GPL v2");
536 MODULE_DESCRIPTION("TI TPS6598x USB Power Delivery Controller Driver");