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/power_supply.h>
13 #include <linux/regmap.h>
14 #include <linux/interrupt.h>
15 #include <linux/usb/typec.h>
16 #include <linux/usb/role.h>
18 /* Register offsets */
19 #define TPS_REG_VID 0x00
20 #define TPS_REG_MODE 0x03
21 #define TPS_REG_CMD1 0x08
22 #define TPS_REG_DATA1 0x09
23 #define TPS_REG_INT_EVENT1 0x14
24 #define TPS_REG_INT_EVENT2 0x15
25 #define TPS_REG_INT_MASK1 0x16
26 #define TPS_REG_INT_MASK2 0x17
27 #define TPS_REG_INT_CLEAR1 0x18
28 #define TPS_REG_INT_CLEAR2 0x19
29 #define TPS_REG_STATUS 0x1a
30 #define TPS_REG_SYSTEM_CONF 0x28
31 #define TPS_REG_CTRL_CONF 0x29
32 #define TPS_REG_POWER_STATUS 0x3f
33 #define TPS_REG_RX_IDENTITY_SOP 0x48
35 /* TPS_REG_INT_* bits */
36 #define TPS_REG_INT_PLUG_EVENT BIT(3)
38 /* TPS_REG_STATUS bits */
39 #define TPS_STATUS_PLUG_PRESENT BIT(0)
40 #define TPS_STATUS_ORIENTATION BIT(4)
41 #define TPS_STATUS_PORTROLE(s) (!!((s) & BIT(5)))
42 #define TPS_STATUS_DATAROLE(s) (!!((s) & BIT(6)))
43 #define TPS_STATUS_VCONN(s) (!!((s) & BIT(7)))
45 /* TPS_REG_SYSTEM_CONF bits */
46 #define TPS_SYSCONF_PORTINFO(c) ((c) & 7)
50 TPS_PORTINFO_SINK_ACCESSORY
,
52 TPS_PORTINFO_DRP_UFP_DRD
,
54 TPS_PORTINFO_DRP_DFP_DRD
,
58 /* TPS_REG_POWER_STATUS bits */
59 #define TPS_POWER_STATUS_CONNECTION BIT(0)
60 #define TPS_POWER_STATUS_SOURCESINK BIT(1)
61 #define TPS_POWER_STATUS_PWROPMODE(p) (((p) & GENMASK(3, 2)) >> 2)
63 /* TPS_REG_RX_IDENTITY_SOP */
64 struct tps6598x_rx_identity_reg
{
66 struct usb_pd_identity identity
;
70 /* Standard Task return codes */
71 #define TPS_TASK_TIMEOUT 1
72 #define TPS_TASK_REJECTED 3
81 static const char *const modes
[] = {
82 [TPS_MODE_APP
] = "APP ",
83 [TPS_MODE_BOOT
] = "BOOT",
84 [TPS_MODE_BIST
] = "BIST",
85 [TPS_MODE_DISC
] = "DISC",
88 /* Unrecognized commands will be replaced with "!CMD" */
89 #define INVALID_CMD(_cmd_) (_cmd_ == 0x444d4321)
93 struct regmap
*regmap
;
94 struct mutex lock
; /* device lock */
97 struct typec_port
*port
;
98 struct typec_partner
*partner
;
99 struct usb_pd_identity partner_identity
;
100 struct usb_role_switch
*role_sw
;
101 struct typec_capability typec_cap
;
103 struct power_supply
*psy
;
104 struct power_supply_desc psy_desc
;
105 enum power_supply_usb_type usb_type
;
108 static enum power_supply_property tps6598x_psy_props
[] = {
109 POWER_SUPPLY_PROP_USB_TYPE
,
110 POWER_SUPPLY_PROP_ONLINE
,
113 static enum power_supply_usb_type tps6598x_psy_usb_types
[] = {
114 POWER_SUPPLY_USB_TYPE_C
,
115 POWER_SUPPLY_USB_TYPE_PD
,
118 static const char *tps6598x_psy_name_prefix
= "tps6598x-source-psy-";
121 * Max data bytes for Data1, Data2, and other registers. See ch 1.3.2:
122 * https://www.ti.com/lit/ug/slvuan1a/slvuan1a.pdf
124 #define TPS_MAX_LEN 64
127 tps6598x_block_read(struct tps6598x
*tps
, u8 reg
, void *val
, size_t len
)
129 u8 data
[TPS_MAX_LEN
+ 1];
132 if (WARN_ON(len
+ 1 > sizeof(data
)))
135 if (!tps
->i2c_protocol
)
136 return regmap_raw_read(tps
->regmap
, reg
, val
, len
);
138 ret
= regmap_raw_read(tps
->regmap
, reg
, data
, sizeof(data
));
145 memcpy(val
, &data
[1], len
);
149 static int tps6598x_block_write(struct tps6598x
*tps
, u8 reg
,
150 const void *val
, size_t len
)
152 u8 data
[TPS_MAX_LEN
+ 1];
154 if (!tps
->i2c_protocol
)
155 return regmap_raw_write(tps
->regmap
, reg
, val
, len
);
158 memcpy(&data
[1], val
, len
);
160 return regmap_raw_write(tps
->regmap
, reg
, data
, sizeof(data
));
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
;
241 ret
= tps6598x_read16(tps
, TPS_REG_POWER_STATUS
, &pwr_status
);
245 mode
= TPS_POWER_STATUS_PWROPMODE(pwr_status
);
247 desc
.usb_pd
= mode
== TYPEC_PWR_MODE_PD
;
248 desc
.accessory
= TYPEC_ACCESSORY_NONE
; /* XXX: handle accessories */
249 desc
.identity
= NULL
;
252 ret
= tps6598x_read_partner_identity(tps
);
255 desc
.identity
= &tps
->partner_identity
;
258 typec_set_pwr_opmode(tps
->port
, mode
);
259 typec_set_pwr_role(tps
->port
, TPS_STATUS_PORTROLE(status
));
260 typec_set_vconn_role(tps
->port
, TPS_STATUS_VCONN(status
));
261 tps6598x_set_data_role(tps
, TPS_STATUS_DATAROLE(status
), true);
263 tps
->partner
= typec_register_partner(tps
->port
, &desc
);
264 if (IS_ERR(tps
->partner
))
265 return PTR_ERR(tps
->partner
);
268 typec_partner_set_identity(tps
->partner
);
270 power_supply_changed(tps
->psy
);
275 static void tps6598x_disconnect(struct tps6598x
*tps
, u32 status
)
277 if (!IS_ERR(tps
->partner
))
278 typec_unregister_partner(tps
->partner
);
280 typec_set_pwr_opmode(tps
->port
, TYPEC_PWR_MODE_USB
);
281 typec_set_pwr_role(tps
->port
, TPS_STATUS_PORTROLE(status
));
282 typec_set_vconn_role(tps
->port
, TPS_STATUS_VCONN(status
));
283 tps6598x_set_data_role(tps
, TPS_STATUS_DATAROLE(status
), false);
284 power_supply_changed(tps
->psy
);
287 static int tps6598x_exec_cmd(struct tps6598x
*tps
, const char *cmd
,
288 size_t in_len
, u8
*in_data
,
289 size_t out_len
, u8
*out_data
)
291 unsigned long timeout
;
295 ret
= tps6598x_read32(tps
, TPS_REG_CMD1
, &val
);
298 if (val
&& !INVALID_CMD(val
))
302 ret
= tps6598x_block_write(tps
, TPS_REG_DATA1
,
308 ret
= tps6598x_write_4cc(tps
, TPS_REG_CMD1
, cmd
);
312 /* XXX: Using 1s for now, but it may not be enough for every command. */
313 timeout
= jiffies
+ msecs_to_jiffies(1000);
316 ret
= tps6598x_read32(tps
, TPS_REG_CMD1
, &val
);
319 if (INVALID_CMD(val
))
322 if (time_is_before_jiffies(timeout
))
327 ret
= tps6598x_block_read(tps
, TPS_REG_DATA1
,
333 ret
= tps6598x_block_read(tps
, TPS_REG_DATA1
, &val
, sizeof(u8
));
339 case TPS_TASK_TIMEOUT
:
341 case TPS_TASK_REJECTED
:
350 static int tps6598x_dr_set(struct typec_port
*port
, enum typec_data_role role
)
352 const char *cmd
= (role
== TYPEC_DEVICE
) ? "SWUF" : "SWDF";
353 struct tps6598x
*tps
= typec_get_drvdata(port
);
357 mutex_lock(&tps
->lock
);
359 ret
= tps6598x_exec_cmd(tps
, cmd
, 0, NULL
, 0, NULL
);
363 ret
= tps6598x_read32(tps
, TPS_REG_STATUS
, &status
);
367 if (role
!= TPS_STATUS_DATAROLE(status
)) {
372 tps6598x_set_data_role(tps
, role
, true);
375 mutex_unlock(&tps
->lock
);
380 static int tps6598x_pr_set(struct typec_port
*port
, enum typec_role role
)
382 const char *cmd
= (role
== TYPEC_SINK
) ? "SWSk" : "SWSr";
383 struct tps6598x
*tps
= typec_get_drvdata(port
);
387 mutex_lock(&tps
->lock
);
389 ret
= tps6598x_exec_cmd(tps
, cmd
, 0, NULL
, 0, NULL
);
393 ret
= tps6598x_read32(tps
, TPS_REG_STATUS
, &status
);
397 if (role
!= TPS_STATUS_PORTROLE(status
)) {
402 typec_set_pwr_role(tps
->port
, role
);
405 mutex_unlock(&tps
->lock
);
410 static const struct typec_operations tps6598x_ops
= {
411 .dr_set
= tps6598x_dr_set
,
412 .pr_set
= tps6598x_pr_set
,
415 static irqreturn_t
tps6598x_interrupt(int irq
, void *data
)
417 struct tps6598x
*tps
= data
;
423 mutex_lock(&tps
->lock
);
425 ret
= tps6598x_read64(tps
, TPS_REG_INT_EVENT1
, &event1
);
426 ret
|= tps6598x_read64(tps
, TPS_REG_INT_EVENT2
, &event2
);
428 dev_err(tps
->dev
, "%s: failed to read events\n", __func__
);
432 ret
= tps6598x_read32(tps
, TPS_REG_STATUS
, &status
);
434 dev_err(tps
->dev
, "%s: failed to read status\n", __func__
);
438 /* Handle plug insert or removal */
439 if ((event1
| event2
) & TPS_REG_INT_PLUG_EVENT
) {
440 if (status
& TPS_STATUS_PLUG_PRESENT
) {
441 ret
= tps6598x_connect(tps
, status
);
444 "failed to register partner\n");
446 tps6598x_disconnect(tps
, status
);
451 tps6598x_write64(tps
, TPS_REG_INT_CLEAR1
, event1
);
452 tps6598x_write64(tps
, TPS_REG_INT_CLEAR2
, event2
);
455 mutex_unlock(&tps
->lock
);
460 static int tps6598x_check_mode(struct tps6598x
*tps
)
465 ret
= tps6598x_read32(tps
, TPS_REG_MODE
, (void *)mode
);
469 switch (match_string(modes
, ARRAY_SIZE(modes
), mode
)) {
473 dev_warn(tps
->dev
, "dead-battery condition\n");
478 dev_err(tps
->dev
, "controller in unsupported mode \"%s\"\n",
486 static const struct regmap_config tps6598x_regmap_config
= {
489 .max_register
= 0x7F,
492 static int tps6598x_psy_get_online(struct tps6598x
*tps
,
493 union power_supply_propval
*val
)
498 ret
= tps6598x_read16(tps
, TPS_REG_POWER_STATUS
, &pwr_status
);
502 if ((pwr_status
& TPS_POWER_STATUS_CONNECTION
) &&
503 (pwr_status
& TPS_POWER_STATUS_SOURCESINK
)) {
511 static int tps6598x_psy_get_prop(struct power_supply
*psy
,
512 enum power_supply_property psp
,
513 union power_supply_propval
*val
)
515 struct tps6598x
*tps
= power_supply_get_drvdata(psy
);
520 case POWER_SUPPLY_PROP_USB_TYPE
:
521 ret
= tps6598x_read16(tps
, TPS_REG_POWER_STATUS
, &pwr_status
);
524 if (TPS_POWER_STATUS_PWROPMODE(pwr_status
) == TYPEC_PWR_MODE_PD
)
525 val
->intval
= POWER_SUPPLY_USB_TYPE_PD
;
527 val
->intval
= POWER_SUPPLY_USB_TYPE_C
;
529 case POWER_SUPPLY_PROP_ONLINE
:
530 ret
= tps6598x_psy_get_online(tps
, val
);
540 static int devm_tps6598_psy_register(struct tps6598x
*tps
)
542 struct power_supply_config psy_cfg
= {};
543 const char *port_dev_name
= dev_name(tps
->dev
);
546 psy_cfg
.drv_data
= tps
;
547 psy_cfg
.fwnode
= dev_fwnode(tps
->dev
);
549 psy_name
= devm_kasprintf(tps
->dev
, GFP_KERNEL
, "%s%s", tps6598x_psy_name_prefix
,
554 tps
->psy_desc
.name
= psy_name
;
555 tps
->psy_desc
.type
= POWER_SUPPLY_TYPE_USB
;
556 tps
->psy_desc
.usb_types
= tps6598x_psy_usb_types
;
557 tps
->psy_desc
.num_usb_types
= ARRAY_SIZE(tps6598x_psy_usb_types
);
558 tps
->psy_desc
.properties
= tps6598x_psy_props
;
559 tps
->psy_desc
.num_properties
= ARRAY_SIZE(tps6598x_psy_props
);
560 tps
->psy_desc
.get_property
= tps6598x_psy_get_prop
;
562 tps
->usb_type
= POWER_SUPPLY_USB_TYPE_C
;
564 tps
->psy
= devm_power_supply_register(tps
->dev
, &tps
->psy_desc
,
566 return PTR_ERR_OR_ZERO(tps
->psy
);
569 static int tps6598x_probe(struct i2c_client
*client
)
571 struct typec_capability typec_cap
= { };
572 struct tps6598x
*tps
;
573 struct fwnode_handle
*fwnode
;
579 tps
= devm_kzalloc(&client
->dev
, sizeof(*tps
), GFP_KERNEL
);
583 mutex_init(&tps
->lock
);
584 tps
->dev
= &client
->dev
;
586 tps
->regmap
= devm_regmap_init_i2c(client
, &tps6598x_regmap_config
);
587 if (IS_ERR(tps
->regmap
))
588 return PTR_ERR(tps
->regmap
);
590 ret
= tps6598x_read32(tps
, TPS_REG_VID
, &vid
);
595 * Checking can the adapter handle SMBus protocol. If it can not, the
596 * driver needs to take care of block reads separately.
598 * FIXME: Testing with I2C_FUNC_I2C. regmap-i2c uses I2C protocol
599 * unconditionally if the adapter has I2C_FUNC_I2C set.
601 if (i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
))
602 tps
->i2c_protocol
= true;
604 /* Make sure the controller has application firmware running */
605 ret
= tps6598x_check_mode(tps
);
609 ret
= tps6598x_read32(tps
, TPS_REG_STATUS
, &status
);
613 ret
= tps6598x_read32(tps
, TPS_REG_SYSTEM_CONF
, &conf
);
617 fwnode
= device_get_named_child_node(&client
->dev
, "connector");
619 return PTR_ERR(fwnode
);
621 tps
->role_sw
= fwnode_usb_role_switch_get(fwnode
);
622 if (IS_ERR(tps
->role_sw
)) {
623 ret
= PTR_ERR(tps
->role_sw
);
627 typec_cap
.revision
= USB_TYPEC_REV_1_2
;
628 typec_cap
.pd_revision
= 0x200;
629 typec_cap
.prefer_role
= TYPEC_NO_PREFERRED_ROLE
;
630 typec_cap
.driver_data
= tps
;
631 typec_cap
.ops
= &tps6598x_ops
;
632 typec_cap
.fwnode
= fwnode
;
634 switch (TPS_SYSCONF_PORTINFO(conf
)) {
635 case TPS_PORTINFO_SINK_ACCESSORY
:
636 case TPS_PORTINFO_SINK
:
637 typec_cap
.type
= TYPEC_PORT_SNK
;
638 typec_cap
.data
= TYPEC_PORT_UFP
;
640 case TPS_PORTINFO_DRP_UFP_DRD
:
641 case TPS_PORTINFO_DRP_DFP_DRD
:
642 typec_cap
.type
= TYPEC_PORT_DRP
;
643 typec_cap
.data
= TYPEC_PORT_DRD
;
645 case TPS_PORTINFO_DRP_UFP
:
646 typec_cap
.type
= TYPEC_PORT_DRP
;
647 typec_cap
.data
= TYPEC_PORT_UFP
;
649 case TPS_PORTINFO_DRP_DFP
:
650 typec_cap
.type
= TYPEC_PORT_DRP
;
651 typec_cap
.data
= TYPEC_PORT_DFP
;
653 case TPS_PORTINFO_SOURCE
:
654 typec_cap
.type
= TYPEC_PORT_SRC
;
655 typec_cap
.data
= TYPEC_PORT_DFP
;
662 ret
= devm_tps6598_psy_register(tps
);
666 tps
->port
= typec_register_port(&client
->dev
, &typec_cap
);
667 if (IS_ERR(tps
->port
)) {
668 ret
= PTR_ERR(tps
->port
);
671 fwnode_handle_put(fwnode
);
673 if (status
& TPS_STATUS_PLUG_PRESENT
) {
674 ret
= tps6598x_connect(tps
, status
);
676 dev_err(&client
->dev
, "failed to register partner\n");
679 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
, NULL
,
681 IRQF_SHARED
| IRQF_ONESHOT
,
682 dev_name(&client
->dev
), tps
);
684 tps6598x_disconnect(tps
, 0);
685 typec_unregister_port(tps
->port
);
689 i2c_set_clientdata(client
, tps
);
694 usb_role_switch_put(tps
->role_sw
);
696 fwnode_handle_put(fwnode
);
701 static int tps6598x_remove(struct i2c_client
*client
)
703 struct tps6598x
*tps
= i2c_get_clientdata(client
);
705 tps6598x_disconnect(tps
, 0);
706 typec_unregister_port(tps
->port
);
707 usb_role_switch_put(tps
->role_sw
);
712 static const struct of_device_id tps6598x_of_match
[] = {
713 { .compatible
= "ti,tps6598x", },
716 MODULE_DEVICE_TABLE(of
, tps6598x_of_match
);
718 static const struct i2c_device_id tps6598x_id
[] = {
722 MODULE_DEVICE_TABLE(i2c
, tps6598x_id
);
724 static struct i2c_driver tps6598x_i2c_driver
= {
727 .of_match_table
= tps6598x_of_match
,
729 .probe_new
= tps6598x_probe
,
730 .remove
= tps6598x_remove
,
731 .id_table
= tps6598x_id
,
733 module_i2c_driver(tps6598x_i2c_driver
);
735 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
736 MODULE_LICENSE("GPL v2");
737 MODULE_DESCRIPTION("TI TPS6598x USB Power Delivery Controller Driver");