2 * USB Type-C Connector Class
4 * Copyright (C) 2017, Intel Corporation
5 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/device.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/slab.h>
16 #include <linux/usb/typec.h>
22 enum typec_port_type roles
;
24 struct typec_altmode
*alt_mode
;
26 unsigned int active
:1;
29 struct attribute_group group
;
30 struct attribute
*attrs
[5];
31 struct device_attribute vdo_attr
;
32 struct device_attribute desc_attr
;
33 struct device_attribute active_attr
;
34 struct device_attribute roles_attr
;
37 struct typec_altmode
{
41 struct typec_mode modes
[ALTMODE_MAX_MODES
];
42 const struct attribute_group
*mode_groups
[ALTMODE_MAX_MODES
];
47 enum typec_plug_index index
;
52 enum typec_plug_type type
;
53 struct usb_pd_identity
*identity
;
54 unsigned int active
:1;
57 struct typec_partner
{
59 unsigned int usb_pd
:1;
60 struct usb_pd_identity
*identity
;
61 enum typec_accessory accessory
;
69 enum typec_data_role data_role
;
70 enum typec_role pwr_role
;
71 enum typec_role vconn_role
;
72 enum typec_pwr_opmode pwr_opmode
;
73 enum typec_port_type port_type
;
74 struct mutex port_type_lock
;
76 const struct typec_capability
*cap
;
79 #define to_typec_port(_dev_) container_of(_dev_, struct typec_port, dev)
80 #define to_typec_plug(_dev_) container_of(_dev_, struct typec_plug, dev)
81 #define to_typec_cable(_dev_) container_of(_dev_, struct typec_cable, dev)
82 #define to_typec_partner(_dev_) container_of(_dev_, struct typec_partner, dev)
83 #define to_altmode(_dev_) container_of(_dev_, struct typec_altmode, dev)
85 static const struct device_type typec_partner_dev_type
;
86 static const struct device_type typec_cable_dev_type
;
87 static const struct device_type typec_plug_dev_type
;
88 static const struct device_type typec_port_dev_type
;
90 #define is_typec_partner(_dev_) (_dev_->type == &typec_partner_dev_type)
91 #define is_typec_cable(_dev_) (_dev_->type == &typec_cable_dev_type)
92 #define is_typec_plug(_dev_) (_dev_->type == &typec_plug_dev_type)
93 #define is_typec_port(_dev_) (_dev_->type == &typec_port_dev_type)
95 static DEFINE_IDA(typec_index_ida
);
96 static struct class *typec_class
;
98 /* Common attributes */
100 static const char * const typec_accessory_modes
[] = {
101 [TYPEC_ACCESSORY_NONE
] = "none",
102 [TYPEC_ACCESSORY_AUDIO
] = "analog_audio",
103 [TYPEC_ACCESSORY_DEBUG
] = "debug",
106 static struct usb_pd_identity
*get_pd_identity(struct device
*dev
)
108 if (is_typec_partner(dev
)) {
109 struct typec_partner
*partner
= to_typec_partner(dev
);
111 return partner
->identity
;
112 } else if (is_typec_cable(dev
)) {
113 struct typec_cable
*cable
= to_typec_cable(dev
);
115 return cable
->identity
;
120 static ssize_t
id_header_show(struct device
*dev
, struct device_attribute
*attr
,
123 struct usb_pd_identity
*id
= get_pd_identity(dev
);
125 return sprintf(buf
, "0x%08x\n", id
->id_header
);
127 static DEVICE_ATTR_RO(id_header
);
129 static ssize_t
cert_stat_show(struct device
*dev
, struct device_attribute
*attr
,
132 struct usb_pd_identity
*id
= get_pd_identity(dev
);
134 return sprintf(buf
, "0x%08x\n", id
->cert_stat
);
136 static DEVICE_ATTR_RO(cert_stat
);
138 static ssize_t
product_show(struct device
*dev
, struct device_attribute
*attr
,
141 struct usb_pd_identity
*id
= get_pd_identity(dev
);
143 return sprintf(buf
, "0x%08x\n", id
->product
);
145 static DEVICE_ATTR_RO(product
);
147 static struct attribute
*usb_pd_id_attrs
[] = {
148 &dev_attr_id_header
.attr
,
149 &dev_attr_cert_stat
.attr
,
150 &dev_attr_product
.attr
,
154 static const struct attribute_group usb_pd_id_group
= {
156 .attrs
= usb_pd_id_attrs
,
159 static const struct attribute_group
*usb_pd_id_groups
[] = {
164 static void typec_report_identity(struct device
*dev
)
166 sysfs_notify(&dev
->kobj
, "identity", "id_header");
167 sysfs_notify(&dev
->kobj
, "identity", "cert_stat");
168 sysfs_notify(&dev
->kobj
, "identity", "product");
171 /* ------------------------------------------------------------------------- */
172 /* Alternate Modes */
175 * typec_altmode_update_active - Report Enter/Exit mode
176 * @alt: Handle to the alternate mode
178 * @active: True when the mode has been entered
180 * If a partner or cable plug executes Enter/Exit Mode command successfully, the
181 * drivers use this routine to report the updated state of the mode.
183 void typec_altmode_update_active(struct typec_altmode
*alt
, int mode
,
186 struct typec_mode
*m
= &alt
->modes
[mode
];
189 if (m
->active
== active
)
193 snprintf(dir
, sizeof(dir
), "mode%d", mode
);
194 sysfs_notify(&alt
->dev
.kobj
, dir
, "active");
195 kobject_uevent(&alt
->dev
.kobj
, KOBJ_CHANGE
);
197 EXPORT_SYMBOL_GPL(typec_altmode_update_active
);
200 * typec_altmode2port - Alternate Mode to USB Type-C port
201 * @alt: The Alternate Mode
203 * Returns handle to the port that a cable plug or partner with @alt is
206 struct typec_port
*typec_altmode2port(struct typec_altmode
*alt
)
208 if (is_typec_plug(alt
->dev
.parent
))
209 return to_typec_port(alt
->dev
.parent
->parent
->parent
);
210 if (is_typec_partner(alt
->dev
.parent
))
211 return to_typec_port(alt
->dev
.parent
->parent
);
212 if (is_typec_port(alt
->dev
.parent
))
213 return to_typec_port(alt
->dev
.parent
);
217 EXPORT_SYMBOL_GPL(typec_altmode2port
);
220 typec_altmode_vdo_show(struct device
*dev
, struct device_attribute
*attr
,
223 struct typec_mode
*mode
= container_of(attr
, struct typec_mode
,
226 return sprintf(buf
, "0x%08x\n", mode
->vdo
);
230 typec_altmode_desc_show(struct device
*dev
, struct device_attribute
*attr
,
233 struct typec_mode
*mode
= container_of(attr
, struct typec_mode
,
236 return sprintf(buf
, "%s\n", mode
->desc
? mode
->desc
: "");
240 typec_altmode_active_show(struct device
*dev
, struct device_attribute
*attr
,
243 struct typec_mode
*mode
= container_of(attr
, struct typec_mode
,
246 return sprintf(buf
, "%s\n", mode
->active
? "yes" : "no");
250 typec_altmode_active_store(struct device
*dev
, struct device_attribute
*attr
,
251 const char *buf
, size_t size
)
253 struct typec_mode
*mode
= container_of(attr
, struct typec_mode
,
255 struct typec_port
*port
= typec_altmode2port(mode
->alt_mode
);
259 if (!port
->cap
->activate_mode
)
262 ret
= kstrtobool(buf
, &activate
);
266 ret
= port
->cap
->activate_mode(port
->cap
, mode
->index
, activate
);
274 typec_altmode_roles_show(struct device
*dev
, struct device_attribute
*attr
,
277 struct typec_mode
*mode
= container_of(attr
, struct typec_mode
,
281 switch (mode
->roles
) {
283 ret
= sprintf(buf
, "source\n");
286 ret
= sprintf(buf
, "sink\n");
290 ret
= sprintf(buf
, "source sink\n");
296 static void typec_init_modes(struct typec_altmode
*alt
,
297 const struct typec_mode_desc
*desc
, bool is_port
)
301 for (i
= 0; i
< alt
->n_modes
; i
++, desc
++) {
302 struct typec_mode
*mode
= &alt
->modes
[i
];
304 /* Not considering the human readable description critical */
305 mode
->desc
= kstrdup(desc
->desc
, GFP_KERNEL
);
306 if (desc
->desc
&& !mode
->desc
)
307 dev_err(&alt
->dev
, "failed to copy mode%d desc\n", i
);
309 mode
->alt_mode
= alt
;
310 mode
->vdo
= desc
->vdo
;
311 mode
->roles
= desc
->roles
;
312 mode
->index
= desc
->index
;
313 sprintf(mode
->group_name
, "mode%d", desc
->index
);
315 sysfs_attr_init(&mode
->vdo_attr
.attr
);
316 mode
->vdo_attr
.attr
.name
= "vdo";
317 mode
->vdo_attr
.attr
.mode
= 0444;
318 mode
->vdo_attr
.show
= typec_altmode_vdo_show
;
320 sysfs_attr_init(&mode
->desc_attr
.attr
);
321 mode
->desc_attr
.attr
.name
= "description";
322 mode
->desc_attr
.attr
.mode
= 0444;
323 mode
->desc_attr
.show
= typec_altmode_desc_show
;
325 sysfs_attr_init(&mode
->active_attr
.attr
);
326 mode
->active_attr
.attr
.name
= "active";
327 mode
->active_attr
.attr
.mode
= 0644;
328 mode
->active_attr
.show
= typec_altmode_active_show
;
329 mode
->active_attr
.store
= typec_altmode_active_store
;
331 mode
->attrs
[0] = &mode
->vdo_attr
.attr
;
332 mode
->attrs
[1] = &mode
->desc_attr
.attr
;
333 mode
->attrs
[2] = &mode
->active_attr
.attr
;
335 /* With ports, list the roles that the mode is supported with */
337 sysfs_attr_init(&mode
->roles_attr
.attr
);
338 mode
->roles_attr
.attr
.name
= "supported_roles";
339 mode
->roles_attr
.attr
.mode
= 0444;
340 mode
->roles_attr
.show
= typec_altmode_roles_show
;
342 mode
->attrs
[3] = &mode
->roles_attr
.attr
;
345 mode
->group
.attrs
= mode
->attrs
;
346 mode
->group
.name
= mode
->group_name
;
348 alt
->mode_groups
[i
] = &mode
->group
;
352 static ssize_t
svid_show(struct device
*dev
, struct device_attribute
*attr
,
355 struct typec_altmode
*alt
= to_altmode(dev
);
357 return sprintf(buf
, "%04x\n", alt
->svid
);
359 static DEVICE_ATTR_RO(svid
);
361 static struct attribute
*typec_altmode_attrs
[] = {
365 ATTRIBUTE_GROUPS(typec_altmode
);
367 static void typec_altmode_release(struct device
*dev
)
369 struct typec_altmode
*alt
= to_altmode(dev
);
372 for (i
= 0; i
< alt
->n_modes
; i
++)
373 kfree(alt
->modes
[i
].desc
);
377 static const struct device_type typec_altmode_dev_type
= {
378 .name
= "typec_alternate_mode",
379 .groups
= typec_altmode_groups
,
380 .release
= typec_altmode_release
,
383 static struct typec_altmode
*
384 typec_register_altmode(struct device
*parent
,
385 const struct typec_altmode_desc
*desc
)
387 struct typec_altmode
*alt
;
390 alt
= kzalloc(sizeof(*alt
), GFP_KERNEL
);
394 alt
->svid
= desc
->svid
;
395 alt
->n_modes
= desc
->n_modes
;
396 typec_init_modes(alt
, desc
->modes
, is_typec_port(parent
));
398 alt
->dev
.parent
= parent
;
399 alt
->dev
.groups
= alt
->mode_groups
;
400 alt
->dev
.type
= &typec_altmode_dev_type
;
401 dev_set_name(&alt
->dev
, "svid-%04x", alt
->svid
);
403 ret
= device_register(&alt
->dev
);
405 dev_err(parent
, "failed to register alternate mode (%d)\n",
407 put_device(&alt
->dev
);
415 * typec_unregister_altmode - Unregister Alternate Mode
416 * @alt: The alternate mode to be unregistered
418 * Unregister device created with typec_partner_register_altmode(),
419 * typec_plug_register_altmode() or typec_port_register_altmode().
421 void typec_unregister_altmode(struct typec_altmode
*alt
)
424 device_unregister(&alt
->dev
);
426 EXPORT_SYMBOL_GPL(typec_unregister_altmode
);
428 /* ------------------------------------------------------------------------- */
429 /* Type-C Partners */
431 static ssize_t
accessory_mode_show(struct device
*dev
,
432 struct device_attribute
*attr
,
435 struct typec_partner
*p
= to_typec_partner(dev
);
437 return sprintf(buf
, "%s\n", typec_accessory_modes
[p
->accessory
]);
439 static DEVICE_ATTR_RO(accessory_mode
);
441 static ssize_t
supports_usb_power_delivery_show(struct device
*dev
,
442 struct device_attribute
*attr
,
445 struct typec_partner
*p
= to_typec_partner(dev
);
447 return sprintf(buf
, "%s\n", p
->usb_pd
? "yes" : "no");
449 static DEVICE_ATTR_RO(supports_usb_power_delivery
);
451 static struct attribute
*typec_partner_attrs
[] = {
452 &dev_attr_accessory_mode
.attr
,
453 &dev_attr_supports_usb_power_delivery
.attr
,
456 ATTRIBUTE_GROUPS(typec_partner
);
458 static void typec_partner_release(struct device
*dev
)
460 struct typec_partner
*partner
= to_typec_partner(dev
);
465 static const struct device_type typec_partner_dev_type
= {
466 .name
= "typec_partner",
467 .groups
= typec_partner_groups
,
468 .release
= typec_partner_release
,
472 * typec_partner_set_identity - Report result from Discover Identity command
473 * @partner: The partner updated identity values
475 * This routine is used to report that the result of Discover Identity USB power
476 * delivery command has become available.
478 int typec_partner_set_identity(struct typec_partner
*partner
)
480 if (!partner
->identity
)
483 typec_report_identity(&partner
->dev
);
486 EXPORT_SYMBOL_GPL(typec_partner_set_identity
);
489 * typec_partner_register_altmode - Register USB Type-C Partner Alternate Mode
490 * @partner: USB Type-C Partner that supports the alternate mode
491 * @desc: Description of the alternate mode
493 * This routine is used to register each alternate mode individually that
494 * @partner has listed in response to Discover SVIDs command. The modes for a
495 * SVID listed in response to Discover Modes command need to be listed in an
498 * Returns handle to the alternate mode on success or NULL on failure.
500 struct typec_altmode
*
501 typec_partner_register_altmode(struct typec_partner
*partner
,
502 const struct typec_altmode_desc
*desc
)
504 return typec_register_altmode(&partner
->dev
, desc
);
506 EXPORT_SYMBOL_GPL(typec_partner_register_altmode
);
509 * typec_register_partner - Register a USB Type-C Partner
510 * @port: The USB Type-C Port the partner is connected to
511 * @desc: Description of the partner
513 * Registers a device for USB Type-C Partner described in @desc.
515 * Returns handle to the partner on success or NULL on failure.
517 struct typec_partner
*typec_register_partner(struct typec_port
*port
,
518 struct typec_partner_desc
*desc
)
520 struct typec_partner
*partner
;
523 partner
= kzalloc(sizeof(*partner
), GFP_KERNEL
);
527 partner
->usb_pd
= desc
->usb_pd
;
528 partner
->accessory
= desc
->accessory
;
530 if (desc
->identity
) {
532 * Creating directory for the identity only if the driver is
533 * able to provide data to it.
535 partner
->dev
.groups
= usb_pd_id_groups
;
536 partner
->identity
= desc
->identity
;
539 partner
->dev
.class = typec_class
;
540 partner
->dev
.parent
= &port
->dev
;
541 partner
->dev
.type
= &typec_partner_dev_type
;
542 dev_set_name(&partner
->dev
, "%s-partner", dev_name(&port
->dev
));
544 ret
= device_register(&partner
->dev
);
546 dev_err(&port
->dev
, "failed to register partner (%d)\n", ret
);
547 put_device(&partner
->dev
);
553 EXPORT_SYMBOL_GPL(typec_register_partner
);
556 * typec_unregister_partner - Unregister a USB Type-C Partner
557 * @partner: The partner to be unregistered
559 * Unregister device created with typec_register_partner().
561 void typec_unregister_partner(struct typec_partner
*partner
)
564 device_unregister(&partner
->dev
);
566 EXPORT_SYMBOL_GPL(typec_unregister_partner
);
568 /* ------------------------------------------------------------------------- */
569 /* Type-C Cable Plugs */
571 static void typec_plug_release(struct device
*dev
)
573 struct typec_plug
*plug
= to_typec_plug(dev
);
578 static const struct device_type typec_plug_dev_type
= {
579 .name
= "typec_plug",
580 .release
= typec_plug_release
,
584 * typec_plug_register_altmode - Register USB Type-C Cable Plug Alternate Mode
585 * @plug: USB Type-C Cable Plug that supports the alternate mode
586 * @desc: Description of the alternate mode
588 * This routine is used to register each alternate mode individually that @plug
589 * has listed in response to Discover SVIDs command. The modes for a SVID that
590 * the plug lists in response to Discover Modes command need to be listed in an
593 * Returns handle to the alternate mode on success or NULL on failure.
595 struct typec_altmode
*
596 typec_plug_register_altmode(struct typec_plug
*plug
,
597 const struct typec_altmode_desc
*desc
)
599 return typec_register_altmode(&plug
->dev
, desc
);
601 EXPORT_SYMBOL_GPL(typec_plug_register_altmode
);
604 * typec_register_plug - Register a USB Type-C Cable Plug
605 * @cable: USB Type-C Cable with the plug
606 * @desc: Description of the cable plug
608 * Registers a device for USB Type-C Cable Plug described in @desc. A USB Type-C
609 * Cable Plug represents a plug with electronics in it that can response to USB
610 * Power Delivery SOP Prime or SOP Double Prime packages.
612 * Returns handle to the cable plug on success or NULL on failure.
614 struct typec_plug
*typec_register_plug(struct typec_cable
*cable
,
615 struct typec_plug_desc
*desc
)
617 struct typec_plug
*plug
;
621 plug
= kzalloc(sizeof(*plug
), GFP_KERNEL
);
625 sprintf(name
, "plug%d", desc
->index
);
627 plug
->index
= desc
->index
;
628 plug
->dev
.class = typec_class
;
629 plug
->dev
.parent
= &cable
->dev
;
630 plug
->dev
.type
= &typec_plug_dev_type
;
631 dev_set_name(&plug
->dev
, "%s-%s", dev_name(cable
->dev
.parent
), name
);
633 ret
= device_register(&plug
->dev
);
635 dev_err(&cable
->dev
, "failed to register plug (%d)\n", ret
);
636 put_device(&plug
->dev
);
642 EXPORT_SYMBOL_GPL(typec_register_plug
);
645 * typec_unregister_plug - Unregister a USB Type-C Cable Plug
646 * @plug: The cable plug to be unregistered
648 * Unregister device created with typec_register_plug().
650 void typec_unregister_plug(struct typec_plug
*plug
)
653 device_unregister(&plug
->dev
);
655 EXPORT_SYMBOL_GPL(typec_unregister_plug
);
660 type_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
662 struct typec_cable
*cable
= to_typec_cable(dev
);
664 return sprintf(buf
, "%s\n", cable
->active
? "active" : "passive");
666 static DEVICE_ATTR_RO(type
);
668 static const char * const typec_plug_types
[] = {
669 [USB_PLUG_NONE
] = "unknown",
670 [USB_PLUG_TYPE_A
] = "type-a",
671 [USB_PLUG_TYPE_B
] = "type-b",
672 [USB_PLUG_TYPE_C
] = "type-c",
673 [USB_PLUG_CAPTIVE
] = "captive",
676 static ssize_t
plug_type_show(struct device
*dev
,
677 struct device_attribute
*attr
, char *buf
)
679 struct typec_cable
*cable
= to_typec_cable(dev
);
681 return sprintf(buf
, "%s\n", typec_plug_types
[cable
->type
]);
683 static DEVICE_ATTR_RO(plug_type
);
685 static struct attribute
*typec_cable_attrs
[] = {
687 &dev_attr_plug_type
.attr
,
690 ATTRIBUTE_GROUPS(typec_cable
);
692 static void typec_cable_release(struct device
*dev
)
694 struct typec_cable
*cable
= to_typec_cable(dev
);
699 static const struct device_type typec_cable_dev_type
= {
700 .name
= "typec_cable",
701 .groups
= typec_cable_groups
,
702 .release
= typec_cable_release
,
706 * typec_cable_set_identity - Report result from Discover Identity command
707 * @cable: The cable updated identity values
709 * This routine is used to report that the result of Discover Identity USB power
710 * delivery command has become available.
712 int typec_cable_set_identity(struct typec_cable
*cable
)
714 if (!cable
->identity
)
717 typec_report_identity(&cable
->dev
);
720 EXPORT_SYMBOL_GPL(typec_cable_set_identity
);
723 * typec_register_cable - Register a USB Type-C Cable
724 * @port: The USB Type-C Port the cable is connected to
725 * @desc: Description of the cable
727 * Registers a device for USB Type-C Cable described in @desc. The cable will be
728 * parent for the optional cable plug devises.
730 * Returns handle to the cable on success or NULL on failure.
732 struct typec_cable
*typec_register_cable(struct typec_port
*port
,
733 struct typec_cable_desc
*desc
)
735 struct typec_cable
*cable
;
738 cable
= kzalloc(sizeof(*cable
), GFP_KERNEL
);
742 cable
->type
= desc
->type
;
743 cable
->active
= desc
->active
;
745 if (desc
->identity
) {
747 * Creating directory for the identity only if the driver is
748 * able to provide data to it.
750 cable
->dev
.groups
= usb_pd_id_groups
;
751 cable
->identity
= desc
->identity
;
754 cable
->dev
.class = typec_class
;
755 cable
->dev
.parent
= &port
->dev
;
756 cable
->dev
.type
= &typec_cable_dev_type
;
757 dev_set_name(&cable
->dev
, "%s-cable", dev_name(&port
->dev
));
759 ret
= device_register(&cable
->dev
);
761 dev_err(&port
->dev
, "failed to register cable (%d)\n", ret
);
762 put_device(&cable
->dev
);
768 EXPORT_SYMBOL_GPL(typec_register_cable
);
771 * typec_unregister_cable - Unregister a USB Type-C Cable
772 * @cable: The cable to be unregistered
774 * Unregister device created with typec_register_cable().
776 void typec_unregister_cable(struct typec_cable
*cable
)
779 device_unregister(&cable
->dev
);
781 EXPORT_SYMBOL_GPL(typec_unregister_cable
);
783 /* ------------------------------------------------------------------------- */
784 /* USB Type-C ports */
786 static const char * const typec_roles
[] = {
787 [TYPEC_SINK
] = "sink",
788 [TYPEC_SOURCE
] = "source",
791 static const char * const typec_data_roles
[] = {
792 [TYPEC_DEVICE
] = "device",
793 [TYPEC_HOST
] = "host",
796 static const char * const typec_port_types
[] = {
797 [TYPEC_PORT_DFP
] = "source",
798 [TYPEC_PORT_UFP
] = "sink",
799 [TYPEC_PORT_DRP
] = "dual",
802 static const char * const typec_port_types_drp
[] = {
803 [TYPEC_PORT_DFP
] = "dual [source] sink",
804 [TYPEC_PORT_UFP
] = "dual source [sink]",
805 [TYPEC_PORT_DRP
] = "[dual] source sink",
809 preferred_role_store(struct device
*dev
, struct device_attribute
*attr
,
810 const char *buf
, size_t size
)
812 struct typec_port
*port
= to_typec_port(dev
);
816 if (port
->cap
->type
!= TYPEC_PORT_DRP
) {
817 dev_dbg(dev
, "Preferred role only supported with DRP ports\n");
821 if (!port
->cap
->try_role
) {
822 dev_dbg(dev
, "Setting preferred role not supported\n");
826 role
= sysfs_match_string(typec_roles
, buf
);
828 if (sysfs_streq(buf
, "none"))
829 role
= TYPEC_NO_PREFERRED_ROLE
;
834 ret
= port
->cap
->try_role(port
->cap
, role
);
838 port
->prefer_role
= role
;
843 preferred_role_show(struct device
*dev
, struct device_attribute
*attr
,
846 struct typec_port
*port
= to_typec_port(dev
);
848 if (port
->cap
->type
!= TYPEC_PORT_DRP
)
851 if (port
->prefer_role
< 0)
854 return sprintf(buf
, "%s\n", typec_roles
[port
->prefer_role
]);
856 static DEVICE_ATTR_RW(preferred_role
);
858 static ssize_t
data_role_store(struct device
*dev
,
859 struct device_attribute
*attr
,
860 const char *buf
, size_t size
)
862 struct typec_port
*port
= to_typec_port(dev
);
865 if (!port
->cap
->dr_set
) {
866 dev_dbg(dev
, "data role swapping not supported\n");
870 ret
= sysfs_match_string(typec_data_roles
, buf
);
874 mutex_lock(&port
->port_type_lock
);
875 if (port
->port_type
!= TYPEC_PORT_DRP
) {
876 dev_dbg(dev
, "port type fixed at \"%s\"",
877 typec_port_types
[port
->port_type
]);
882 ret
= port
->cap
->dr_set(port
->cap
, ret
);
888 mutex_unlock(&port
->port_type_lock
);
892 static ssize_t
data_role_show(struct device
*dev
,
893 struct device_attribute
*attr
, char *buf
)
895 struct typec_port
*port
= to_typec_port(dev
);
897 if (port
->cap
->type
== TYPEC_PORT_DRP
)
898 return sprintf(buf
, "%s\n", port
->data_role
== TYPEC_HOST
?
899 "[host] device" : "host [device]");
901 return sprintf(buf
, "[%s]\n", typec_data_roles
[port
->data_role
]);
903 static DEVICE_ATTR_RW(data_role
);
905 static ssize_t
power_role_store(struct device
*dev
,
906 struct device_attribute
*attr
,
907 const char *buf
, size_t size
)
909 struct typec_port
*port
= to_typec_port(dev
);
912 if (!port
->cap
->pd_revision
) {
913 dev_dbg(dev
, "USB Power Delivery not supported\n");
917 if (!port
->cap
->pr_set
) {
918 dev_dbg(dev
, "power role swapping not supported\n");
922 if (port
->pwr_opmode
!= TYPEC_PWR_MODE_PD
) {
923 dev_dbg(dev
, "partner unable to swap power role\n");
927 ret
= sysfs_match_string(typec_roles
, buf
);
931 mutex_lock(&port
->port_type_lock
);
932 if (port
->port_type
!= TYPEC_PORT_DRP
) {
933 dev_dbg(dev
, "port type fixed at \"%s\"",
934 typec_port_types
[port
->port_type
]);
939 ret
= port
->cap
->pr_set(port
->cap
, ret
);
945 mutex_unlock(&port
->port_type_lock
);
949 static ssize_t
power_role_show(struct device
*dev
,
950 struct device_attribute
*attr
, char *buf
)
952 struct typec_port
*port
= to_typec_port(dev
);
954 if (port
->cap
->type
== TYPEC_PORT_DRP
)
955 return sprintf(buf
, "%s\n", port
->pwr_role
== TYPEC_SOURCE
?
956 "[source] sink" : "source [sink]");
958 return sprintf(buf
, "[%s]\n", typec_roles
[port
->pwr_role
]);
960 static DEVICE_ATTR_RW(power_role
);
963 port_type_store(struct device
*dev
, struct device_attribute
*attr
,
964 const char *buf
, size_t size
)
966 struct typec_port
*port
= to_typec_port(dev
);
968 enum typec_port_type type
;
970 if (!port
->cap
->port_type_set
|| port
->cap
->type
!= TYPEC_PORT_DRP
) {
971 dev_dbg(dev
, "changing port type not supported\n");
975 ret
= sysfs_match_string(typec_port_types
, buf
);
980 mutex_lock(&port
->port_type_lock
);
982 if (port
->port_type
== type
) {
987 ret
= port
->cap
->port_type_set(port
->cap
, type
);
991 port
->port_type
= type
;
995 mutex_unlock(&port
->port_type_lock
);
1000 port_type_show(struct device
*dev
, struct device_attribute
*attr
,
1003 struct typec_port
*port
= to_typec_port(dev
);
1005 if (port
->cap
->type
== TYPEC_PORT_DRP
)
1006 return sprintf(buf
, "%s\n",
1007 typec_port_types_drp
[port
->port_type
]);
1009 return sprintf(buf
, "[%s]\n", typec_port_types
[port
->cap
->type
]);
1011 static DEVICE_ATTR_RW(port_type
);
1013 static const char * const typec_pwr_opmodes
[] = {
1014 [TYPEC_PWR_MODE_USB
] = "default",
1015 [TYPEC_PWR_MODE_1_5A
] = "1.5A",
1016 [TYPEC_PWR_MODE_3_0A
] = "3.0A",
1017 [TYPEC_PWR_MODE_PD
] = "usb_power_delivery",
1020 static ssize_t
power_operation_mode_show(struct device
*dev
,
1021 struct device_attribute
*attr
,
1024 struct typec_port
*port
= to_typec_port(dev
);
1026 return sprintf(buf
, "%s\n", typec_pwr_opmodes
[port
->pwr_opmode
]);
1028 static DEVICE_ATTR_RO(power_operation_mode
);
1030 static ssize_t
vconn_source_store(struct device
*dev
,
1031 struct device_attribute
*attr
,
1032 const char *buf
, size_t size
)
1034 struct typec_port
*port
= to_typec_port(dev
);
1038 if (!port
->cap
->pd_revision
) {
1039 dev_dbg(dev
, "VCONN swap depends on USB Power Delivery\n");
1043 if (!port
->cap
->vconn_set
) {
1044 dev_dbg(dev
, "VCONN swapping not supported\n");
1048 ret
= kstrtobool(buf
, &source
);
1052 ret
= port
->cap
->vconn_set(port
->cap
, (enum typec_role
)source
);
1059 static ssize_t
vconn_source_show(struct device
*dev
,
1060 struct device_attribute
*attr
, char *buf
)
1062 struct typec_port
*port
= to_typec_port(dev
);
1064 return sprintf(buf
, "%s\n",
1065 port
->vconn_role
== TYPEC_SOURCE
? "yes" : "no");
1067 static DEVICE_ATTR_RW(vconn_source
);
1069 static ssize_t
supported_accessory_modes_show(struct device
*dev
,
1070 struct device_attribute
*attr
,
1073 struct typec_port
*port
= to_typec_port(dev
);
1077 for (i
= 0; i
< ARRAY_SIZE(port
->cap
->accessory
); i
++) {
1078 if (port
->cap
->accessory
[i
])
1079 ret
+= sprintf(buf
+ ret
, "%s ",
1080 typec_accessory_modes
[port
->cap
->accessory
[i
]]);
1084 return sprintf(buf
, "none\n");
1086 buf
[ret
- 1] = '\n';
1090 static DEVICE_ATTR_RO(supported_accessory_modes
);
1092 static ssize_t
usb_typec_revision_show(struct device
*dev
,
1093 struct device_attribute
*attr
,
1096 struct typec_port
*port
= to_typec_port(dev
);
1097 u16 rev
= port
->cap
->revision
;
1099 return sprintf(buf
, "%d.%d\n", (rev
>> 8) & 0xff, (rev
>> 4) & 0xf);
1101 static DEVICE_ATTR_RO(usb_typec_revision
);
1103 static ssize_t
usb_power_delivery_revision_show(struct device
*dev
,
1104 struct device_attribute
*attr
,
1107 struct typec_port
*p
= to_typec_port(dev
);
1109 return sprintf(buf
, "%d\n", (p
->cap
->pd_revision
>> 8) & 0xff);
1111 static DEVICE_ATTR_RO(usb_power_delivery_revision
);
1113 static struct attribute
*typec_attrs
[] = {
1114 &dev_attr_data_role
.attr
,
1115 &dev_attr_power_operation_mode
.attr
,
1116 &dev_attr_power_role
.attr
,
1117 &dev_attr_preferred_role
.attr
,
1118 &dev_attr_supported_accessory_modes
.attr
,
1119 &dev_attr_usb_power_delivery_revision
.attr
,
1120 &dev_attr_usb_typec_revision
.attr
,
1121 &dev_attr_vconn_source
.attr
,
1122 &dev_attr_port_type
.attr
,
1125 ATTRIBUTE_GROUPS(typec
);
1127 static int typec_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
1131 ret
= add_uevent_var(env
, "TYPEC_PORT=%s", dev_name(dev
));
1133 dev_err(dev
, "failed to add uevent TYPEC_PORT\n");
1138 static void typec_release(struct device
*dev
)
1140 struct typec_port
*port
= to_typec_port(dev
);
1142 ida_simple_remove(&typec_index_ida
, port
->id
);
1146 static const struct device_type typec_port_dev_type
= {
1147 .name
= "typec_port",
1148 .groups
= typec_groups
,
1149 .uevent
= typec_uevent
,
1150 .release
= typec_release
,
1153 /* --------------------------------------- */
1154 /* Driver callbacks to report role updates */
1157 * typec_set_data_role - Report data role change
1158 * @port: The USB Type-C Port where the role was changed
1159 * @role: The new data role
1161 * This routine is used by the port drivers to report data role changes.
1163 void typec_set_data_role(struct typec_port
*port
, enum typec_data_role role
)
1165 if (port
->data_role
== role
)
1168 port
->data_role
= role
;
1169 sysfs_notify(&port
->dev
.kobj
, NULL
, "data_role");
1170 kobject_uevent(&port
->dev
.kobj
, KOBJ_CHANGE
);
1172 EXPORT_SYMBOL_GPL(typec_set_data_role
);
1175 * typec_set_pwr_role - Report power role change
1176 * @port: The USB Type-C Port where the role was changed
1177 * @role: The new data role
1179 * This routine is used by the port drivers to report power role changes.
1181 void typec_set_pwr_role(struct typec_port
*port
, enum typec_role role
)
1183 if (port
->pwr_role
== role
)
1186 port
->pwr_role
= role
;
1187 sysfs_notify(&port
->dev
.kobj
, NULL
, "power_role");
1188 kobject_uevent(&port
->dev
.kobj
, KOBJ_CHANGE
);
1190 EXPORT_SYMBOL_GPL(typec_set_pwr_role
);
1193 * typec_set_pwr_role - Report VCONN source change
1194 * @port: The USB Type-C Port which VCONN role changed
1195 * @role: Source when @port is sourcing VCONN, or Sink when it's not
1197 * This routine is used by the port drivers to report if the VCONN source is
1200 void typec_set_vconn_role(struct typec_port
*port
, enum typec_role role
)
1202 if (port
->vconn_role
== role
)
1205 port
->vconn_role
= role
;
1206 sysfs_notify(&port
->dev
.kobj
, NULL
, "vconn_source");
1207 kobject_uevent(&port
->dev
.kobj
, KOBJ_CHANGE
);
1209 EXPORT_SYMBOL_GPL(typec_set_vconn_role
);
1211 static int partner_match(struct device
*dev
, void *data
)
1213 return is_typec_partner(dev
);
1217 * typec_set_pwr_opmode - Report changed power operation mode
1218 * @port: The USB Type-C Port where the mode was changed
1219 * @opmode: New power operation mode
1221 * This routine is used by the port drivers to report changed power operation
1222 * mode in @port. The modes are USB (default), 1.5A, 3.0A as defined in USB
1223 * Type-C specification, and "USB Power Delivery" when the power levels are
1224 * negotiated with methods defined in USB Power Delivery specification.
1226 void typec_set_pwr_opmode(struct typec_port
*port
,
1227 enum typec_pwr_opmode opmode
)
1229 struct device
*partner_dev
;
1231 if (port
->pwr_opmode
== opmode
)
1234 port
->pwr_opmode
= opmode
;
1235 sysfs_notify(&port
->dev
.kobj
, NULL
, "power_operation_mode");
1236 kobject_uevent(&port
->dev
.kobj
, KOBJ_CHANGE
);
1238 partner_dev
= device_find_child(&port
->dev
, NULL
, partner_match
);
1240 struct typec_partner
*partner
= to_typec_partner(partner_dev
);
1242 if (opmode
== TYPEC_PWR_MODE_PD
&& !partner
->usb_pd
) {
1243 partner
->usb_pd
= 1;
1244 sysfs_notify(&partner_dev
->kobj
, NULL
,
1245 "supports_usb_power_delivery");
1247 put_device(partner_dev
);
1250 EXPORT_SYMBOL_GPL(typec_set_pwr_opmode
);
1252 /* --------------------------------------- */
1255 * typec_port_register_altmode - Register USB Type-C Port Alternate Mode
1256 * @port: USB Type-C Port that supports the alternate mode
1257 * @desc: Description of the alternate mode
1259 * This routine is used to register an alternate mode that @port is capable of
1262 * Returns handle to the alternate mode on success or NULL on failure.
1264 struct typec_altmode
*
1265 typec_port_register_altmode(struct typec_port
*port
,
1266 const struct typec_altmode_desc
*desc
)
1268 return typec_register_altmode(&port
->dev
, desc
);
1270 EXPORT_SYMBOL_GPL(typec_port_register_altmode
);
1273 * typec_register_port - Register a USB Type-C Port
1274 * @parent: Parent device
1275 * @cap: Description of the port
1277 * Registers a device for USB Type-C Port described in @cap.
1279 * Returns handle to the port on success or NULL on failure.
1281 struct typec_port
*typec_register_port(struct device
*parent
,
1282 const struct typec_capability
*cap
)
1284 struct typec_port
*port
;
1289 port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
1293 id
= ida_simple_get(&typec_index_ida
, 0, 0, GFP_KERNEL
);
1299 if (cap
->type
== TYPEC_PORT_DFP
)
1300 role
= TYPEC_SOURCE
;
1301 else if (cap
->type
== TYPEC_PORT_UFP
)
1304 role
= cap
->prefer_role
;
1306 if (role
== TYPEC_SOURCE
) {
1307 port
->data_role
= TYPEC_HOST
;
1308 port
->pwr_role
= TYPEC_SOURCE
;
1309 port
->vconn_role
= TYPEC_SOURCE
;
1311 port
->data_role
= TYPEC_DEVICE
;
1312 port
->pwr_role
= TYPEC_SINK
;
1313 port
->vconn_role
= TYPEC_SINK
;
1318 port
->port_type
= cap
->type
;
1319 mutex_init(&port
->port_type_lock
);
1320 port
->prefer_role
= cap
->prefer_role
;
1322 port
->dev
.class = typec_class
;
1323 port
->dev
.parent
= parent
;
1324 port
->dev
.fwnode
= cap
->fwnode
;
1325 port
->dev
.type
= &typec_port_dev_type
;
1326 dev_set_name(&port
->dev
, "port%d", id
);
1328 ret
= device_register(&port
->dev
);
1330 dev_err(parent
, "failed to register port (%d)\n", ret
);
1331 put_device(&port
->dev
);
1337 EXPORT_SYMBOL_GPL(typec_register_port
);
1340 * typec_unregister_port - Unregister a USB Type-C Port
1341 * @port: The port to be unregistered
1343 * Unregister device created with typec_register_port().
1345 void typec_unregister_port(struct typec_port
*port
)
1348 device_unregister(&port
->dev
);
1350 EXPORT_SYMBOL_GPL(typec_unregister_port
);
1352 static int __init
typec_init(void)
1354 typec_class
= class_create(THIS_MODULE
, "typec");
1355 return PTR_ERR_OR_ZERO(typec_class
);
1357 subsys_initcall(typec_init
);
1359 static void __exit
typec_exit(void)
1361 class_destroy(typec_class
);
1362 ida_destroy(&typec_index_ida
);
1364 module_exit(typec_exit
);
1366 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1367 MODULE_LICENSE("GPL v2");
1368 MODULE_DESCRIPTION("USB Type-C Connector Class");