1 // SPDX-License-Identifier: GPL-2.0
3 * USB Type-C Connector Class
5 * Copyright (C) 2017, Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
9 #include <linux/device.h>
10 #include <linux/module.h>
11 #include <linux/mutex.h>
12 #include <linux/slab.h>
13 #include <linux/usb/typec.h>
19 enum typec_port_type roles
;
21 struct typec_altmode
*alt_mode
;
23 unsigned int active
:1;
26 struct attribute_group group
;
27 struct attribute
*attrs
[5];
28 struct device_attribute vdo_attr
;
29 struct device_attribute desc_attr
;
30 struct device_attribute active_attr
;
31 struct device_attribute roles_attr
;
34 struct typec_altmode
{
38 struct typec_mode modes
[ALTMODE_MAX_MODES
];
39 const struct attribute_group
*mode_groups
[ALTMODE_MAX_MODES
];
44 enum typec_plug_index index
;
49 enum typec_plug_type type
;
50 struct usb_pd_identity
*identity
;
51 unsigned int active
:1;
54 struct typec_partner
{
56 unsigned int usb_pd
:1;
57 struct usb_pd_identity
*identity
;
58 enum typec_accessory accessory
;
66 enum typec_data_role data_role
;
67 enum typec_role pwr_role
;
68 enum typec_role vconn_role
;
69 enum typec_pwr_opmode pwr_opmode
;
70 enum typec_port_type port_type
;
71 struct mutex port_type_lock
;
73 const struct typec_capability
*cap
;
76 #define to_typec_port(_dev_) container_of(_dev_, struct typec_port, dev)
77 #define to_typec_plug(_dev_) container_of(_dev_, struct typec_plug, dev)
78 #define to_typec_cable(_dev_) container_of(_dev_, struct typec_cable, dev)
79 #define to_typec_partner(_dev_) container_of(_dev_, struct typec_partner, dev)
80 #define to_altmode(_dev_) container_of(_dev_, struct typec_altmode, dev)
82 static const struct device_type typec_partner_dev_type
;
83 static const struct device_type typec_cable_dev_type
;
84 static const struct device_type typec_plug_dev_type
;
85 static const struct device_type typec_port_dev_type
;
87 #define is_typec_partner(_dev_) (_dev_->type == &typec_partner_dev_type)
88 #define is_typec_cable(_dev_) (_dev_->type == &typec_cable_dev_type)
89 #define is_typec_plug(_dev_) (_dev_->type == &typec_plug_dev_type)
90 #define is_typec_port(_dev_) (_dev_->type == &typec_port_dev_type)
92 static DEFINE_IDA(typec_index_ida
);
93 static struct class *typec_class
;
95 /* Common attributes */
97 static const char * const typec_accessory_modes
[] = {
98 [TYPEC_ACCESSORY_NONE
] = "none",
99 [TYPEC_ACCESSORY_AUDIO
] = "analog_audio",
100 [TYPEC_ACCESSORY_DEBUG
] = "debug",
103 static struct usb_pd_identity
*get_pd_identity(struct device
*dev
)
105 if (is_typec_partner(dev
)) {
106 struct typec_partner
*partner
= to_typec_partner(dev
);
108 return partner
->identity
;
109 } else if (is_typec_cable(dev
)) {
110 struct typec_cable
*cable
= to_typec_cable(dev
);
112 return cable
->identity
;
117 static ssize_t
id_header_show(struct device
*dev
, struct device_attribute
*attr
,
120 struct usb_pd_identity
*id
= get_pd_identity(dev
);
122 return sprintf(buf
, "0x%08x\n", id
->id_header
);
124 static DEVICE_ATTR_RO(id_header
);
126 static ssize_t
cert_stat_show(struct device
*dev
, struct device_attribute
*attr
,
129 struct usb_pd_identity
*id
= get_pd_identity(dev
);
131 return sprintf(buf
, "0x%08x\n", id
->cert_stat
);
133 static DEVICE_ATTR_RO(cert_stat
);
135 static ssize_t
product_show(struct device
*dev
, struct device_attribute
*attr
,
138 struct usb_pd_identity
*id
= get_pd_identity(dev
);
140 return sprintf(buf
, "0x%08x\n", id
->product
);
142 static DEVICE_ATTR_RO(product
);
144 static struct attribute
*usb_pd_id_attrs
[] = {
145 &dev_attr_id_header
.attr
,
146 &dev_attr_cert_stat
.attr
,
147 &dev_attr_product
.attr
,
151 static const struct attribute_group usb_pd_id_group
= {
153 .attrs
= usb_pd_id_attrs
,
156 static const struct attribute_group
*usb_pd_id_groups
[] = {
161 static void typec_report_identity(struct device
*dev
)
163 sysfs_notify(&dev
->kobj
, "identity", "id_header");
164 sysfs_notify(&dev
->kobj
, "identity", "cert_stat");
165 sysfs_notify(&dev
->kobj
, "identity", "product");
168 /* ------------------------------------------------------------------------- */
169 /* Alternate Modes */
172 * typec_altmode_update_active - Report Enter/Exit mode
173 * @alt: Handle to the alternate mode
175 * @active: True when the mode has been entered
177 * If a partner or cable plug executes Enter/Exit Mode command successfully, the
178 * drivers use this routine to report the updated state of the mode.
180 void typec_altmode_update_active(struct typec_altmode
*alt
, int mode
,
183 struct typec_mode
*m
= &alt
->modes
[mode
];
186 if (m
->active
== active
)
190 snprintf(dir
, sizeof(dir
), "mode%d", mode
);
191 sysfs_notify(&alt
->dev
.kobj
, dir
, "active");
192 kobject_uevent(&alt
->dev
.kobj
, KOBJ_CHANGE
);
194 EXPORT_SYMBOL_GPL(typec_altmode_update_active
);
197 * typec_altmode2port - Alternate Mode to USB Type-C port
198 * @alt: The Alternate Mode
200 * Returns handle to the port that a cable plug or partner with @alt is
203 struct typec_port
*typec_altmode2port(struct typec_altmode
*alt
)
205 if (is_typec_plug(alt
->dev
.parent
))
206 return to_typec_port(alt
->dev
.parent
->parent
->parent
);
207 if (is_typec_partner(alt
->dev
.parent
))
208 return to_typec_port(alt
->dev
.parent
->parent
);
209 if (is_typec_port(alt
->dev
.parent
))
210 return to_typec_port(alt
->dev
.parent
);
214 EXPORT_SYMBOL_GPL(typec_altmode2port
);
217 typec_altmode_vdo_show(struct device
*dev
, struct device_attribute
*attr
,
220 struct typec_mode
*mode
= container_of(attr
, struct typec_mode
,
223 return sprintf(buf
, "0x%08x\n", mode
->vdo
);
227 typec_altmode_desc_show(struct device
*dev
, struct device_attribute
*attr
,
230 struct typec_mode
*mode
= container_of(attr
, struct typec_mode
,
233 return sprintf(buf
, "%s\n", mode
->desc
? mode
->desc
: "");
237 typec_altmode_active_show(struct device
*dev
, struct device_attribute
*attr
,
240 struct typec_mode
*mode
= container_of(attr
, struct typec_mode
,
243 return sprintf(buf
, "%s\n", mode
->active
? "yes" : "no");
247 typec_altmode_active_store(struct device
*dev
, struct device_attribute
*attr
,
248 const char *buf
, size_t size
)
250 struct typec_mode
*mode
= container_of(attr
, struct typec_mode
,
252 struct typec_port
*port
= typec_altmode2port(mode
->alt_mode
);
256 if (!port
->cap
->activate_mode
)
259 ret
= kstrtobool(buf
, &activate
);
263 ret
= port
->cap
->activate_mode(port
->cap
, mode
->index
, activate
);
271 typec_altmode_roles_show(struct device
*dev
, struct device_attribute
*attr
,
274 struct typec_mode
*mode
= container_of(attr
, struct typec_mode
,
278 switch (mode
->roles
) {
280 ret
= sprintf(buf
, "source\n");
283 ret
= sprintf(buf
, "sink\n");
287 ret
= sprintf(buf
, "source sink\n");
293 static void typec_init_modes(struct typec_altmode
*alt
,
294 const struct typec_mode_desc
*desc
, bool is_port
)
298 for (i
= 0; i
< alt
->n_modes
; i
++, desc
++) {
299 struct typec_mode
*mode
= &alt
->modes
[i
];
301 /* Not considering the human readable description critical */
302 mode
->desc
= kstrdup(desc
->desc
, GFP_KERNEL
);
303 if (desc
->desc
&& !mode
->desc
)
304 dev_err(&alt
->dev
, "failed to copy mode%d desc\n", i
);
306 mode
->alt_mode
= alt
;
307 mode
->vdo
= desc
->vdo
;
308 mode
->roles
= desc
->roles
;
309 mode
->index
= desc
->index
;
310 sprintf(mode
->group_name
, "mode%d", desc
->index
);
312 sysfs_attr_init(&mode
->vdo_attr
.attr
);
313 mode
->vdo_attr
.attr
.name
= "vdo";
314 mode
->vdo_attr
.attr
.mode
= 0444;
315 mode
->vdo_attr
.show
= typec_altmode_vdo_show
;
317 sysfs_attr_init(&mode
->desc_attr
.attr
);
318 mode
->desc_attr
.attr
.name
= "description";
319 mode
->desc_attr
.attr
.mode
= 0444;
320 mode
->desc_attr
.show
= typec_altmode_desc_show
;
322 sysfs_attr_init(&mode
->active_attr
.attr
);
323 mode
->active_attr
.attr
.name
= "active";
324 mode
->active_attr
.attr
.mode
= 0644;
325 mode
->active_attr
.show
= typec_altmode_active_show
;
326 mode
->active_attr
.store
= typec_altmode_active_store
;
328 mode
->attrs
[0] = &mode
->vdo_attr
.attr
;
329 mode
->attrs
[1] = &mode
->desc_attr
.attr
;
330 mode
->attrs
[2] = &mode
->active_attr
.attr
;
332 /* With ports, list the roles that the mode is supported with */
334 sysfs_attr_init(&mode
->roles_attr
.attr
);
335 mode
->roles_attr
.attr
.name
= "supported_roles";
336 mode
->roles_attr
.attr
.mode
= 0444;
337 mode
->roles_attr
.show
= typec_altmode_roles_show
;
339 mode
->attrs
[3] = &mode
->roles_attr
.attr
;
342 mode
->group
.attrs
= mode
->attrs
;
343 mode
->group
.name
= mode
->group_name
;
345 alt
->mode_groups
[i
] = &mode
->group
;
349 static ssize_t
svid_show(struct device
*dev
, struct device_attribute
*attr
,
352 struct typec_altmode
*alt
= to_altmode(dev
);
354 return sprintf(buf
, "%04x\n", alt
->svid
);
356 static DEVICE_ATTR_RO(svid
);
358 static struct attribute
*typec_altmode_attrs
[] = {
362 ATTRIBUTE_GROUPS(typec_altmode
);
364 static void typec_altmode_release(struct device
*dev
)
366 struct typec_altmode
*alt
= to_altmode(dev
);
369 for (i
= 0; i
< alt
->n_modes
; i
++)
370 kfree(alt
->modes
[i
].desc
);
374 static const struct device_type typec_altmode_dev_type
= {
375 .name
= "typec_alternate_mode",
376 .groups
= typec_altmode_groups
,
377 .release
= typec_altmode_release
,
380 static struct typec_altmode
*
381 typec_register_altmode(struct device
*parent
,
382 const struct typec_altmode_desc
*desc
)
384 struct typec_altmode
*alt
;
387 alt
= kzalloc(sizeof(*alt
), GFP_KERNEL
);
391 alt
->svid
= desc
->svid
;
392 alt
->n_modes
= desc
->n_modes
;
393 typec_init_modes(alt
, desc
->modes
, is_typec_port(parent
));
395 alt
->dev
.parent
= parent
;
396 alt
->dev
.groups
= alt
->mode_groups
;
397 alt
->dev
.type
= &typec_altmode_dev_type
;
398 dev_set_name(&alt
->dev
, "svid-%04x", alt
->svid
);
400 ret
= device_register(&alt
->dev
);
402 dev_err(parent
, "failed to register alternate mode (%d)\n",
404 put_device(&alt
->dev
);
412 * typec_unregister_altmode - Unregister Alternate Mode
413 * @alt: The alternate mode to be unregistered
415 * Unregister device created with typec_partner_register_altmode(),
416 * typec_plug_register_altmode() or typec_port_register_altmode().
418 void typec_unregister_altmode(struct typec_altmode
*alt
)
421 device_unregister(&alt
->dev
);
423 EXPORT_SYMBOL_GPL(typec_unregister_altmode
);
425 /* ------------------------------------------------------------------------- */
426 /* Type-C Partners */
428 static ssize_t
accessory_mode_show(struct device
*dev
,
429 struct device_attribute
*attr
,
432 struct typec_partner
*p
= to_typec_partner(dev
);
434 return sprintf(buf
, "%s\n", typec_accessory_modes
[p
->accessory
]);
436 static DEVICE_ATTR_RO(accessory_mode
);
438 static ssize_t
supports_usb_power_delivery_show(struct device
*dev
,
439 struct device_attribute
*attr
,
442 struct typec_partner
*p
= to_typec_partner(dev
);
444 return sprintf(buf
, "%s\n", p
->usb_pd
? "yes" : "no");
446 static DEVICE_ATTR_RO(supports_usb_power_delivery
);
448 static struct attribute
*typec_partner_attrs
[] = {
449 &dev_attr_accessory_mode
.attr
,
450 &dev_attr_supports_usb_power_delivery
.attr
,
453 ATTRIBUTE_GROUPS(typec_partner
);
455 static void typec_partner_release(struct device
*dev
)
457 struct typec_partner
*partner
= to_typec_partner(dev
);
462 static const struct device_type typec_partner_dev_type
= {
463 .name
= "typec_partner",
464 .groups
= typec_partner_groups
,
465 .release
= typec_partner_release
,
469 * typec_partner_set_identity - Report result from Discover Identity command
470 * @partner: The partner updated identity values
472 * This routine is used to report that the result of Discover Identity USB power
473 * delivery command has become available.
475 int typec_partner_set_identity(struct typec_partner
*partner
)
477 if (!partner
->identity
)
480 typec_report_identity(&partner
->dev
);
483 EXPORT_SYMBOL_GPL(typec_partner_set_identity
);
486 * typec_partner_register_altmode - Register USB Type-C Partner Alternate Mode
487 * @partner: USB Type-C Partner that supports the alternate mode
488 * @desc: Description of the alternate mode
490 * This routine is used to register each alternate mode individually that
491 * @partner has listed in response to Discover SVIDs command. The modes for a
492 * SVID listed in response to Discover Modes command need to be listed in an
495 * Returns handle to the alternate mode on success or NULL on failure.
497 struct typec_altmode
*
498 typec_partner_register_altmode(struct typec_partner
*partner
,
499 const struct typec_altmode_desc
*desc
)
501 return typec_register_altmode(&partner
->dev
, desc
);
503 EXPORT_SYMBOL_GPL(typec_partner_register_altmode
);
506 * typec_register_partner - Register a USB Type-C Partner
507 * @port: The USB Type-C Port the partner is connected to
508 * @desc: Description of the partner
510 * Registers a device for USB Type-C Partner described in @desc.
512 * Returns handle to the partner on success or NULL on failure.
514 struct typec_partner
*typec_register_partner(struct typec_port
*port
,
515 struct typec_partner_desc
*desc
)
517 struct typec_partner
*partner
;
520 partner
= kzalloc(sizeof(*partner
), GFP_KERNEL
);
524 partner
->usb_pd
= desc
->usb_pd
;
525 partner
->accessory
= desc
->accessory
;
527 if (desc
->identity
) {
529 * Creating directory for the identity only if the driver is
530 * able to provide data to it.
532 partner
->dev
.groups
= usb_pd_id_groups
;
533 partner
->identity
= desc
->identity
;
536 partner
->dev
.class = typec_class
;
537 partner
->dev
.parent
= &port
->dev
;
538 partner
->dev
.type
= &typec_partner_dev_type
;
539 dev_set_name(&partner
->dev
, "%s-partner", dev_name(&port
->dev
));
541 ret
= device_register(&partner
->dev
);
543 dev_err(&port
->dev
, "failed to register partner (%d)\n", ret
);
544 put_device(&partner
->dev
);
550 EXPORT_SYMBOL_GPL(typec_register_partner
);
553 * typec_unregister_partner - Unregister a USB Type-C Partner
554 * @partner: The partner to be unregistered
556 * Unregister device created with typec_register_partner().
558 void typec_unregister_partner(struct typec_partner
*partner
)
561 device_unregister(&partner
->dev
);
563 EXPORT_SYMBOL_GPL(typec_unregister_partner
);
565 /* ------------------------------------------------------------------------- */
566 /* Type-C Cable Plugs */
568 static void typec_plug_release(struct device
*dev
)
570 struct typec_plug
*plug
= to_typec_plug(dev
);
575 static const struct device_type typec_plug_dev_type
= {
576 .name
= "typec_plug",
577 .release
= typec_plug_release
,
581 * typec_plug_register_altmode - Register USB Type-C Cable Plug Alternate Mode
582 * @plug: USB Type-C Cable Plug that supports the alternate mode
583 * @desc: Description of the alternate mode
585 * This routine is used to register each alternate mode individually that @plug
586 * has listed in response to Discover SVIDs command. The modes for a SVID that
587 * the plug lists in response to Discover Modes command need to be listed in an
590 * Returns handle to the alternate mode on success or NULL on failure.
592 struct typec_altmode
*
593 typec_plug_register_altmode(struct typec_plug
*plug
,
594 const struct typec_altmode_desc
*desc
)
596 return typec_register_altmode(&plug
->dev
, desc
);
598 EXPORT_SYMBOL_GPL(typec_plug_register_altmode
);
601 * typec_register_plug - Register a USB Type-C Cable Plug
602 * @cable: USB Type-C Cable with the plug
603 * @desc: Description of the cable plug
605 * Registers a device for USB Type-C Cable Plug described in @desc. A USB Type-C
606 * Cable Plug represents a plug with electronics in it that can response to USB
607 * Power Delivery SOP Prime or SOP Double Prime packages.
609 * Returns handle to the cable plug on success or NULL on failure.
611 struct typec_plug
*typec_register_plug(struct typec_cable
*cable
,
612 struct typec_plug_desc
*desc
)
614 struct typec_plug
*plug
;
618 plug
= kzalloc(sizeof(*plug
), GFP_KERNEL
);
622 sprintf(name
, "plug%d", desc
->index
);
624 plug
->index
= desc
->index
;
625 plug
->dev
.class = typec_class
;
626 plug
->dev
.parent
= &cable
->dev
;
627 plug
->dev
.type
= &typec_plug_dev_type
;
628 dev_set_name(&plug
->dev
, "%s-%s", dev_name(cable
->dev
.parent
), name
);
630 ret
= device_register(&plug
->dev
);
632 dev_err(&cable
->dev
, "failed to register plug (%d)\n", ret
);
633 put_device(&plug
->dev
);
639 EXPORT_SYMBOL_GPL(typec_register_plug
);
642 * typec_unregister_plug - Unregister a USB Type-C Cable Plug
643 * @plug: The cable plug to be unregistered
645 * Unregister device created with typec_register_plug().
647 void typec_unregister_plug(struct typec_plug
*plug
)
650 device_unregister(&plug
->dev
);
652 EXPORT_SYMBOL_GPL(typec_unregister_plug
);
657 type_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
659 struct typec_cable
*cable
= to_typec_cable(dev
);
661 return sprintf(buf
, "%s\n", cable
->active
? "active" : "passive");
663 static DEVICE_ATTR_RO(type
);
665 static const char * const typec_plug_types
[] = {
666 [USB_PLUG_NONE
] = "unknown",
667 [USB_PLUG_TYPE_A
] = "type-a",
668 [USB_PLUG_TYPE_B
] = "type-b",
669 [USB_PLUG_TYPE_C
] = "type-c",
670 [USB_PLUG_CAPTIVE
] = "captive",
673 static ssize_t
plug_type_show(struct device
*dev
,
674 struct device_attribute
*attr
, char *buf
)
676 struct typec_cable
*cable
= to_typec_cable(dev
);
678 return sprintf(buf
, "%s\n", typec_plug_types
[cable
->type
]);
680 static DEVICE_ATTR_RO(plug_type
);
682 static struct attribute
*typec_cable_attrs
[] = {
684 &dev_attr_plug_type
.attr
,
687 ATTRIBUTE_GROUPS(typec_cable
);
689 static void typec_cable_release(struct device
*dev
)
691 struct typec_cable
*cable
= to_typec_cable(dev
);
696 static const struct device_type typec_cable_dev_type
= {
697 .name
= "typec_cable",
698 .groups
= typec_cable_groups
,
699 .release
= typec_cable_release
,
703 * typec_cable_set_identity - Report result from Discover Identity command
704 * @cable: The cable updated identity values
706 * This routine is used to report that the result of Discover Identity USB power
707 * delivery command has become available.
709 int typec_cable_set_identity(struct typec_cable
*cable
)
711 if (!cable
->identity
)
714 typec_report_identity(&cable
->dev
);
717 EXPORT_SYMBOL_GPL(typec_cable_set_identity
);
720 * typec_register_cable - Register a USB Type-C Cable
721 * @port: The USB Type-C Port the cable is connected to
722 * @desc: Description of the cable
724 * Registers a device for USB Type-C Cable described in @desc. The cable will be
725 * parent for the optional cable plug devises.
727 * Returns handle to the cable on success or NULL on failure.
729 struct typec_cable
*typec_register_cable(struct typec_port
*port
,
730 struct typec_cable_desc
*desc
)
732 struct typec_cable
*cable
;
735 cable
= kzalloc(sizeof(*cable
), GFP_KERNEL
);
739 cable
->type
= desc
->type
;
740 cable
->active
= desc
->active
;
742 if (desc
->identity
) {
744 * Creating directory for the identity only if the driver is
745 * able to provide data to it.
747 cable
->dev
.groups
= usb_pd_id_groups
;
748 cable
->identity
= desc
->identity
;
751 cable
->dev
.class = typec_class
;
752 cable
->dev
.parent
= &port
->dev
;
753 cable
->dev
.type
= &typec_cable_dev_type
;
754 dev_set_name(&cable
->dev
, "%s-cable", dev_name(&port
->dev
));
756 ret
= device_register(&cable
->dev
);
758 dev_err(&port
->dev
, "failed to register cable (%d)\n", ret
);
759 put_device(&cable
->dev
);
765 EXPORT_SYMBOL_GPL(typec_register_cable
);
768 * typec_unregister_cable - Unregister a USB Type-C Cable
769 * @cable: The cable to be unregistered
771 * Unregister device created with typec_register_cable().
773 void typec_unregister_cable(struct typec_cable
*cable
)
776 device_unregister(&cable
->dev
);
778 EXPORT_SYMBOL_GPL(typec_unregister_cable
);
780 /* ------------------------------------------------------------------------- */
781 /* USB Type-C ports */
783 static const char * const typec_roles
[] = {
784 [TYPEC_SINK
] = "sink",
785 [TYPEC_SOURCE
] = "source",
788 static const char * const typec_data_roles
[] = {
789 [TYPEC_DEVICE
] = "device",
790 [TYPEC_HOST
] = "host",
793 static const char * const typec_port_types
[] = {
794 [TYPEC_PORT_DFP
] = "source",
795 [TYPEC_PORT_UFP
] = "sink",
796 [TYPEC_PORT_DRP
] = "dual",
799 static const char * const typec_port_types_drp
[] = {
800 [TYPEC_PORT_DFP
] = "dual [source] sink",
801 [TYPEC_PORT_UFP
] = "dual source [sink]",
802 [TYPEC_PORT_DRP
] = "[dual] source sink",
806 preferred_role_store(struct device
*dev
, struct device_attribute
*attr
,
807 const char *buf
, size_t size
)
809 struct typec_port
*port
= to_typec_port(dev
);
813 if (port
->cap
->type
!= TYPEC_PORT_DRP
) {
814 dev_dbg(dev
, "Preferred role only supported with DRP ports\n");
818 if (!port
->cap
->try_role
) {
819 dev_dbg(dev
, "Setting preferred role not supported\n");
823 role
= sysfs_match_string(typec_roles
, buf
);
825 if (sysfs_streq(buf
, "none"))
826 role
= TYPEC_NO_PREFERRED_ROLE
;
831 ret
= port
->cap
->try_role(port
->cap
, role
);
835 port
->prefer_role
= role
;
840 preferred_role_show(struct device
*dev
, struct device_attribute
*attr
,
843 struct typec_port
*port
= to_typec_port(dev
);
845 if (port
->cap
->type
!= TYPEC_PORT_DRP
)
848 if (port
->prefer_role
< 0)
851 return sprintf(buf
, "%s\n", typec_roles
[port
->prefer_role
]);
853 static DEVICE_ATTR_RW(preferred_role
);
855 static ssize_t
data_role_store(struct device
*dev
,
856 struct device_attribute
*attr
,
857 const char *buf
, size_t size
)
859 struct typec_port
*port
= to_typec_port(dev
);
862 if (!port
->cap
->dr_set
) {
863 dev_dbg(dev
, "data role swapping not supported\n");
867 ret
= sysfs_match_string(typec_data_roles
, buf
);
871 mutex_lock(&port
->port_type_lock
);
872 if (port
->port_type
!= TYPEC_PORT_DRP
) {
873 dev_dbg(dev
, "port type fixed at \"%s\"",
874 typec_port_types
[port
->port_type
]);
879 ret
= port
->cap
->dr_set(port
->cap
, ret
);
885 mutex_unlock(&port
->port_type_lock
);
889 static ssize_t
data_role_show(struct device
*dev
,
890 struct device_attribute
*attr
, char *buf
)
892 struct typec_port
*port
= to_typec_port(dev
);
894 if (port
->cap
->type
== TYPEC_PORT_DRP
)
895 return sprintf(buf
, "%s\n", port
->data_role
== TYPEC_HOST
?
896 "[host] device" : "host [device]");
898 return sprintf(buf
, "[%s]\n", typec_data_roles
[port
->data_role
]);
900 static DEVICE_ATTR_RW(data_role
);
902 static ssize_t
power_role_store(struct device
*dev
,
903 struct device_attribute
*attr
,
904 const char *buf
, size_t size
)
906 struct typec_port
*port
= to_typec_port(dev
);
909 if (!port
->cap
->pd_revision
) {
910 dev_dbg(dev
, "USB Power Delivery not supported\n");
914 if (!port
->cap
->pr_set
) {
915 dev_dbg(dev
, "power role swapping not supported\n");
919 if (port
->pwr_opmode
!= TYPEC_PWR_MODE_PD
) {
920 dev_dbg(dev
, "partner unable to swap power role\n");
924 ret
= sysfs_match_string(typec_roles
, buf
);
928 mutex_lock(&port
->port_type_lock
);
929 if (port
->port_type
!= TYPEC_PORT_DRP
) {
930 dev_dbg(dev
, "port type fixed at \"%s\"",
931 typec_port_types
[port
->port_type
]);
936 ret
= port
->cap
->pr_set(port
->cap
, ret
);
942 mutex_unlock(&port
->port_type_lock
);
946 static ssize_t
power_role_show(struct device
*dev
,
947 struct device_attribute
*attr
, char *buf
)
949 struct typec_port
*port
= to_typec_port(dev
);
951 if (port
->cap
->type
== TYPEC_PORT_DRP
)
952 return sprintf(buf
, "%s\n", port
->pwr_role
== TYPEC_SOURCE
?
953 "[source] sink" : "source [sink]");
955 return sprintf(buf
, "[%s]\n", typec_roles
[port
->pwr_role
]);
957 static DEVICE_ATTR_RW(power_role
);
960 port_type_store(struct device
*dev
, struct device_attribute
*attr
,
961 const char *buf
, size_t size
)
963 struct typec_port
*port
= to_typec_port(dev
);
965 enum typec_port_type type
;
967 if (!port
->cap
->port_type_set
|| port
->cap
->type
!= TYPEC_PORT_DRP
) {
968 dev_dbg(dev
, "changing port type not supported\n");
972 ret
= sysfs_match_string(typec_port_types
, buf
);
977 mutex_lock(&port
->port_type_lock
);
979 if (port
->port_type
== type
) {
984 ret
= port
->cap
->port_type_set(port
->cap
, type
);
988 port
->port_type
= type
;
992 mutex_unlock(&port
->port_type_lock
);
997 port_type_show(struct device
*dev
, struct device_attribute
*attr
,
1000 struct typec_port
*port
= to_typec_port(dev
);
1002 if (port
->cap
->type
== TYPEC_PORT_DRP
)
1003 return sprintf(buf
, "%s\n",
1004 typec_port_types_drp
[port
->port_type
]);
1006 return sprintf(buf
, "[%s]\n", typec_port_types
[port
->cap
->type
]);
1008 static DEVICE_ATTR_RW(port_type
);
1010 static const char * const typec_pwr_opmodes
[] = {
1011 [TYPEC_PWR_MODE_USB
] = "default",
1012 [TYPEC_PWR_MODE_1_5A
] = "1.5A",
1013 [TYPEC_PWR_MODE_3_0A
] = "3.0A",
1014 [TYPEC_PWR_MODE_PD
] = "usb_power_delivery",
1017 static ssize_t
power_operation_mode_show(struct device
*dev
,
1018 struct device_attribute
*attr
,
1021 struct typec_port
*port
= to_typec_port(dev
);
1023 return sprintf(buf
, "%s\n", typec_pwr_opmodes
[port
->pwr_opmode
]);
1025 static DEVICE_ATTR_RO(power_operation_mode
);
1027 static ssize_t
vconn_source_store(struct device
*dev
,
1028 struct device_attribute
*attr
,
1029 const char *buf
, size_t size
)
1031 struct typec_port
*port
= to_typec_port(dev
);
1035 if (!port
->cap
->pd_revision
) {
1036 dev_dbg(dev
, "VCONN swap depends on USB Power Delivery\n");
1040 if (!port
->cap
->vconn_set
) {
1041 dev_dbg(dev
, "VCONN swapping not supported\n");
1045 ret
= kstrtobool(buf
, &source
);
1049 ret
= port
->cap
->vconn_set(port
->cap
, (enum typec_role
)source
);
1056 static ssize_t
vconn_source_show(struct device
*dev
,
1057 struct device_attribute
*attr
, char *buf
)
1059 struct typec_port
*port
= to_typec_port(dev
);
1061 return sprintf(buf
, "%s\n",
1062 port
->vconn_role
== TYPEC_SOURCE
? "yes" : "no");
1064 static DEVICE_ATTR_RW(vconn_source
);
1066 static ssize_t
supported_accessory_modes_show(struct device
*dev
,
1067 struct device_attribute
*attr
,
1070 struct typec_port
*port
= to_typec_port(dev
);
1074 for (i
= 0; i
< ARRAY_SIZE(port
->cap
->accessory
); i
++) {
1075 if (port
->cap
->accessory
[i
])
1076 ret
+= sprintf(buf
+ ret
, "%s ",
1077 typec_accessory_modes
[port
->cap
->accessory
[i
]]);
1081 return sprintf(buf
, "none\n");
1083 buf
[ret
- 1] = '\n';
1087 static DEVICE_ATTR_RO(supported_accessory_modes
);
1089 static ssize_t
usb_typec_revision_show(struct device
*dev
,
1090 struct device_attribute
*attr
,
1093 struct typec_port
*port
= to_typec_port(dev
);
1094 u16 rev
= port
->cap
->revision
;
1096 return sprintf(buf
, "%d.%d\n", (rev
>> 8) & 0xff, (rev
>> 4) & 0xf);
1098 static DEVICE_ATTR_RO(usb_typec_revision
);
1100 static ssize_t
usb_power_delivery_revision_show(struct device
*dev
,
1101 struct device_attribute
*attr
,
1104 struct typec_port
*p
= to_typec_port(dev
);
1106 return sprintf(buf
, "%d\n", (p
->cap
->pd_revision
>> 8) & 0xff);
1108 static DEVICE_ATTR_RO(usb_power_delivery_revision
);
1110 static struct attribute
*typec_attrs
[] = {
1111 &dev_attr_data_role
.attr
,
1112 &dev_attr_power_operation_mode
.attr
,
1113 &dev_attr_power_role
.attr
,
1114 &dev_attr_preferred_role
.attr
,
1115 &dev_attr_supported_accessory_modes
.attr
,
1116 &dev_attr_usb_power_delivery_revision
.attr
,
1117 &dev_attr_usb_typec_revision
.attr
,
1118 &dev_attr_vconn_source
.attr
,
1119 &dev_attr_port_type
.attr
,
1122 ATTRIBUTE_GROUPS(typec
);
1124 static int typec_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
1128 ret
= add_uevent_var(env
, "TYPEC_PORT=%s", dev_name(dev
));
1130 dev_err(dev
, "failed to add uevent TYPEC_PORT\n");
1135 static void typec_release(struct device
*dev
)
1137 struct typec_port
*port
= to_typec_port(dev
);
1139 ida_simple_remove(&typec_index_ida
, port
->id
);
1143 static const struct device_type typec_port_dev_type
= {
1144 .name
= "typec_port",
1145 .groups
= typec_groups
,
1146 .uevent
= typec_uevent
,
1147 .release
= typec_release
,
1150 /* --------------------------------------- */
1151 /* Driver callbacks to report role updates */
1154 * typec_set_data_role - Report data role change
1155 * @port: The USB Type-C Port where the role was changed
1156 * @role: The new data role
1158 * This routine is used by the port drivers to report data role changes.
1160 void typec_set_data_role(struct typec_port
*port
, enum typec_data_role role
)
1162 if (port
->data_role
== role
)
1165 port
->data_role
= role
;
1166 sysfs_notify(&port
->dev
.kobj
, NULL
, "data_role");
1167 kobject_uevent(&port
->dev
.kobj
, KOBJ_CHANGE
);
1169 EXPORT_SYMBOL_GPL(typec_set_data_role
);
1172 * typec_set_pwr_role - Report power role change
1173 * @port: The USB Type-C Port where the role was changed
1174 * @role: The new data role
1176 * This routine is used by the port drivers to report power role changes.
1178 void typec_set_pwr_role(struct typec_port
*port
, enum typec_role role
)
1180 if (port
->pwr_role
== role
)
1183 port
->pwr_role
= role
;
1184 sysfs_notify(&port
->dev
.kobj
, NULL
, "power_role");
1185 kobject_uevent(&port
->dev
.kobj
, KOBJ_CHANGE
);
1187 EXPORT_SYMBOL_GPL(typec_set_pwr_role
);
1190 * typec_set_pwr_role - Report VCONN source change
1191 * @port: The USB Type-C Port which VCONN role changed
1192 * @role: Source when @port is sourcing VCONN, or Sink when it's not
1194 * This routine is used by the port drivers to report if the VCONN source is
1197 void typec_set_vconn_role(struct typec_port
*port
, enum typec_role role
)
1199 if (port
->vconn_role
== role
)
1202 port
->vconn_role
= role
;
1203 sysfs_notify(&port
->dev
.kobj
, NULL
, "vconn_source");
1204 kobject_uevent(&port
->dev
.kobj
, KOBJ_CHANGE
);
1206 EXPORT_SYMBOL_GPL(typec_set_vconn_role
);
1208 static int partner_match(struct device
*dev
, void *data
)
1210 return is_typec_partner(dev
);
1214 * typec_set_pwr_opmode - Report changed power operation mode
1215 * @port: The USB Type-C Port where the mode was changed
1216 * @opmode: New power operation mode
1218 * This routine is used by the port drivers to report changed power operation
1219 * mode in @port. The modes are USB (default), 1.5A, 3.0A as defined in USB
1220 * Type-C specification, and "USB Power Delivery" when the power levels are
1221 * negotiated with methods defined in USB Power Delivery specification.
1223 void typec_set_pwr_opmode(struct typec_port
*port
,
1224 enum typec_pwr_opmode opmode
)
1226 struct device
*partner_dev
;
1228 if (port
->pwr_opmode
== opmode
)
1231 port
->pwr_opmode
= opmode
;
1232 sysfs_notify(&port
->dev
.kobj
, NULL
, "power_operation_mode");
1233 kobject_uevent(&port
->dev
.kobj
, KOBJ_CHANGE
);
1235 partner_dev
= device_find_child(&port
->dev
, NULL
, partner_match
);
1237 struct typec_partner
*partner
= to_typec_partner(partner_dev
);
1239 if (opmode
== TYPEC_PWR_MODE_PD
&& !partner
->usb_pd
) {
1240 partner
->usb_pd
= 1;
1241 sysfs_notify(&partner_dev
->kobj
, NULL
,
1242 "supports_usb_power_delivery");
1244 put_device(partner_dev
);
1247 EXPORT_SYMBOL_GPL(typec_set_pwr_opmode
);
1249 /* --------------------------------------- */
1252 * typec_port_register_altmode - Register USB Type-C Port Alternate Mode
1253 * @port: USB Type-C Port that supports the alternate mode
1254 * @desc: Description of the alternate mode
1256 * This routine is used to register an alternate mode that @port is capable of
1259 * Returns handle to the alternate mode on success or NULL on failure.
1261 struct typec_altmode
*
1262 typec_port_register_altmode(struct typec_port
*port
,
1263 const struct typec_altmode_desc
*desc
)
1265 return typec_register_altmode(&port
->dev
, desc
);
1267 EXPORT_SYMBOL_GPL(typec_port_register_altmode
);
1270 * typec_register_port - Register a USB Type-C Port
1271 * @parent: Parent device
1272 * @cap: Description of the port
1274 * Registers a device for USB Type-C Port described in @cap.
1276 * Returns handle to the port on success or NULL on failure.
1278 struct typec_port
*typec_register_port(struct device
*parent
,
1279 const struct typec_capability
*cap
)
1281 struct typec_port
*port
;
1286 port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
1290 id
= ida_simple_get(&typec_index_ida
, 0, 0, GFP_KERNEL
);
1296 if (cap
->type
== TYPEC_PORT_DFP
)
1297 role
= TYPEC_SOURCE
;
1298 else if (cap
->type
== TYPEC_PORT_UFP
)
1301 role
= cap
->prefer_role
;
1303 if (role
== TYPEC_SOURCE
) {
1304 port
->data_role
= TYPEC_HOST
;
1305 port
->pwr_role
= TYPEC_SOURCE
;
1306 port
->vconn_role
= TYPEC_SOURCE
;
1308 port
->data_role
= TYPEC_DEVICE
;
1309 port
->pwr_role
= TYPEC_SINK
;
1310 port
->vconn_role
= TYPEC_SINK
;
1315 port
->port_type
= cap
->type
;
1316 mutex_init(&port
->port_type_lock
);
1317 port
->prefer_role
= cap
->prefer_role
;
1319 port
->dev
.class = typec_class
;
1320 port
->dev
.parent
= parent
;
1321 port
->dev
.fwnode
= cap
->fwnode
;
1322 port
->dev
.type
= &typec_port_dev_type
;
1323 dev_set_name(&port
->dev
, "port%d", id
);
1325 ret
= device_register(&port
->dev
);
1327 dev_err(parent
, "failed to register port (%d)\n", ret
);
1328 put_device(&port
->dev
);
1334 EXPORT_SYMBOL_GPL(typec_register_port
);
1337 * typec_unregister_port - Unregister a USB Type-C Port
1338 * @port: The port to be unregistered
1340 * Unregister device created with typec_register_port().
1342 void typec_unregister_port(struct typec_port
*port
)
1345 device_unregister(&port
->dev
);
1347 EXPORT_SYMBOL_GPL(typec_unregister_port
);
1349 static int __init
typec_init(void)
1351 typec_class
= class_create(THIS_MODULE
, "typec");
1352 return PTR_ERR_OR_ZERO(typec_class
);
1354 subsys_initcall(typec_init
);
1356 static void __exit
typec_exit(void)
1358 class_destroy(typec_class
);
1359 ida_destroy(&typec_index_ida
);
1361 module_exit(typec_exit
);
1363 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1364 MODULE_LICENSE("GPL v2");
1365 MODULE_DESCRIPTION("USB Type-C Connector Class");