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_VID 0x00
18 #define TPS_REG_MODE 0x03
19 #define TPS_REG_CMD1 0x08
20 #define TPS_REG_DATA1 0x09
21 #define TPS_REG_INT_EVENT1 0x14
22 #define TPS_REG_INT_EVENT2 0x15
23 #define TPS_REG_INT_MASK1 0x16
24 #define TPS_REG_INT_MASK2 0x17
25 #define TPS_REG_INT_CLEAR1 0x18
26 #define TPS_REG_INT_CLEAR2 0x19
27 #define TPS_REG_STATUS 0x1a
28 #define TPS_REG_SYSTEM_CONF 0x28
29 #define TPS_REG_CTRL_CONF 0x29
30 #define TPS_REG_POWER_STATUS 0x3f
31 #define TPS_REG_RX_IDENTITY_SOP 0x48
33 /* TPS_REG_INT_* bits */
34 #define TPS_REG_INT_PLUG_EVENT BIT(3)
36 /* TPS_REG_STATUS bits */
37 #define TPS_STATUS_PLUG_PRESENT BIT(0)
38 #define TPS_STATUS_ORIENTATION BIT(4)
39 #define TPS_STATUS_PORTROLE(s) (!!((s) & BIT(5)))
40 #define TPS_STATUS_DATAROLE(s) (!!((s) & BIT(6)))
41 #define TPS_STATUS_VCONN(s) (!!((s) & BIT(7)))
43 /* TPS_REG_SYSTEM_CONF bits */
44 #define TPS_SYSCONF_PORTINFO(c) ((c) & 7)
48 TPS_PORTINFO_SINK_ACCESSORY
,
50 TPS_PORTINFO_DRP_UFP_DRD
,
52 TPS_PORTINFO_DRP_DFP_DRD
,
56 /* TPS_REG_POWER_STATUS bits */
57 #define TPS_POWER_STATUS_SOURCESINK BIT(1)
58 #define TPS_POWER_STATUS_PWROPMODE(p) (((p) & GENMASK(3, 2)) >> 2)
60 /* TPS_REG_RX_IDENTITY_SOP */
61 struct tps6598x_rx_identity_reg
{
63 struct usb_pd_identity identity
;
67 /* Standard Task return codes */
68 #define TPS_TASK_TIMEOUT 1
69 #define TPS_TASK_REJECTED 3
78 static const char *const modes
[] = {
79 [TPS_MODE_APP
] = "APP ",
80 [TPS_MODE_BOOT
] = "BOOT",
81 [TPS_MODE_BIST
] = "BIST",
82 [TPS_MODE_DISC
] = "DISC",
85 /* Unrecognized commands will be replaced with "!CMD" */
86 #define INVALID_CMD(_cmd_) (_cmd_ == 0x444d4321)
90 struct regmap
*regmap
;
91 struct mutex lock
; /* device lock */
94 struct typec_port
*port
;
95 struct typec_partner
*partner
;
96 struct usb_pd_identity partner_identity
;
97 struct typec_capability typec_cap
;
101 * Max data bytes for Data1, Data2, and other registers. See ch 1.3.2:
102 * http://www.ti.com/lit/ug/slvuan1a/slvuan1a.pdf
104 #define TPS_MAX_LEN 64
107 tps6598x_block_read(struct tps6598x
*tps
, u8 reg
, void *val
, size_t len
)
109 u8 data
[TPS_MAX_LEN
+ 1];
112 if (WARN_ON(len
+ 1 > sizeof(data
)))
115 if (!tps
->i2c_protocol
)
116 return regmap_raw_read(tps
->regmap
, reg
, val
, len
);
118 ret
= regmap_raw_read(tps
->regmap
, reg
, data
, sizeof(data
));
125 memcpy(val
, &data
[1], len
);
129 static int tps6598x_block_write(struct tps6598x
*tps
, u8 reg
,
130 const void *val
, size_t len
)
132 u8 data
[TPS_MAX_LEN
+ 1];
134 if (!tps
->i2c_protocol
)
135 return regmap_raw_write(tps
->regmap
, reg
, val
, len
);
138 memcpy(&data
[1], val
, len
);
140 return regmap_raw_write(tps
->regmap
, reg
, data
, sizeof(data
));
143 static inline int tps6598x_read16(struct tps6598x
*tps
, u8 reg
, u16
*val
)
145 return tps6598x_block_read(tps
, reg
, val
, sizeof(u16
));
148 static inline int tps6598x_read32(struct tps6598x
*tps
, u8 reg
, u32
*val
)
150 return tps6598x_block_read(tps
, reg
, val
, sizeof(u32
));
153 static inline int tps6598x_read64(struct tps6598x
*tps
, u8 reg
, u64
*val
)
155 return tps6598x_block_read(tps
, reg
, val
, sizeof(u64
));
158 static inline int tps6598x_write16(struct tps6598x
*tps
, u8 reg
, u16 val
)
160 return tps6598x_block_write(tps
, reg
, &val
, sizeof(u16
));
163 static inline int tps6598x_write32(struct tps6598x
*tps
, u8 reg
, u32 val
)
165 return tps6598x_block_write(tps
, reg
, &val
, sizeof(u32
));
168 static inline int tps6598x_write64(struct tps6598x
*tps
, u8 reg
, u64 val
)
170 return tps6598x_block_write(tps
, reg
, &val
, sizeof(u64
));
174 tps6598x_write_4cc(struct tps6598x
*tps
, u8 reg
, const char *val
)
176 return tps6598x_block_write(tps
, reg
, val
, 4);
179 static int tps6598x_read_partner_identity(struct tps6598x
*tps
)
181 struct tps6598x_rx_identity_reg id
;
184 ret
= tps6598x_block_read(tps
, TPS_REG_RX_IDENTITY_SOP
,
189 tps
->partner_identity
= id
.identity
;
194 static int tps6598x_connect(struct tps6598x
*tps
, u32 status
)
196 struct typec_partner_desc desc
;
197 enum typec_pwr_opmode mode
;
204 ret
= tps6598x_read16(tps
, TPS_REG_POWER_STATUS
, &pwr_status
);
208 mode
= TPS_POWER_STATUS_PWROPMODE(pwr_status
);
210 desc
.usb_pd
= mode
== TYPEC_PWR_MODE_PD
;
211 desc
.accessory
= TYPEC_ACCESSORY_NONE
; /* XXX: handle accessories */
212 desc
.identity
= NULL
;
215 ret
= tps6598x_read_partner_identity(tps
);
218 desc
.identity
= &tps
->partner_identity
;
221 typec_set_pwr_opmode(tps
->port
, mode
);
222 typec_set_pwr_role(tps
->port
, TPS_STATUS_PORTROLE(status
));
223 typec_set_vconn_role(tps
->port
, TPS_STATUS_VCONN(status
));
224 typec_set_data_role(tps
->port
, TPS_STATUS_DATAROLE(status
));
226 tps
->partner
= typec_register_partner(tps
->port
, &desc
);
227 if (IS_ERR(tps
->partner
))
228 return PTR_ERR(tps
->partner
);
231 typec_partner_set_identity(tps
->partner
);
236 static void tps6598x_disconnect(struct tps6598x
*tps
, u32 status
)
238 if (!IS_ERR(tps
->partner
))
239 typec_unregister_partner(tps
->partner
);
241 typec_set_pwr_opmode(tps
->port
, TYPEC_PWR_MODE_USB
);
242 typec_set_pwr_role(tps
->port
, TPS_STATUS_PORTROLE(status
));
243 typec_set_vconn_role(tps
->port
, TPS_STATUS_VCONN(status
));
244 typec_set_data_role(tps
->port
, TPS_STATUS_DATAROLE(status
));
247 static int tps6598x_exec_cmd(struct tps6598x
*tps
, const char *cmd
,
248 size_t in_len
, u8
*in_data
,
249 size_t out_len
, u8
*out_data
)
251 unsigned long timeout
;
255 ret
= tps6598x_read32(tps
, TPS_REG_CMD1
, &val
);
258 if (val
&& !INVALID_CMD(val
))
262 ret
= tps6598x_block_write(tps
, TPS_REG_DATA1
,
268 ret
= tps6598x_write_4cc(tps
, TPS_REG_CMD1
, cmd
);
272 /* XXX: Using 1s for now, but it may not be enough for every command. */
273 timeout
= jiffies
+ msecs_to_jiffies(1000);
276 ret
= tps6598x_read32(tps
, TPS_REG_CMD1
, &val
);
279 if (INVALID_CMD(val
))
282 if (time_is_before_jiffies(timeout
))
287 ret
= tps6598x_block_read(tps
, TPS_REG_DATA1
,
293 ret
= tps6598x_block_read(tps
, TPS_REG_DATA1
, &val
, sizeof(u8
));
299 case TPS_TASK_TIMEOUT
:
301 case TPS_TASK_REJECTED
:
311 tps6598x_dr_set(const struct typec_capability
*cap
, enum typec_data_role role
)
313 struct tps6598x
*tps
= container_of(cap
, struct tps6598x
, typec_cap
);
314 const char *cmd
= (role
== TYPEC_DEVICE
) ? "SWUF" : "SWDF";
318 mutex_lock(&tps
->lock
);
320 ret
= tps6598x_exec_cmd(tps
, cmd
, 0, NULL
, 0, NULL
);
324 ret
= tps6598x_read32(tps
, TPS_REG_STATUS
, &status
);
328 if (role
!= TPS_STATUS_DATAROLE(status
)) {
333 typec_set_data_role(tps
->port
, role
);
336 mutex_unlock(&tps
->lock
);
342 tps6598x_pr_set(const struct typec_capability
*cap
, enum typec_role role
)
344 struct tps6598x
*tps
= container_of(cap
, struct tps6598x
, typec_cap
);
345 const char *cmd
= (role
== TYPEC_SINK
) ? "SWSk" : "SWSr";
349 mutex_lock(&tps
->lock
);
351 ret
= tps6598x_exec_cmd(tps
, cmd
, 0, NULL
, 0, NULL
);
355 ret
= tps6598x_read32(tps
, TPS_REG_STATUS
, &status
);
359 if (role
!= TPS_STATUS_PORTROLE(status
)) {
364 typec_set_pwr_role(tps
->port
, role
);
367 mutex_unlock(&tps
->lock
);
372 static irqreturn_t
tps6598x_interrupt(int irq
, void *data
)
374 struct tps6598x
*tps
= data
;
380 mutex_lock(&tps
->lock
);
382 ret
= tps6598x_read64(tps
, TPS_REG_INT_EVENT1
, &event1
);
383 ret
|= tps6598x_read64(tps
, TPS_REG_INT_EVENT2
, &event2
);
385 dev_err(tps
->dev
, "%s: failed to read events\n", __func__
);
389 ret
= tps6598x_read32(tps
, TPS_REG_STATUS
, &status
);
391 dev_err(tps
->dev
, "%s: failed to read status\n", __func__
);
395 /* Handle plug insert or removal */
396 if ((event1
| event2
) & TPS_REG_INT_PLUG_EVENT
) {
397 if (status
& TPS_STATUS_PLUG_PRESENT
) {
398 ret
= tps6598x_connect(tps
, status
);
401 "failed to register partner\n");
403 tps6598x_disconnect(tps
, status
);
408 tps6598x_write64(tps
, TPS_REG_INT_CLEAR1
, event1
);
409 tps6598x_write64(tps
, TPS_REG_INT_CLEAR2
, event2
);
412 mutex_unlock(&tps
->lock
);
417 static int tps6598x_check_mode(struct tps6598x
*tps
)
422 ret
= tps6598x_read32(tps
, TPS_REG_MODE
, (void *)mode
);
426 switch (match_string(modes
, ARRAY_SIZE(modes
), mode
)) {
430 dev_warn(tps
->dev
, "dead-battery condition\n");
435 dev_err(tps
->dev
, "controller in unsupported mode \"%s\"\n",
443 static const struct regmap_config tps6598x_regmap_config
= {
446 .max_register
= 0x7F,
449 static int tps6598x_probe(struct i2c_client
*client
)
451 struct tps6598x
*tps
;
457 tps
= devm_kzalloc(&client
->dev
, sizeof(*tps
), GFP_KERNEL
);
461 mutex_init(&tps
->lock
);
462 tps
->dev
= &client
->dev
;
464 tps
->regmap
= devm_regmap_init_i2c(client
, &tps6598x_regmap_config
);
465 if (IS_ERR(tps
->regmap
))
466 return PTR_ERR(tps
->regmap
);
468 ret
= tps6598x_read32(tps
, TPS_REG_VID
, &vid
);
473 * Checking can the adapter handle SMBus protocol. If it can not, the
474 * driver needs to take care of block reads separately.
476 * FIXME: Testing with I2C_FUNC_I2C. regmap-i2c uses I2C protocol
477 * unconditionally if the adapter has I2C_FUNC_I2C set.
479 if (i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
))
480 tps
->i2c_protocol
= true;
482 /* Make sure the controller has application firmware running */
483 ret
= tps6598x_check_mode(tps
);
487 ret
= tps6598x_read32(tps
, TPS_REG_STATUS
, &status
);
491 ret
= tps6598x_read32(tps
, TPS_REG_SYSTEM_CONF
, &conf
);
495 tps
->typec_cap
.revision
= USB_TYPEC_REV_1_2
;
496 tps
->typec_cap
.pd_revision
= 0x200;
497 tps
->typec_cap
.prefer_role
= TYPEC_NO_PREFERRED_ROLE
;
498 tps
->typec_cap
.pr_set
= tps6598x_pr_set
;
499 tps
->typec_cap
.dr_set
= tps6598x_dr_set
;
501 switch (TPS_SYSCONF_PORTINFO(conf
)) {
502 case TPS_PORTINFO_SINK_ACCESSORY
:
503 case TPS_PORTINFO_SINK
:
504 tps
->typec_cap
.type
= TYPEC_PORT_SNK
;
505 tps
->typec_cap
.data
= TYPEC_PORT_UFP
;
507 case TPS_PORTINFO_DRP_UFP_DRD
:
508 case TPS_PORTINFO_DRP_DFP_DRD
:
509 tps
->typec_cap
.type
= TYPEC_PORT_DRP
;
510 tps
->typec_cap
.data
= TYPEC_PORT_DRD
;
512 case TPS_PORTINFO_DRP_UFP
:
513 tps
->typec_cap
.type
= TYPEC_PORT_DRP
;
514 tps
->typec_cap
.data
= TYPEC_PORT_UFP
;
516 case TPS_PORTINFO_DRP_DFP
:
517 tps
->typec_cap
.type
= TYPEC_PORT_DRP
;
518 tps
->typec_cap
.data
= TYPEC_PORT_DFP
;
520 case TPS_PORTINFO_SOURCE
:
521 tps
->typec_cap
.type
= TYPEC_PORT_SRC
;
522 tps
->typec_cap
.data
= TYPEC_PORT_DFP
;
528 tps
->port
= typec_register_port(&client
->dev
, &tps
->typec_cap
);
529 if (IS_ERR(tps
->port
))
530 return PTR_ERR(tps
->port
);
532 if (status
& TPS_STATUS_PLUG_PRESENT
) {
533 ret
= tps6598x_connect(tps
, status
);
535 dev_err(&client
->dev
, "failed to register partner\n");
538 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
, NULL
,
540 IRQF_SHARED
| IRQF_ONESHOT
,
541 dev_name(&client
->dev
), tps
);
543 tps6598x_disconnect(tps
, 0);
544 typec_unregister_port(tps
->port
);
548 i2c_set_clientdata(client
, tps
);
553 static int tps6598x_remove(struct i2c_client
*client
)
555 struct tps6598x
*tps
= i2c_get_clientdata(client
);
557 tps6598x_disconnect(tps
, 0);
558 typec_unregister_port(tps
->port
);
563 static const struct i2c_device_id tps6598x_id
[] = {
567 MODULE_DEVICE_TABLE(i2c
, tps6598x_id
);
569 static struct i2c_driver tps6598x_i2c_driver
= {
573 .probe_new
= tps6598x_probe
,
574 .remove
= tps6598x_remove
,
575 .id_table
= tps6598x_id
,
577 module_i2c_driver(tps6598x_i2c_driver
);
579 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
580 MODULE_LICENSE("GPL v2");
581 MODULE_DESCRIPTION("TI TPS6598x USB Power Delivery Controller Driver");