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>
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) & 3)
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 */
77 struct typec_port *port;
78 struct typec_partner *partner;
79 struct usb_pd_identity partner_identity;
80 struct typec_capability typec_cap;
83 static inline int tps6598x_read16(struct tps6598x *tps, u8 reg, u16 *val)
85 return regmap_raw_read(tps->regmap, reg, val, sizeof(u16));
88 static inline int tps6598x_read32(struct tps6598x *tps, u8 reg, u32 *val)
90 return regmap_raw_read(tps->regmap, reg, val, sizeof(u32));
93 static inline int tps6598x_read64(struct tps6598x *tps, u8 reg, u64 *val)
95 return regmap_raw_read(tps->regmap, reg, val, sizeof(u64));
98 static inline int tps6598x_write16(struct tps6598x *tps, u8 reg, u16 val)
100 return regmap_raw_write(tps->regmap, reg, &val, sizeof(u16));
103 static inline int tps6598x_write32(struct tps6598x *tps, u8 reg, u32 val)
105 return regmap_raw_write(tps->regmap, reg, &val, sizeof(u32));
108 static inline int tps6598x_write64(struct tps6598x *tps, u8 reg, u64 val)
110 return regmap_raw_write(tps->regmap, reg, &val, sizeof(u64));
114 tps6598x_write_4cc(struct tps6598x *tps, u8 reg, const char *val)
116 return regmap_raw_write(tps->regmap, reg, &val, sizeof(u32));
119 static int tps6598x_read_partner_identity(struct tps6598x *tps)
121 struct tps6598x_rx_identity_reg id;
124 ret = regmap_raw_read(tps->regmap, TPS_REG_RX_IDENTITY_SOP,
129 tps->partner_identity = id.identity;
134 static int tps6598x_connect(struct tps6598x *tps, u32 status)
136 struct typec_partner_desc desc;
137 enum typec_pwr_opmode mode;
144 ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
148 mode = TPS_POWER_STATUS_PWROPMODE(pwr_status);
150 desc.usb_pd = mode == TYPEC_PWR_MODE_PD;
151 desc.accessory = TYPEC_ACCESSORY_NONE; /* XXX: handle accessories */
152 desc.identity = NULL;
155 ret = tps6598x_read_partner_identity(tps);
158 desc.identity = &tps->partner_identity;
161 tps->partner = typec_register_partner(tps->port, &desc);
165 typec_set_pwr_opmode(tps->port, mode);
166 typec_set_pwr_role(tps->port, TPS_STATUS_PORTROLE(status));
167 typec_set_vconn_role(tps->port, TPS_STATUS_VCONN(status));
168 typec_set_data_role(tps->port, TPS_STATUS_DATAROLE(status));
171 typec_partner_set_identity(tps->partner);
176 static void tps6598x_disconnect(struct tps6598x *tps, u32 status)
178 typec_unregister_partner(tps->partner);
180 typec_set_pwr_opmode(tps->port, TYPEC_PWR_MODE_USB);
181 typec_set_pwr_role(tps->port, TPS_STATUS_PORTROLE(status));
182 typec_set_vconn_role(tps->port, TPS_STATUS_VCONN(status));
183 typec_set_data_role(tps->port, TPS_STATUS_DATAROLE(status));
186 static int tps6598x_exec_cmd(struct tps6598x *tps, const char *cmd,
187 size_t in_len, u8 *in_data,
188 size_t out_len, u8 *out_data)
190 unsigned long timeout;
194 ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
197 if (val && !INVALID_CMD(val))
201 ret = regmap_raw_write(tps->regmap, TPS_REG_DATA1,
207 ret = tps6598x_write_4cc(tps, TPS_REG_CMD1, cmd);
211 /* XXX: Using 1s for now, but it may not be enough for every command. */
212 timeout = jiffies + msecs_to_jiffies(1000);
215 ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
218 if (INVALID_CMD(val))
221 if (time_is_before_jiffies(timeout))
226 ret = regmap_raw_read(tps->regmap, TPS_REG_DATA1,
232 ret = regmap_read(tps->regmap, TPS_REG_DATA1, &val);
238 case TPS_TASK_TIMEOUT:
240 case TPS_TASK_REJECTED:
250 tps6598x_dr_set(const struct typec_capability *cap, enum typec_data_role role)
252 struct tps6598x *tps = container_of(cap, struct tps6598x, typec_cap);
253 const char *cmd = (role == TYPEC_DEVICE) ? "SWUF" : "SWDF";
257 mutex_lock(&tps->lock);
259 ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
263 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
267 if (role != TPS_STATUS_DATAROLE(status)) {
272 typec_set_data_role(tps->port, role);
275 mutex_unlock(&tps->lock);
281 tps6598x_pr_set(const struct typec_capability *cap, enum typec_role role)
283 struct tps6598x *tps = container_of(cap, struct tps6598x, typec_cap);
284 const char *cmd = (role == TYPEC_SINK) ? "SWSk" : "SWSr";
288 mutex_lock(&tps->lock);
290 ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
294 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
298 if (role != TPS_STATUS_PORTROLE(status)) {
303 typec_set_pwr_role(tps->port, role);
306 mutex_unlock(&tps->lock);
311 static irqreturn_t tps6598x_interrupt(int irq, void *data)
313 struct tps6598x *tps = data;
319 mutex_lock(&tps->lock);
321 ret = tps6598x_read64(tps, TPS_REG_INT_EVENT1, &event1);
322 ret |= tps6598x_read64(tps, TPS_REG_INT_EVENT2, &event2);
324 dev_err(tps->dev, "%s: failed to read events\n", __func__);
328 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
330 dev_err(tps->dev, "%s: failed to read status\n", __func__);
334 /* Handle plug insert or removal */
335 if ((event1 | event2) & TPS_REG_INT_PLUG_EVENT) {
336 if (status & TPS_STATUS_PLUG_PRESENT) {
337 ret = tps6598x_connect(tps, status);
340 "failed to register partner\n");
342 tps6598x_disconnect(tps, status);
347 tps6598x_write64(tps, TPS_REG_INT_CLEAR1, event1);
348 tps6598x_write64(tps, TPS_REG_INT_CLEAR2, event2);
351 mutex_unlock(&tps->lock);
356 static const struct regmap_config tps6598x_regmap_config = {
359 .max_register = 0x7F,
362 static int tps6598x_probe(struct i2c_client *client)
364 struct tps6598x *tps;
370 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
374 mutex_init(&tps->lock);
375 tps->dev = &client->dev;
377 tps->regmap = devm_regmap_init_i2c(client, &tps6598x_regmap_config);
378 if (IS_ERR(tps->regmap))
379 return PTR_ERR(tps->regmap);
381 ret = tps6598x_read32(tps, 0, &vid);
387 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
391 ret = tps6598x_read32(tps, TPS_REG_SYSTEM_CONF, &conf);
395 switch (TPS_SYSCONF_PORTINFO(conf)) {
396 case TPS_PORTINFO_SINK_ACCESSORY:
397 case TPS_PORTINFO_SINK:
398 tps->typec_cap.type = TYPEC_PORT_UFP;
400 case TPS_PORTINFO_DRP_UFP_DRD:
401 case TPS_PORTINFO_DRP_DFP_DRD:
402 tps->typec_cap.dr_set = tps6598x_dr_set;
404 case TPS_PORTINFO_DRP_UFP:
405 case TPS_PORTINFO_DRP_DFP:
406 tps->typec_cap.pr_set = tps6598x_pr_set;
407 tps->typec_cap.type = TYPEC_PORT_DRP;
409 case TPS_PORTINFO_SOURCE:
410 tps->typec_cap.type = TYPEC_PORT_DFP;
416 tps->typec_cap.revision = USB_TYPEC_REV_1_2;
417 tps->typec_cap.pd_revision = 0x200;
418 tps->typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
420 tps->port = typec_register_port(&client->dev, &tps->typec_cap);
424 if (status & TPS_STATUS_PLUG_PRESENT) {
425 ret = tps6598x_connect(tps, status);
427 dev_err(&client->dev, "failed to register partner\n");
430 ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
432 IRQF_SHARED | IRQF_ONESHOT,
433 dev_name(&client->dev), tps);
435 tps6598x_disconnect(tps, 0);
436 typec_unregister_port(tps->port);
440 i2c_set_clientdata(client, tps);
445 static int tps6598x_remove(struct i2c_client *client)
447 struct tps6598x *tps = i2c_get_clientdata(client);
449 tps6598x_disconnect(tps, 0);
450 typec_unregister_port(tps->port);
455 static const struct acpi_device_id tps6598x_acpi_match[] = {
459 MODULE_DEVICE_TABLE(acpi, tps6598x_acpi_match);
461 static struct i2c_driver tps6598x_i2c_driver = {
464 .acpi_match_table = tps6598x_acpi_match,
466 .probe_new = tps6598x_probe,
467 .remove = tps6598x_remove,
469 module_i2c_driver(tps6598x_i2c_driver);
472 MODULE_LICENSE("GPL v2");
473 MODULE_DESCRIPTION("TI TPS6598x USB Power Delivery Controller Driver");