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/property.h>
13 #include <linux/slab.h>
19 enum typec_plug_index index
;
25 enum typec_plug_type type
;
26 struct usb_pd_identity
*identity
;
27 unsigned int active
:1;
30 struct typec_partner
{
32 unsigned int usb_pd
:1;
33 struct usb_pd_identity
*identity
;
34 enum typec_accessory accessory
;
44 enum typec_data_role data_role
;
45 enum typec_role pwr_role
;
46 enum typec_role vconn_role
;
47 enum typec_pwr_opmode pwr_opmode
;
48 enum typec_port_type port_type
;
49 struct mutex port_type_lock
;
51 enum typec_orientation orientation
;
52 struct typec_switch
*sw
;
53 struct typec_mux
*mux
;
55 const struct typec_capability
*cap
;
58 #define to_typec_port(_dev_) container_of(_dev_, struct typec_port, dev)
59 #define to_typec_plug(_dev_) container_of(_dev_, struct typec_plug, dev)
60 #define to_typec_cable(_dev_) container_of(_dev_, struct typec_cable, dev)
61 #define to_typec_partner(_dev_) container_of(_dev_, struct typec_partner, dev)
63 static const struct device_type typec_partner_dev_type
;
64 static const struct device_type typec_cable_dev_type
;
65 static const struct device_type typec_plug_dev_type
;
67 #define is_typec_partner(_dev_) (_dev_->type == &typec_partner_dev_type)
68 #define is_typec_cable(_dev_) (_dev_->type == &typec_cable_dev_type)
69 #define is_typec_plug(_dev_) (_dev_->type == &typec_plug_dev_type)
71 static DEFINE_IDA(typec_index_ida
);
72 static struct class *typec_class
;
74 /* ------------------------------------------------------------------------- */
75 /* Common attributes */
77 static const char * const typec_accessory_modes
[] = {
78 [TYPEC_ACCESSORY_NONE
] = "none",
79 [TYPEC_ACCESSORY_AUDIO
] = "analog_audio",
80 [TYPEC_ACCESSORY_DEBUG
] = "debug",
83 static struct usb_pd_identity
*get_pd_identity(struct device
*dev
)
85 if (is_typec_partner(dev
)) {
86 struct typec_partner
*partner
= to_typec_partner(dev
);
88 return partner
->identity
;
89 } else if (is_typec_cable(dev
)) {
90 struct typec_cable
*cable
= to_typec_cable(dev
);
92 return cable
->identity
;
97 static ssize_t
id_header_show(struct device
*dev
, struct device_attribute
*attr
,
100 struct usb_pd_identity
*id
= get_pd_identity(dev
);
102 return sprintf(buf
, "0x%08x\n", id
->id_header
);
104 static DEVICE_ATTR_RO(id_header
);
106 static ssize_t
cert_stat_show(struct device
*dev
, struct device_attribute
*attr
,
109 struct usb_pd_identity
*id
= get_pd_identity(dev
);
111 return sprintf(buf
, "0x%08x\n", id
->cert_stat
);
113 static DEVICE_ATTR_RO(cert_stat
);
115 static ssize_t
product_show(struct device
*dev
, struct device_attribute
*attr
,
118 struct usb_pd_identity
*id
= get_pd_identity(dev
);
120 return sprintf(buf
, "0x%08x\n", id
->product
);
122 static DEVICE_ATTR_RO(product
);
124 static struct attribute
*usb_pd_id_attrs
[] = {
125 &dev_attr_id_header
.attr
,
126 &dev_attr_cert_stat
.attr
,
127 &dev_attr_product
.attr
,
131 static const struct attribute_group usb_pd_id_group
= {
133 .attrs
= usb_pd_id_attrs
,
136 static const struct attribute_group
*usb_pd_id_groups
[] = {
141 static void typec_report_identity(struct device
*dev
)
143 sysfs_notify(&dev
->kobj
, "identity", "id_header");
144 sysfs_notify(&dev
->kobj
, "identity", "cert_stat");
145 sysfs_notify(&dev
->kobj
, "identity", "product");
148 /* ------------------------------------------------------------------------- */
149 /* Alternate Modes */
151 static int altmode_match(struct device
*dev
, void *data
)
153 struct typec_altmode
*adev
= to_typec_altmode(dev
);
154 struct typec_device_id
*id
= data
;
156 if (!is_typec_altmode(dev
))
159 return ((adev
->svid
== id
->svid
) && (adev
->mode
== id
->mode
));
162 static void typec_altmode_set_partner(struct altmode
*altmode
)
164 struct typec_altmode
*adev
= &altmode
->adev
;
165 struct typec_device_id id
= { adev
->svid
, adev
->mode
, };
166 struct typec_port
*port
= typec_altmode2port(adev
);
167 struct altmode
*partner
;
170 dev
= device_find_child(&port
->dev
, &id
, altmode_match
);
174 /* Bind the port alt mode to the partner/plug alt mode. */
175 partner
= to_altmode(to_typec_altmode(dev
));
176 altmode
->partner
= partner
;
178 /* Bind the partner/plug alt mode to the port alt mode. */
179 if (is_typec_plug(adev
->dev
.parent
)) {
180 struct typec_plug
*plug
= to_typec_plug(adev
->dev
.parent
);
182 partner
->plug
[plug
->index
] = altmode
;
184 partner
->partner
= altmode
;
188 static void typec_altmode_put_partner(struct altmode
*altmode
)
190 struct altmode
*partner
= altmode
->partner
;
191 struct typec_altmode
*adev
;
196 adev
= &partner
->adev
;
198 if (is_typec_plug(adev
->dev
.parent
)) {
199 struct typec_plug
*plug
= to_typec_plug(adev
->dev
.parent
);
201 partner
->plug
[plug
->index
] = NULL
;
203 partner
->partner
= NULL
;
205 put_device(&adev
->dev
);
208 static void *typec_port_match(struct device_connection
*con
, int ep
, void *data
)
213 * FIXME: Check does the fwnode supports the requested SVID. If it does
214 * we need to return ERR_PTR(-PROBE_DEFER) when there is no device.
217 return class_find_device_by_fwnode(typec_class
, con
->fwnode
);
219 dev
= class_find_device_by_name(typec_class
, con
->endpoint
[ep
]);
221 return dev
? dev
: ERR_PTR(-EPROBE_DEFER
);
224 struct typec_altmode
*
225 typec_altmode_register_notifier(struct device
*dev
, u16 svid
, u8 mode
,
226 struct notifier_block
*nb
)
228 struct typec_device_id id
= { svid
, mode
, };
229 struct device
*altmode_dev
;
230 struct device
*port_dev
;
231 struct altmode
*altmode
;
234 /* Find the port linked to the caller */
235 port_dev
= device_connection_find_match(dev
, NULL
, NULL
,
237 if (IS_ERR_OR_NULL(port_dev
))
238 return port_dev
? ERR_CAST(port_dev
) : ERR_PTR(-ENODEV
);
240 /* Find the altmode with matching svid */
241 altmode_dev
= device_find_child(port_dev
, &id
, altmode_match
);
243 put_device(port_dev
);
246 return ERR_PTR(-ENODEV
);
248 altmode
= to_altmode(to_typec_altmode(altmode_dev
));
250 /* Register notifier */
251 ret
= blocking_notifier_chain_register(&altmode
->nh
, nb
);
253 put_device(altmode_dev
);
257 return &altmode
->adev
;
259 EXPORT_SYMBOL_GPL(typec_altmode_register_notifier
);
261 void typec_altmode_unregister_notifier(struct typec_altmode
*adev
,
262 struct notifier_block
*nb
)
264 struct altmode
*altmode
= to_altmode(adev
);
266 blocking_notifier_chain_unregister(&altmode
->nh
, nb
);
267 put_device(&adev
->dev
);
269 EXPORT_SYMBOL_GPL(typec_altmode_unregister_notifier
);
272 * typec_altmode_update_active - Report Enter/Exit mode
273 * @adev: Handle to the alternate mode
274 * @active: True when the mode has been entered
276 * If a partner or cable plug executes Enter/Exit Mode command successfully, the
277 * drivers use this routine to report the updated state of the mode.
279 void typec_altmode_update_active(struct typec_altmode
*adev
, bool active
)
283 if (adev
->active
== active
)
286 if (!is_typec_port(adev
->dev
.parent
) && adev
->dev
.driver
) {
288 module_put(adev
->dev
.driver
->owner
);
290 WARN_ON(!try_module_get(adev
->dev
.driver
->owner
));
293 adev
->active
= active
;
294 snprintf(dir
, sizeof(dir
), "mode%d", adev
->mode
);
295 sysfs_notify(&adev
->dev
.kobj
, dir
, "active");
296 sysfs_notify(&adev
->dev
.kobj
, NULL
, "active");
297 kobject_uevent(&adev
->dev
.kobj
, KOBJ_CHANGE
);
299 EXPORT_SYMBOL_GPL(typec_altmode_update_active
);
302 * typec_altmode2port - Alternate Mode to USB Type-C port
303 * @alt: The Alternate Mode
305 * Returns handle to the port that a cable plug or partner with @alt is
308 struct typec_port
*typec_altmode2port(struct typec_altmode
*alt
)
310 if (is_typec_plug(alt
->dev
.parent
))
311 return to_typec_port(alt
->dev
.parent
->parent
->parent
);
312 if (is_typec_partner(alt
->dev
.parent
))
313 return to_typec_port(alt
->dev
.parent
->parent
);
314 if (is_typec_port(alt
->dev
.parent
))
315 return to_typec_port(alt
->dev
.parent
);
319 EXPORT_SYMBOL_GPL(typec_altmode2port
);
322 vdo_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
324 struct typec_altmode
*alt
= to_typec_altmode(dev
);
326 return sprintf(buf
, "0x%08x\n", alt
->vdo
);
328 static DEVICE_ATTR_RO(vdo
);
331 description_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
333 struct typec_altmode
*alt
= to_typec_altmode(dev
);
335 return sprintf(buf
, "%s\n", alt
->desc
? alt
->desc
: "");
337 static DEVICE_ATTR_RO(description
);
340 active_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
342 struct typec_altmode
*alt
= to_typec_altmode(dev
);
344 return sprintf(buf
, "%s\n", alt
->active
? "yes" : "no");
347 static ssize_t
active_store(struct device
*dev
, struct device_attribute
*attr
,
348 const char *buf
, size_t size
)
350 struct typec_altmode
*adev
= to_typec_altmode(dev
);
351 struct altmode
*altmode
= to_altmode(adev
);
355 ret
= kstrtobool(buf
, &enter
);
359 if (adev
->active
== enter
)
362 if (is_typec_port(adev
->dev
.parent
)) {
363 typec_altmode_update_active(adev
, enter
);
365 /* Make sure that the partner exits the mode before disabling */
366 if (altmode
->partner
&& !enter
&& altmode
->partner
->adev
.active
)
367 typec_altmode_exit(&altmode
->partner
->adev
);
368 } else if (altmode
->partner
) {
369 if (enter
&& !altmode
->partner
->adev
.active
) {
370 dev_warn(dev
, "port has the mode disabled\n");
375 /* Note: If there is no driver, the mode will not be entered */
376 if (adev
->ops
&& adev
->ops
->activate
) {
377 ret
= adev
->ops
->activate(adev
, enter
);
384 static DEVICE_ATTR_RW(active
);
387 supported_roles_show(struct device
*dev
, struct device_attribute
*attr
,
390 struct altmode
*alt
= to_altmode(to_typec_altmode(dev
));
393 switch (alt
->roles
) {
395 ret
= sprintf(buf
, "source\n");
398 ret
= sprintf(buf
, "sink\n");
402 ret
= sprintf(buf
, "source sink\n");
407 static DEVICE_ATTR_RO(supported_roles
);
410 mode_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
412 struct typec_altmode
*adev
= to_typec_altmode(dev
);
414 return sprintf(buf
, "%u\n", adev
->mode
);
416 static DEVICE_ATTR_RO(mode
);
419 svid_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
421 struct typec_altmode
*adev
= to_typec_altmode(dev
);
423 return sprintf(buf
, "%04x\n", adev
->svid
);
425 static DEVICE_ATTR_RO(svid
);
427 static struct attribute
*typec_altmode_attrs
[] = {
428 &dev_attr_active
.attr
,
434 ATTRIBUTE_GROUPS(typec_altmode
);
436 static int altmode_id_get(struct device
*dev
)
440 if (is_typec_partner(dev
))
441 ids
= &to_typec_partner(dev
)->mode_ids
;
442 else if (is_typec_plug(dev
))
443 ids
= &to_typec_plug(dev
)->mode_ids
;
445 ids
= &to_typec_port(dev
)->mode_ids
;
447 return ida_simple_get(ids
, 0, 0, GFP_KERNEL
);
450 static void altmode_id_remove(struct device
*dev
, int id
)
454 if (is_typec_partner(dev
))
455 ids
= &to_typec_partner(dev
)->mode_ids
;
456 else if (is_typec_plug(dev
))
457 ids
= &to_typec_plug(dev
)->mode_ids
;
459 ids
= &to_typec_port(dev
)->mode_ids
;
461 ida_simple_remove(ids
, id
);
464 static void typec_altmode_release(struct device
*dev
)
466 struct altmode
*alt
= to_altmode(to_typec_altmode(dev
));
468 typec_altmode_put_partner(alt
);
470 altmode_id_remove(alt
->adev
.dev
.parent
, alt
->id
);
474 const struct device_type typec_altmode_dev_type
= {
475 .name
= "typec_alternate_mode",
476 .groups
= typec_altmode_groups
,
477 .release
= typec_altmode_release
,
480 static struct typec_altmode
*
481 typec_register_altmode(struct device
*parent
,
482 const struct typec_altmode_desc
*desc
)
484 unsigned int id
= altmode_id_get(parent
);
485 bool is_port
= is_typec_port(parent
);
489 alt
= kzalloc(sizeof(*alt
), GFP_KERNEL
);
491 return ERR_PTR(-ENOMEM
);
493 alt
->adev
.svid
= desc
->svid
;
494 alt
->adev
.mode
= desc
->mode
;
495 alt
->adev
.vdo
= desc
->vdo
;
496 alt
->roles
= desc
->roles
;
499 alt
->attrs
[0] = &dev_attr_vdo
.attr
;
500 alt
->attrs
[1] = &dev_attr_description
.attr
;
501 alt
->attrs
[2] = &dev_attr_active
.attr
;
504 alt
->attrs
[3] = &dev_attr_supported_roles
.attr
;
505 alt
->adev
.active
= true; /* Enabled by default */
508 sprintf(alt
->group_name
, "mode%d", desc
->mode
);
509 alt
->group
.name
= alt
->group_name
;
510 alt
->group
.attrs
= alt
->attrs
;
511 alt
->groups
[0] = &alt
->group
;
513 alt
->adev
.dev
.parent
= parent
;
514 alt
->adev
.dev
.groups
= alt
->groups
;
515 alt
->adev
.dev
.type
= &typec_altmode_dev_type
;
516 dev_set_name(&alt
->adev
.dev
, "%s.%u", dev_name(parent
), id
);
518 /* Link partners and plugs with the ports */
520 BLOCKING_INIT_NOTIFIER_HEAD(&alt
->nh
);
522 typec_altmode_set_partner(alt
);
524 /* The partners are bind to drivers */
525 if (is_typec_partner(parent
))
526 alt
->adev
.dev
.bus
= &typec_bus
;
528 ret
= device_register(&alt
->adev
.dev
);
530 dev_err(parent
, "failed to register alternate mode (%d)\n",
532 put_device(&alt
->adev
.dev
);
540 * typec_unregister_altmode - Unregister Alternate Mode
541 * @adev: The alternate mode to be unregistered
543 * Unregister device created with typec_partner_register_altmode(),
544 * typec_plug_register_altmode() or typec_port_register_altmode().
546 void typec_unregister_altmode(struct typec_altmode
*adev
)
548 if (IS_ERR_OR_NULL(adev
))
550 typec_mux_put(to_altmode(adev
)->mux
);
551 device_unregister(&adev
->dev
);
553 EXPORT_SYMBOL_GPL(typec_unregister_altmode
);
555 /* ------------------------------------------------------------------------- */
556 /* Type-C Partners */
558 static ssize_t
accessory_mode_show(struct device
*dev
,
559 struct device_attribute
*attr
,
562 struct typec_partner
*p
= to_typec_partner(dev
);
564 return sprintf(buf
, "%s\n", typec_accessory_modes
[p
->accessory
]);
566 static DEVICE_ATTR_RO(accessory_mode
);
568 static ssize_t
supports_usb_power_delivery_show(struct device
*dev
,
569 struct device_attribute
*attr
,
572 struct typec_partner
*p
= to_typec_partner(dev
);
574 return sprintf(buf
, "%s\n", p
->usb_pd
? "yes" : "no");
576 static DEVICE_ATTR_RO(supports_usb_power_delivery
);
578 static struct attribute
*typec_partner_attrs
[] = {
579 &dev_attr_accessory_mode
.attr
,
580 &dev_attr_supports_usb_power_delivery
.attr
,
583 ATTRIBUTE_GROUPS(typec_partner
);
585 static void typec_partner_release(struct device
*dev
)
587 struct typec_partner
*partner
= to_typec_partner(dev
);
589 ida_destroy(&partner
->mode_ids
);
593 static const struct device_type typec_partner_dev_type
= {
594 .name
= "typec_partner",
595 .groups
= typec_partner_groups
,
596 .release
= typec_partner_release
,
600 * typec_partner_set_identity - Report result from Discover Identity command
601 * @partner: The partner updated identity values
603 * This routine is used to report that the result of Discover Identity USB power
604 * delivery command has become available.
606 int typec_partner_set_identity(struct typec_partner
*partner
)
608 if (!partner
->identity
)
611 typec_report_identity(&partner
->dev
);
614 EXPORT_SYMBOL_GPL(typec_partner_set_identity
);
617 * typec_partner_register_altmode - Register USB Type-C Partner Alternate Mode
618 * @partner: USB Type-C Partner that supports the alternate mode
619 * @desc: Description of the alternate mode
621 * This routine is used to register each alternate mode individually that
622 * @partner has listed in response to Discover SVIDs command. The modes for a
623 * SVID listed in response to Discover Modes command need to be listed in an
626 * Returns handle to the alternate mode on success or NULL on failure.
628 struct typec_altmode
*
629 typec_partner_register_altmode(struct typec_partner
*partner
,
630 const struct typec_altmode_desc
*desc
)
632 return typec_register_altmode(&partner
->dev
, desc
);
634 EXPORT_SYMBOL_GPL(typec_partner_register_altmode
);
637 * typec_register_partner - Register a USB Type-C Partner
638 * @port: The USB Type-C Port the partner is connected to
639 * @desc: Description of the partner
641 * Registers a device for USB Type-C Partner described in @desc.
643 * Returns handle to the partner on success or ERR_PTR on failure.
645 struct typec_partner
*typec_register_partner(struct typec_port
*port
,
646 struct typec_partner_desc
*desc
)
648 struct typec_partner
*partner
;
651 partner
= kzalloc(sizeof(*partner
), GFP_KERNEL
);
653 return ERR_PTR(-ENOMEM
);
655 ida_init(&partner
->mode_ids
);
656 partner
->usb_pd
= desc
->usb_pd
;
657 partner
->accessory
= desc
->accessory
;
659 if (desc
->identity
) {
661 * Creating directory for the identity only if the driver is
662 * able to provide data to it.
664 partner
->dev
.groups
= usb_pd_id_groups
;
665 partner
->identity
= desc
->identity
;
668 partner
->dev
.class = typec_class
;
669 partner
->dev
.parent
= &port
->dev
;
670 partner
->dev
.type
= &typec_partner_dev_type
;
671 dev_set_name(&partner
->dev
, "%s-partner", dev_name(&port
->dev
));
673 ret
= device_register(&partner
->dev
);
675 dev_err(&port
->dev
, "failed to register partner (%d)\n", ret
);
676 put_device(&partner
->dev
);
682 EXPORT_SYMBOL_GPL(typec_register_partner
);
685 * typec_unregister_partner - Unregister a USB Type-C Partner
686 * @partner: The partner to be unregistered
688 * Unregister device created with typec_register_partner().
690 void typec_unregister_partner(struct typec_partner
*partner
)
692 if (!IS_ERR_OR_NULL(partner
))
693 device_unregister(&partner
->dev
);
695 EXPORT_SYMBOL_GPL(typec_unregister_partner
);
697 /* ------------------------------------------------------------------------- */
698 /* Type-C Cable Plugs */
700 static void typec_plug_release(struct device
*dev
)
702 struct typec_plug
*plug
= to_typec_plug(dev
);
704 ida_destroy(&plug
->mode_ids
);
708 static const struct device_type typec_plug_dev_type
= {
709 .name
= "typec_plug",
710 .release
= typec_plug_release
,
714 * typec_plug_register_altmode - Register USB Type-C Cable Plug Alternate Mode
715 * @plug: USB Type-C Cable Plug that supports the alternate mode
716 * @desc: Description of the alternate mode
718 * This routine is used to register each alternate mode individually that @plug
719 * has listed in response to Discover SVIDs command. The modes for a SVID that
720 * the plug lists in response to Discover Modes command need to be listed in an
723 * Returns handle to the alternate mode on success or ERR_PTR on failure.
725 struct typec_altmode
*
726 typec_plug_register_altmode(struct typec_plug
*plug
,
727 const struct typec_altmode_desc
*desc
)
729 return typec_register_altmode(&plug
->dev
, desc
);
731 EXPORT_SYMBOL_GPL(typec_plug_register_altmode
);
734 * typec_register_plug - Register a USB Type-C Cable Plug
735 * @cable: USB Type-C Cable with the plug
736 * @desc: Description of the cable plug
738 * Registers a device for USB Type-C Cable Plug described in @desc. A USB Type-C
739 * Cable Plug represents a plug with electronics in it that can response to USB
740 * Power Delivery SOP Prime or SOP Double Prime packages.
742 * Returns handle to the cable plug on success or ERR_PTR on failure.
744 struct typec_plug
*typec_register_plug(struct typec_cable
*cable
,
745 struct typec_plug_desc
*desc
)
747 struct typec_plug
*plug
;
751 plug
= kzalloc(sizeof(*plug
), GFP_KERNEL
);
753 return ERR_PTR(-ENOMEM
);
755 sprintf(name
, "plug%d", desc
->index
);
757 ida_init(&plug
->mode_ids
);
758 plug
->index
= desc
->index
;
759 plug
->dev
.class = typec_class
;
760 plug
->dev
.parent
= &cable
->dev
;
761 plug
->dev
.type
= &typec_plug_dev_type
;
762 dev_set_name(&plug
->dev
, "%s-%s", dev_name(cable
->dev
.parent
), name
);
764 ret
= device_register(&plug
->dev
);
766 dev_err(&cable
->dev
, "failed to register plug (%d)\n", ret
);
767 put_device(&plug
->dev
);
773 EXPORT_SYMBOL_GPL(typec_register_plug
);
776 * typec_unregister_plug - Unregister a USB Type-C Cable Plug
777 * @plug: The cable plug to be unregistered
779 * Unregister device created with typec_register_plug().
781 void typec_unregister_plug(struct typec_plug
*plug
)
783 if (!IS_ERR_OR_NULL(plug
))
784 device_unregister(&plug
->dev
);
786 EXPORT_SYMBOL_GPL(typec_unregister_plug
);
791 type_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
793 struct typec_cable
*cable
= to_typec_cable(dev
);
795 return sprintf(buf
, "%s\n", cable
->active
? "active" : "passive");
797 static DEVICE_ATTR_RO(type
);
799 static const char * const typec_plug_types
[] = {
800 [USB_PLUG_NONE
] = "unknown",
801 [USB_PLUG_TYPE_A
] = "type-a",
802 [USB_PLUG_TYPE_B
] = "type-b",
803 [USB_PLUG_TYPE_C
] = "type-c",
804 [USB_PLUG_CAPTIVE
] = "captive",
807 static ssize_t
plug_type_show(struct device
*dev
,
808 struct device_attribute
*attr
, char *buf
)
810 struct typec_cable
*cable
= to_typec_cable(dev
);
812 return sprintf(buf
, "%s\n", typec_plug_types
[cable
->type
]);
814 static DEVICE_ATTR_RO(plug_type
);
816 static struct attribute
*typec_cable_attrs
[] = {
818 &dev_attr_plug_type
.attr
,
821 ATTRIBUTE_GROUPS(typec_cable
);
823 static void typec_cable_release(struct device
*dev
)
825 struct typec_cable
*cable
= to_typec_cable(dev
);
830 static const struct device_type typec_cable_dev_type
= {
831 .name
= "typec_cable",
832 .groups
= typec_cable_groups
,
833 .release
= typec_cable_release
,
837 * typec_cable_set_identity - Report result from Discover Identity command
838 * @cable: The cable updated identity values
840 * This routine is used to report that the result of Discover Identity USB power
841 * delivery command has become available.
843 int typec_cable_set_identity(struct typec_cable
*cable
)
845 if (!cable
->identity
)
848 typec_report_identity(&cable
->dev
);
851 EXPORT_SYMBOL_GPL(typec_cable_set_identity
);
854 * typec_register_cable - Register a USB Type-C Cable
855 * @port: The USB Type-C Port the cable is connected to
856 * @desc: Description of the cable
858 * Registers a device for USB Type-C Cable described in @desc. The cable will be
859 * parent for the optional cable plug devises.
861 * Returns handle to the cable on success or ERR_PTR on failure.
863 struct typec_cable
*typec_register_cable(struct typec_port
*port
,
864 struct typec_cable_desc
*desc
)
866 struct typec_cable
*cable
;
869 cable
= kzalloc(sizeof(*cable
), GFP_KERNEL
);
871 return ERR_PTR(-ENOMEM
);
873 cable
->type
= desc
->type
;
874 cable
->active
= desc
->active
;
876 if (desc
->identity
) {
878 * Creating directory for the identity only if the driver is
879 * able to provide data to it.
881 cable
->dev
.groups
= usb_pd_id_groups
;
882 cable
->identity
= desc
->identity
;
885 cable
->dev
.class = typec_class
;
886 cable
->dev
.parent
= &port
->dev
;
887 cable
->dev
.type
= &typec_cable_dev_type
;
888 dev_set_name(&cable
->dev
, "%s-cable", dev_name(&port
->dev
));
890 ret
= device_register(&cable
->dev
);
892 dev_err(&port
->dev
, "failed to register cable (%d)\n", ret
);
893 put_device(&cable
->dev
);
899 EXPORT_SYMBOL_GPL(typec_register_cable
);
902 * typec_unregister_cable - Unregister a USB Type-C Cable
903 * @cable: The cable to be unregistered
905 * Unregister device created with typec_register_cable().
907 void typec_unregister_cable(struct typec_cable
*cable
)
909 if (!IS_ERR_OR_NULL(cable
))
910 device_unregister(&cable
->dev
);
912 EXPORT_SYMBOL_GPL(typec_unregister_cable
);
914 /* ------------------------------------------------------------------------- */
915 /* USB Type-C ports */
917 static const char * const typec_roles
[] = {
918 [TYPEC_SINK
] = "sink",
919 [TYPEC_SOURCE
] = "source",
922 static const char * const typec_data_roles
[] = {
923 [TYPEC_DEVICE
] = "device",
924 [TYPEC_HOST
] = "host",
927 static const char * const typec_port_power_roles
[] = {
928 [TYPEC_PORT_SRC
] = "source",
929 [TYPEC_PORT_SNK
] = "sink",
930 [TYPEC_PORT_DRP
] = "dual",
933 static const char * const typec_port_data_roles
[] = {
934 [TYPEC_PORT_DFP
] = "host",
935 [TYPEC_PORT_UFP
] = "device",
936 [TYPEC_PORT_DRD
] = "dual",
939 static const char * const typec_port_types_drp
[] = {
940 [TYPEC_PORT_SRC
] = "dual [source] sink",
941 [TYPEC_PORT_SNK
] = "dual source [sink]",
942 [TYPEC_PORT_DRP
] = "[dual] source sink",
946 preferred_role_store(struct device
*dev
, struct device_attribute
*attr
,
947 const char *buf
, size_t size
)
949 struct typec_port
*port
= to_typec_port(dev
);
953 if (port
->cap
->type
!= TYPEC_PORT_DRP
) {
954 dev_dbg(dev
, "Preferred role only supported with DRP ports\n");
958 if (!port
->cap
->try_role
) {
959 dev_dbg(dev
, "Setting preferred role not supported\n");
963 role
= sysfs_match_string(typec_roles
, buf
);
965 if (sysfs_streq(buf
, "none"))
966 role
= TYPEC_NO_PREFERRED_ROLE
;
971 ret
= port
->cap
->try_role(port
->cap
, role
);
975 port
->prefer_role
= role
;
980 preferred_role_show(struct device
*dev
, struct device_attribute
*attr
,
983 struct typec_port
*port
= to_typec_port(dev
);
985 if (port
->cap
->type
!= TYPEC_PORT_DRP
)
988 if (port
->prefer_role
< 0)
991 return sprintf(buf
, "%s\n", typec_roles
[port
->prefer_role
]);
993 static DEVICE_ATTR_RW(preferred_role
);
995 static ssize_t
data_role_store(struct device
*dev
,
996 struct device_attribute
*attr
,
997 const char *buf
, size_t size
)
999 struct typec_port
*port
= to_typec_port(dev
);
1002 if (!port
->cap
->dr_set
) {
1003 dev_dbg(dev
, "data role swapping not supported\n");
1007 ret
= sysfs_match_string(typec_data_roles
, buf
);
1011 mutex_lock(&port
->port_type_lock
);
1012 if (port
->cap
->data
!= TYPEC_PORT_DRD
) {
1014 goto unlock_and_ret
;
1017 ret
= port
->cap
->dr_set(port
->cap
, ret
);
1019 goto unlock_and_ret
;
1023 mutex_unlock(&port
->port_type_lock
);
1027 static ssize_t
data_role_show(struct device
*dev
,
1028 struct device_attribute
*attr
, char *buf
)
1030 struct typec_port
*port
= to_typec_port(dev
);
1032 if (port
->cap
->data
== TYPEC_PORT_DRD
)
1033 return sprintf(buf
, "%s\n", port
->data_role
== TYPEC_HOST
?
1034 "[host] device" : "host [device]");
1036 return sprintf(buf
, "[%s]\n", typec_data_roles
[port
->data_role
]);
1038 static DEVICE_ATTR_RW(data_role
);
1040 static ssize_t
power_role_store(struct device
*dev
,
1041 struct device_attribute
*attr
,
1042 const char *buf
, size_t size
)
1044 struct typec_port
*port
= to_typec_port(dev
);
1047 if (!port
->cap
->pd_revision
) {
1048 dev_dbg(dev
, "USB Power Delivery not supported\n");
1052 if (!port
->cap
->pr_set
) {
1053 dev_dbg(dev
, "power role swapping not supported\n");
1057 if (port
->pwr_opmode
!= TYPEC_PWR_MODE_PD
) {
1058 dev_dbg(dev
, "partner unable to swap power role\n");
1062 ret
= sysfs_match_string(typec_roles
, buf
);
1066 mutex_lock(&port
->port_type_lock
);
1067 if (port
->port_type
!= TYPEC_PORT_DRP
) {
1068 dev_dbg(dev
, "port type fixed at \"%s\"",
1069 typec_port_power_roles
[port
->port_type
]);
1071 goto unlock_and_ret
;
1074 ret
= port
->cap
->pr_set(port
->cap
, ret
);
1076 goto unlock_and_ret
;
1080 mutex_unlock(&port
->port_type_lock
);
1084 static ssize_t
power_role_show(struct device
*dev
,
1085 struct device_attribute
*attr
, char *buf
)
1087 struct typec_port
*port
= to_typec_port(dev
);
1089 if (port
->cap
->type
== TYPEC_PORT_DRP
)
1090 return sprintf(buf
, "%s\n", port
->pwr_role
== TYPEC_SOURCE
?
1091 "[source] sink" : "source [sink]");
1093 return sprintf(buf
, "[%s]\n", typec_roles
[port
->pwr_role
]);
1095 static DEVICE_ATTR_RW(power_role
);
1098 port_type_store(struct device
*dev
, struct device_attribute
*attr
,
1099 const char *buf
, size_t size
)
1101 struct typec_port
*port
= to_typec_port(dev
);
1103 enum typec_port_type type
;
1105 if (!port
->cap
->port_type_set
|| port
->cap
->type
!= TYPEC_PORT_DRP
) {
1106 dev_dbg(dev
, "changing port type not supported\n");
1110 ret
= sysfs_match_string(typec_port_power_roles
, buf
);
1115 mutex_lock(&port
->port_type_lock
);
1117 if (port
->port_type
== type
) {
1119 goto unlock_and_ret
;
1122 ret
= port
->cap
->port_type_set(port
->cap
, type
);
1124 goto unlock_and_ret
;
1126 port
->port_type
= type
;
1130 mutex_unlock(&port
->port_type_lock
);
1135 port_type_show(struct device
*dev
, struct device_attribute
*attr
,
1138 struct typec_port
*port
= to_typec_port(dev
);
1140 if (port
->cap
->type
== TYPEC_PORT_DRP
)
1141 return sprintf(buf
, "%s\n",
1142 typec_port_types_drp
[port
->port_type
]);
1144 return sprintf(buf
, "[%s]\n", typec_port_power_roles
[port
->cap
->type
]);
1146 static DEVICE_ATTR_RW(port_type
);
1148 static const char * const typec_pwr_opmodes
[] = {
1149 [TYPEC_PWR_MODE_USB
] = "default",
1150 [TYPEC_PWR_MODE_1_5A
] = "1.5A",
1151 [TYPEC_PWR_MODE_3_0A
] = "3.0A",
1152 [TYPEC_PWR_MODE_PD
] = "usb_power_delivery",
1155 static ssize_t
power_operation_mode_show(struct device
*dev
,
1156 struct device_attribute
*attr
,
1159 struct typec_port
*port
= to_typec_port(dev
);
1161 return sprintf(buf
, "%s\n", typec_pwr_opmodes
[port
->pwr_opmode
]);
1163 static DEVICE_ATTR_RO(power_operation_mode
);
1165 static ssize_t
vconn_source_store(struct device
*dev
,
1166 struct device_attribute
*attr
,
1167 const char *buf
, size_t size
)
1169 struct typec_port
*port
= to_typec_port(dev
);
1173 if (!port
->cap
->pd_revision
) {
1174 dev_dbg(dev
, "VCONN swap depends on USB Power Delivery\n");
1178 if (!port
->cap
->vconn_set
) {
1179 dev_dbg(dev
, "VCONN swapping not supported\n");
1183 ret
= kstrtobool(buf
, &source
);
1187 ret
= port
->cap
->vconn_set(port
->cap
, (enum typec_role
)source
);
1194 static ssize_t
vconn_source_show(struct device
*dev
,
1195 struct device_attribute
*attr
, char *buf
)
1197 struct typec_port
*port
= to_typec_port(dev
);
1199 return sprintf(buf
, "%s\n",
1200 port
->vconn_role
== TYPEC_SOURCE
? "yes" : "no");
1202 static DEVICE_ATTR_RW(vconn_source
);
1204 static ssize_t
supported_accessory_modes_show(struct device
*dev
,
1205 struct device_attribute
*attr
,
1208 struct typec_port
*port
= to_typec_port(dev
);
1212 for (i
= 0; i
< ARRAY_SIZE(port
->cap
->accessory
); i
++) {
1213 if (port
->cap
->accessory
[i
])
1214 ret
+= sprintf(buf
+ ret
, "%s ",
1215 typec_accessory_modes
[port
->cap
->accessory
[i
]]);
1219 return sprintf(buf
, "none\n");
1221 buf
[ret
- 1] = '\n';
1225 static DEVICE_ATTR_RO(supported_accessory_modes
);
1227 static ssize_t
usb_typec_revision_show(struct device
*dev
,
1228 struct device_attribute
*attr
,
1231 struct typec_port
*port
= to_typec_port(dev
);
1232 u16 rev
= port
->cap
->revision
;
1234 return sprintf(buf
, "%d.%d\n", (rev
>> 8) & 0xff, (rev
>> 4) & 0xf);
1236 static DEVICE_ATTR_RO(usb_typec_revision
);
1238 static ssize_t
usb_power_delivery_revision_show(struct device
*dev
,
1239 struct device_attribute
*attr
,
1242 struct typec_port
*p
= to_typec_port(dev
);
1244 return sprintf(buf
, "%d\n", (p
->cap
->pd_revision
>> 8) & 0xff);
1246 static DEVICE_ATTR_RO(usb_power_delivery_revision
);
1248 static struct attribute
*typec_attrs
[] = {
1249 &dev_attr_data_role
.attr
,
1250 &dev_attr_power_operation_mode
.attr
,
1251 &dev_attr_power_role
.attr
,
1252 &dev_attr_preferred_role
.attr
,
1253 &dev_attr_supported_accessory_modes
.attr
,
1254 &dev_attr_usb_power_delivery_revision
.attr
,
1255 &dev_attr_usb_typec_revision
.attr
,
1256 &dev_attr_vconn_source
.attr
,
1257 &dev_attr_port_type
.attr
,
1260 ATTRIBUTE_GROUPS(typec
);
1262 static int typec_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
1266 ret
= add_uevent_var(env
, "TYPEC_PORT=%s", dev_name(dev
));
1268 dev_err(dev
, "failed to add uevent TYPEC_PORT\n");
1273 static void typec_release(struct device
*dev
)
1275 struct typec_port
*port
= to_typec_port(dev
);
1277 ida_simple_remove(&typec_index_ida
, port
->id
);
1278 ida_destroy(&port
->mode_ids
);
1279 typec_switch_put(port
->sw
);
1280 typec_mux_put(port
->mux
);
1284 const struct device_type typec_port_dev_type
= {
1285 .name
= "typec_port",
1286 .groups
= typec_groups
,
1287 .uevent
= typec_uevent
,
1288 .release
= typec_release
,
1291 /* --------------------------------------- */
1292 /* Driver callbacks to report role updates */
1295 * typec_set_data_role - Report data role change
1296 * @port: The USB Type-C Port where the role was changed
1297 * @role: The new data role
1299 * This routine is used by the port drivers to report data role changes.
1301 void typec_set_data_role(struct typec_port
*port
, enum typec_data_role role
)
1303 if (port
->data_role
== role
)
1306 port
->data_role
= role
;
1307 sysfs_notify(&port
->dev
.kobj
, NULL
, "data_role");
1308 kobject_uevent(&port
->dev
.kobj
, KOBJ_CHANGE
);
1310 EXPORT_SYMBOL_GPL(typec_set_data_role
);
1313 * typec_set_pwr_role - Report power role change
1314 * @port: The USB Type-C Port where the role was changed
1315 * @role: The new data role
1317 * This routine is used by the port drivers to report power role changes.
1319 void typec_set_pwr_role(struct typec_port
*port
, enum typec_role role
)
1321 if (port
->pwr_role
== role
)
1324 port
->pwr_role
= role
;
1325 sysfs_notify(&port
->dev
.kobj
, NULL
, "power_role");
1326 kobject_uevent(&port
->dev
.kobj
, KOBJ_CHANGE
);
1328 EXPORT_SYMBOL_GPL(typec_set_pwr_role
);
1331 * typec_set_vconn_role - Report VCONN source change
1332 * @port: The USB Type-C Port which VCONN role changed
1333 * @role: Source when @port is sourcing VCONN, or Sink when it's not
1335 * This routine is used by the port drivers to report if the VCONN source is
1338 void typec_set_vconn_role(struct typec_port
*port
, enum typec_role role
)
1340 if (port
->vconn_role
== role
)
1343 port
->vconn_role
= role
;
1344 sysfs_notify(&port
->dev
.kobj
, NULL
, "vconn_source");
1345 kobject_uevent(&port
->dev
.kobj
, KOBJ_CHANGE
);
1347 EXPORT_SYMBOL_GPL(typec_set_vconn_role
);
1349 static int partner_match(struct device
*dev
, void *data
)
1351 return is_typec_partner(dev
);
1355 * typec_set_pwr_opmode - Report changed power operation mode
1356 * @port: The USB Type-C Port where the mode was changed
1357 * @opmode: New power operation mode
1359 * This routine is used by the port drivers to report changed power operation
1360 * mode in @port. The modes are USB (default), 1.5A, 3.0A as defined in USB
1361 * Type-C specification, and "USB Power Delivery" when the power levels are
1362 * negotiated with methods defined in USB Power Delivery specification.
1364 void typec_set_pwr_opmode(struct typec_port
*port
,
1365 enum typec_pwr_opmode opmode
)
1367 struct device
*partner_dev
;
1369 if (port
->pwr_opmode
== opmode
)
1372 port
->pwr_opmode
= opmode
;
1373 sysfs_notify(&port
->dev
.kobj
, NULL
, "power_operation_mode");
1374 kobject_uevent(&port
->dev
.kobj
, KOBJ_CHANGE
);
1376 partner_dev
= device_find_child(&port
->dev
, NULL
, partner_match
);
1378 struct typec_partner
*partner
= to_typec_partner(partner_dev
);
1380 if (opmode
== TYPEC_PWR_MODE_PD
&& !partner
->usb_pd
) {
1381 partner
->usb_pd
= 1;
1382 sysfs_notify(&partner_dev
->kobj
, NULL
,
1383 "supports_usb_power_delivery");
1385 put_device(partner_dev
);
1388 EXPORT_SYMBOL_GPL(typec_set_pwr_opmode
);
1391 * typec_find_port_power_role - Get the typec port power capability
1392 * @name: port power capability string
1394 * This routine is used to find the typec_port_type by its string name.
1396 * Returns typec_port_type if success, otherwise negative error code.
1398 int typec_find_port_power_role(const char *name
)
1400 return match_string(typec_port_power_roles
,
1401 ARRAY_SIZE(typec_port_power_roles
), name
);
1403 EXPORT_SYMBOL_GPL(typec_find_port_power_role
);
1406 * typec_find_power_role - Find the typec one specific power role
1407 * @name: power role string
1409 * This routine is used to find the typec_role by its string name.
1411 * Returns typec_role if success, otherwise negative error code.
1413 int typec_find_power_role(const char *name
)
1415 return match_string(typec_roles
, ARRAY_SIZE(typec_roles
), name
);
1417 EXPORT_SYMBOL_GPL(typec_find_power_role
);
1420 * typec_find_port_data_role - Get the typec port data capability
1421 * @name: port data capability string
1423 * This routine is used to find the typec_port_data by its string name.
1425 * Returns typec_port_data if success, otherwise negative error code.
1427 int typec_find_port_data_role(const char *name
)
1429 return match_string(typec_port_data_roles
,
1430 ARRAY_SIZE(typec_port_data_roles
), name
);
1432 EXPORT_SYMBOL_GPL(typec_find_port_data_role
);
1434 /* ------------------------------------------ */
1435 /* API for Multiplexer/DeMultiplexer Switches */
1438 * typec_set_orientation - Set USB Type-C cable plug orientation
1439 * @port: USB Type-C Port
1440 * @orientation: USB Type-C cable plug orientation
1442 * Set cable plug orientation for @port.
1444 int typec_set_orientation(struct typec_port
*port
,
1445 enum typec_orientation orientation
)
1450 ret
= port
->sw
->set(port
->sw
, orientation
);
1455 port
->orientation
= orientation
;
1459 EXPORT_SYMBOL_GPL(typec_set_orientation
);
1462 * typec_get_orientation - Get USB Type-C cable plug orientation
1463 * @port: USB Type-C Port
1465 * Get current cable plug orientation for @port.
1467 enum typec_orientation
typec_get_orientation(struct typec_port
*port
)
1469 return port
->orientation
;
1471 EXPORT_SYMBOL_GPL(typec_get_orientation
);
1474 * typec_set_mode - Set mode of operation for USB Type-C connector
1475 * @port: USB Type-C connector
1476 * @mode: Accessory Mode, USB Operation or Safe State
1478 * Configure @port for Accessory Mode @mode. This function will configure the
1479 * muxes needed for @mode.
1481 int typec_set_mode(struct typec_port
*port
, int mode
)
1483 return port
->mux
? port
->mux
->set(port
->mux
, mode
) : 0;
1485 EXPORT_SYMBOL_GPL(typec_set_mode
);
1487 /* --------------------------------------- */
1490 * typec_port_register_altmode - Register USB Type-C Port Alternate Mode
1491 * @port: USB Type-C Port that supports the alternate mode
1492 * @desc: Description of the alternate mode
1494 * This routine is used to register an alternate mode that @port is capable of
1497 * Returns handle to the alternate mode on success or ERR_PTR on failure.
1499 struct typec_altmode
*
1500 typec_port_register_altmode(struct typec_port
*port
,
1501 const struct typec_altmode_desc
*desc
)
1503 struct typec_altmode
*adev
;
1504 struct typec_mux
*mux
;
1506 mux
= typec_mux_get(&port
->dev
, desc
);
1508 return ERR_CAST(mux
);
1510 adev
= typec_register_altmode(&port
->dev
, desc
);
1514 to_altmode(adev
)->mux
= mux
;
1518 EXPORT_SYMBOL_GPL(typec_port_register_altmode
);
1521 * typec_register_port - Register a USB Type-C Port
1522 * @parent: Parent device
1523 * @cap: Description of the port
1525 * Registers a device for USB Type-C Port described in @cap.
1527 * Returns handle to the port on success or ERR_PTR on failure.
1529 struct typec_port
*typec_register_port(struct device
*parent
,
1530 const struct typec_capability
*cap
)
1532 struct typec_port
*port
;
1536 port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
1538 return ERR_PTR(-ENOMEM
);
1540 id
= ida_simple_get(&typec_index_ida
, 0, 0, GFP_KERNEL
);
1546 switch (cap
->type
) {
1547 case TYPEC_PORT_SRC
:
1548 port
->pwr_role
= TYPEC_SOURCE
;
1549 port
->vconn_role
= TYPEC_SOURCE
;
1551 case TYPEC_PORT_SNK
:
1552 port
->pwr_role
= TYPEC_SINK
;
1553 port
->vconn_role
= TYPEC_SINK
;
1555 case TYPEC_PORT_DRP
:
1556 if (cap
->prefer_role
!= TYPEC_NO_PREFERRED_ROLE
)
1557 port
->pwr_role
= cap
->prefer_role
;
1559 port
->pwr_role
= TYPEC_SINK
;
1563 switch (cap
->data
) {
1564 case TYPEC_PORT_DFP
:
1565 port
->data_role
= TYPEC_HOST
;
1567 case TYPEC_PORT_UFP
:
1568 port
->data_role
= TYPEC_DEVICE
;
1570 case TYPEC_PORT_DRD
:
1571 if (cap
->prefer_role
== TYPEC_SOURCE
)
1572 port
->data_role
= TYPEC_HOST
;
1574 port
->data_role
= TYPEC_DEVICE
;
1578 ida_init(&port
->mode_ids
);
1579 mutex_init(&port
->port_type_lock
);
1583 port
->port_type
= cap
->type
;
1584 port
->prefer_role
= cap
->prefer_role
;
1586 device_initialize(&port
->dev
);
1587 port
->dev
.class = typec_class
;
1588 port
->dev
.parent
= parent
;
1589 port
->dev
.fwnode
= cap
->fwnode
;
1590 port
->dev
.type
= &typec_port_dev_type
;
1591 dev_set_name(&port
->dev
, "port%d", id
);
1593 port
->sw
= typec_switch_get(&port
->dev
);
1594 if (IS_ERR(port
->sw
)) {
1595 ret
= PTR_ERR(port
->sw
);
1596 put_device(&port
->dev
);
1597 return ERR_PTR(ret
);
1600 port
->mux
= typec_mux_get(&port
->dev
, NULL
);
1601 if (IS_ERR(port
->mux
)) {
1602 ret
= PTR_ERR(port
->mux
);
1603 put_device(&port
->dev
);
1604 return ERR_PTR(ret
);
1607 ret
= device_add(&port
->dev
);
1609 dev_err(parent
, "failed to register port (%d)\n", ret
);
1610 put_device(&port
->dev
);
1611 return ERR_PTR(ret
);
1616 EXPORT_SYMBOL_GPL(typec_register_port
);
1619 * typec_unregister_port - Unregister a USB Type-C Port
1620 * @port: The port to be unregistered
1622 * Unregister device created with typec_register_port().
1624 void typec_unregister_port(struct typec_port
*port
)
1626 if (!IS_ERR_OR_NULL(port
))
1627 device_unregister(&port
->dev
);
1629 EXPORT_SYMBOL_GPL(typec_unregister_port
);
1631 static int __init
typec_init(void)
1635 ret
= bus_register(&typec_bus
);
1639 ret
= class_register(&typec_mux_class
);
1641 goto err_unregister_bus
;
1643 typec_class
= class_create(THIS_MODULE
, "typec");
1644 if (IS_ERR(typec_class
)) {
1645 ret
= PTR_ERR(typec_class
);
1646 goto err_unregister_mux_class
;
1651 err_unregister_mux_class
:
1652 class_unregister(&typec_mux_class
);
1655 bus_unregister(&typec_bus
);
1659 subsys_initcall(typec_init
);
1661 static void __exit
typec_exit(void)
1663 class_destroy(typec_class
);
1664 ida_destroy(&typec_index_ida
);
1665 bus_unregister(&typec_bus
);
1666 class_unregister(&typec_mux_class
);
1668 module_exit(typec_exit
);
1670 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1671 MODULE_LICENSE("GPL v2");
1672 MODULE_DESCRIPTION("USB Type-C Connector Class");