1 // SPDX-License-Identifier: GPL-2.0
3 * USB Type-C Connector Class
5 * Copyright (C) 2017, Intel Corporation
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;
56 const struct typec_operations *ops;
59 #define to_typec_port(_dev_) container_of(_dev_, struct typec_port, dev)
60 #define to_typec_plug(_dev_) container_of(_dev_, struct typec_plug, dev)
61 #define to_typec_cable(_dev_) container_of(_dev_, struct typec_cable, dev)
62 #define to_typec_partner(_dev_) container_of(_dev_, struct typec_partner, dev)
64 static const struct device_type typec_partner_dev_type;
65 static const struct device_type typec_cable_dev_type;
66 static const struct device_type typec_plug_dev_type;
68 #define is_typec_partner(_dev_) (_dev_->type == &typec_partner_dev_type)
69 #define is_typec_cable(_dev_) (_dev_->type == &typec_cable_dev_type)
70 #define is_typec_plug(_dev_) (_dev_->type == &typec_plug_dev_type)
72 static DEFINE_IDA(typec_index_ida);
73 static struct class *typec_class;
75 /* ------------------------------------------------------------------------- */
76 /* Common attributes */
78 static const char * const typec_accessory_modes[] = {
79 [TYPEC_ACCESSORY_NONE] = "none",
80 [TYPEC_ACCESSORY_AUDIO] = "analog_audio",
81 [TYPEC_ACCESSORY_DEBUG] = "debug",
84 static struct usb_pd_identity *get_pd_identity(struct device *dev)
86 if (is_typec_partner(dev)) {
87 struct typec_partner *partner = to_typec_partner(dev);
89 return partner->identity;
90 } else if (is_typec_cable(dev)) {
91 struct typec_cable *cable = to_typec_cable(dev);
93 return cable->identity;
98 static ssize_t id_header_show(struct device *dev, struct device_attribute *attr,
101 struct usb_pd_identity *id = get_pd_identity(dev);
103 return sprintf(buf, "0x%08x\n", id->id_header);
105 static DEVICE_ATTR_RO(id_header);
107 static ssize_t cert_stat_show(struct device *dev, struct device_attribute *attr,
110 struct usb_pd_identity *id = get_pd_identity(dev);
112 return sprintf(buf, "0x%08x\n", id->cert_stat);
114 static DEVICE_ATTR_RO(cert_stat);
116 static ssize_t product_show(struct device *dev, struct device_attribute *attr,
119 struct usb_pd_identity *id = get_pd_identity(dev);
121 return sprintf(buf, "0x%08x\n", id->product);
123 static DEVICE_ATTR_RO(product);
125 static struct attribute *usb_pd_id_attrs[] = {
126 &dev_attr_id_header.attr,
127 &dev_attr_cert_stat.attr,
128 &dev_attr_product.attr,
132 static const struct attribute_group usb_pd_id_group = {
134 .attrs = usb_pd_id_attrs,
137 static const struct attribute_group *usb_pd_id_groups[] = {
142 static void typec_report_identity(struct device *dev)
144 sysfs_notify(&dev->kobj, "identity", "id_header");
145 sysfs_notify(&dev->kobj, "identity", "cert_stat");
146 sysfs_notify(&dev->kobj, "identity", "product");
149 /* ------------------------------------------------------------------------- */
150 /* Alternate Modes */
152 static int altmode_match(struct device *dev, void *data)
154 struct typec_altmode *adev = to_typec_altmode(dev);
155 struct typec_device_id *id = data;
157 if (!is_typec_altmode(dev))
160 return ((adev->svid == id->svid) && (adev->mode == id->mode));
163 static void typec_altmode_set_partner(struct altmode *altmode)
165 struct typec_altmode *adev = &altmode->adev;
166 struct typec_device_id id = { adev->svid, adev->mode, };
167 struct typec_port *port = typec_altmode2port(adev);
168 struct altmode *partner;
171 dev = device_find_child(&port->dev, &id, altmode_match);
175 /* Bind the port alt mode to the partner/plug alt mode. */
176 partner = to_altmode(to_typec_altmode(dev));
177 altmode->partner = partner;
179 /* Bind the partner/plug alt mode to the port alt mode. */
180 if (is_typec_plug(adev->dev.parent)) {
181 struct typec_plug *plug = to_typec_plug(adev->dev.parent);
183 partner->plug[plug->index] = altmode;
185 partner->partner = altmode;
189 static void typec_altmode_put_partner(struct altmode *altmode)
191 struct altmode *partner = altmode->partner;
192 struct typec_altmode *adev;
197 adev = &partner->adev;
199 if (is_typec_plug(adev->dev.parent)) {
200 struct typec_plug *plug = to_typec_plug(adev->dev.parent);
202 partner->plug[plug->index] = NULL;
204 partner->partner = NULL;
206 put_device(&adev->dev);
210 * typec_altmode_update_active - Report Enter/Exit mode
211 * @adev: Handle to the alternate mode
212 * @active: True when the mode has been entered
214 * If a partner or cable plug executes Enter/Exit Mode command successfully, the
215 * drivers use this routine to report the updated state of the mode.
217 void typec_altmode_update_active(struct typec_altmode *adev, bool active)
221 if (adev->active == active)
224 if (!is_typec_port(adev->dev.parent) && adev->dev.driver) {
226 module_put(adev->dev.driver->owner);
228 WARN_ON(!try_module_get(adev->dev.driver->owner));
231 adev->active = active;
232 snprintf(dir, sizeof(dir), "mode%d", adev->mode);
233 sysfs_notify(&adev->dev.kobj, dir, "active");
234 sysfs_notify(&adev->dev.kobj, NULL, "active");
235 kobject_uevent(&adev->dev.kobj, KOBJ_CHANGE);
237 EXPORT_SYMBOL_GPL(typec_altmode_update_active);
240 * typec_altmode2port - Alternate Mode to USB Type-C port
241 * @alt: The Alternate Mode
243 * Returns handle to the port that a cable plug or partner with @alt is
246 struct typec_port *typec_altmode2port(struct typec_altmode *alt)
248 if (is_typec_plug(alt->dev.parent))
249 return to_typec_port(alt->dev.parent->parent->parent);
250 if (is_typec_partner(alt->dev.parent))
251 return to_typec_port(alt->dev.parent->parent);
252 if (is_typec_port(alt->dev.parent))
253 return to_typec_port(alt->dev.parent);
257 EXPORT_SYMBOL_GPL(typec_altmode2port);
260 vdo_show(struct device *dev, struct device_attribute *attr, char *buf)
262 struct typec_altmode *alt = to_typec_altmode(dev);
264 return sprintf(buf, "0x%08x\n", alt->vdo);
266 static DEVICE_ATTR_RO(vdo);
269 description_show(struct device *dev, struct device_attribute *attr, char *buf)
271 struct typec_altmode *alt = to_typec_altmode(dev);
273 return sprintf(buf, "%s\n", alt->desc ? alt->desc : "");
275 static DEVICE_ATTR_RO(description);
278 active_show(struct device *dev, struct device_attribute *attr, char *buf)
280 struct typec_altmode *alt = to_typec_altmode(dev);
282 return sprintf(buf, "%s\n", alt->active ? "yes" : "no");
285 static ssize_t active_store(struct device *dev, struct device_attribute *attr,
286 const char *buf, size_t size)
288 struct typec_altmode *adev = to_typec_altmode(dev);
289 struct altmode *altmode = to_altmode(adev);
293 ret = kstrtobool(buf, &enter);
297 if (adev->active == enter)
300 if (is_typec_port(adev->dev.parent)) {
301 typec_altmode_update_active(adev, enter);
303 /* Make sure that the partner exits the mode before disabling */
304 if (altmode->partner && !enter && altmode->partner->adev.active)
305 typec_altmode_exit(&altmode->partner->adev);
306 } else if (altmode->partner) {
307 if (enter && !altmode->partner->adev.active) {
308 dev_warn(dev, "port has the mode disabled\n");
313 /* Note: If there is no driver, the mode will not be entered */
314 if (adev->ops && adev->ops->activate) {
315 ret = adev->ops->activate(adev, enter);
322 static DEVICE_ATTR_RW(active);
325 supported_roles_show(struct device *dev, struct device_attribute *attr,
328 struct altmode *alt = to_altmode(to_typec_altmode(dev));
331 switch (alt->roles) {
333 ret = sprintf(buf, "source\n");
336 ret = sprintf(buf, "sink\n");
340 ret = sprintf(buf, "source sink\n");
345 static DEVICE_ATTR_RO(supported_roles);
348 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
350 struct typec_altmode *adev = to_typec_altmode(dev);
352 return sprintf(buf, "%u\n", adev->mode);
354 static DEVICE_ATTR_RO(mode);
357 svid_show(struct device *dev, struct device_attribute *attr, char *buf)
359 struct typec_altmode *adev = to_typec_altmode(dev);
361 return sprintf(buf, "%04x\n", adev->svid);
363 static DEVICE_ATTR_RO(svid);
365 static struct attribute *typec_altmode_attrs[] = {
366 &dev_attr_active.attr,
373 static umode_t typec_altmode_attr_is_visible(struct kobject *kobj,
374 struct attribute *attr, int n)
376 struct typec_altmode *adev = to_typec_altmode(kobj_to_dev(kobj));
378 if (attr == &dev_attr_active.attr)
379 if (!adev->ops || !adev->ops->activate)
385 static struct attribute_group typec_altmode_group = {
386 .is_visible = typec_altmode_attr_is_visible,
387 .attrs = typec_altmode_attrs,
390 static const struct attribute_group *typec_altmode_groups[] = {
391 &typec_altmode_group,
395 static int altmode_id_get(struct device *dev)
399 if (is_typec_partner(dev))
400 ids = &to_typec_partner(dev)->mode_ids;
401 else if (is_typec_plug(dev))
402 ids = &to_typec_plug(dev)->mode_ids;
404 ids = &to_typec_port(dev)->mode_ids;
406 return ida_simple_get(ids, 0, 0, GFP_KERNEL);
409 static void altmode_id_remove(struct device *dev, int id)
413 if (is_typec_partner(dev))
414 ids = &to_typec_partner(dev)->mode_ids;
415 else if (is_typec_plug(dev))
416 ids = &to_typec_plug(dev)->mode_ids;
418 ids = &to_typec_port(dev)->mode_ids;
420 ida_simple_remove(ids, id);
423 static void typec_altmode_release(struct device *dev)
425 struct altmode *alt = to_altmode(to_typec_altmode(dev));
427 typec_altmode_put_partner(alt);
429 altmode_id_remove(alt->adev.dev.parent, alt->id);
433 const struct device_type typec_altmode_dev_type = {
434 .name = "typec_alternate_mode",
435 .groups = typec_altmode_groups,
436 .release = typec_altmode_release,
439 static struct typec_altmode *
440 typec_register_altmode(struct device *parent,
441 const struct typec_altmode_desc *desc)
443 unsigned int id = altmode_id_get(parent);
444 bool is_port = is_typec_port(parent);
448 alt = kzalloc(sizeof(*alt), GFP_KERNEL);
450 return ERR_PTR(-ENOMEM);
452 alt->adev.svid = desc->svid;
453 alt->adev.mode = desc->mode;
454 alt->adev.vdo = desc->vdo;
455 alt->roles = desc->roles;
458 alt->attrs[0] = &dev_attr_vdo.attr;
459 alt->attrs[1] = &dev_attr_description.attr;
460 alt->attrs[2] = &dev_attr_active.attr;
463 alt->attrs[3] = &dev_attr_supported_roles.attr;
464 alt->adev.active = true; /* Enabled by default */
467 sprintf(alt->group_name, "mode%d", desc->mode);
468 alt->group.name = alt->group_name;
469 alt->group.attrs = alt->attrs;
470 alt->groups[0] = &alt->group;
472 alt->adev.dev.parent = parent;
473 alt->adev.dev.groups = alt->groups;
474 alt->adev.dev.type = &typec_altmode_dev_type;
475 dev_set_name(&alt->adev.dev, "%s.%u", dev_name(parent), id);
477 /* Link partners and plugs with the ports */
479 typec_altmode_set_partner(alt);
481 /* The partners are bind to drivers */
482 if (is_typec_partner(parent))
483 alt->adev.dev.bus = &typec_bus;
485 ret = device_register(&alt->adev.dev);
487 dev_err(parent, "failed to register alternate mode (%d)\n",
489 put_device(&alt->adev.dev);
497 * typec_unregister_altmode - Unregister Alternate Mode
498 * @adev: The alternate mode to be unregistered
500 * Unregister device created with typec_partner_register_altmode(),
501 * typec_plug_register_altmode() or typec_port_register_altmode().
503 void typec_unregister_altmode(struct typec_altmode *adev)
505 if (IS_ERR_OR_NULL(adev))
507 typec_mux_put(to_altmode(adev)->mux);
508 device_unregister(&adev->dev);
510 EXPORT_SYMBOL_GPL(typec_unregister_altmode);
512 /* ------------------------------------------------------------------------- */
513 /* Type-C Partners */
515 static ssize_t accessory_mode_show(struct device *dev,
516 struct device_attribute *attr,
519 struct typec_partner *p = to_typec_partner(dev);
521 return sprintf(buf, "%s\n", typec_accessory_modes[p->accessory]);
523 static DEVICE_ATTR_RO(accessory_mode);
525 static ssize_t supports_usb_power_delivery_show(struct device *dev,
526 struct device_attribute *attr,
529 struct typec_partner *p = to_typec_partner(dev);
531 return sprintf(buf, "%s\n", p->usb_pd ? "yes" : "no");
533 static DEVICE_ATTR_RO(supports_usb_power_delivery);
535 static struct attribute *typec_partner_attrs[] = {
536 &dev_attr_accessory_mode.attr,
537 &dev_attr_supports_usb_power_delivery.attr,
540 ATTRIBUTE_GROUPS(typec_partner);
542 static void typec_partner_release(struct device *dev)
544 struct typec_partner *partner = to_typec_partner(dev);
546 ida_destroy(&partner->mode_ids);
550 static const struct device_type typec_partner_dev_type = {
551 .name = "typec_partner",
552 .groups = typec_partner_groups,
553 .release = typec_partner_release,
557 * typec_partner_set_identity - Report result from Discover Identity command
558 * @partner: The partner updated identity values
560 * This routine is used to report that the result of Discover Identity USB power
561 * delivery command has become available.
563 int typec_partner_set_identity(struct typec_partner *partner)
565 if (!partner->identity)
568 typec_report_identity(&partner->dev);
571 EXPORT_SYMBOL_GPL(typec_partner_set_identity);
574 * typec_partner_register_altmode - Register USB Type-C Partner Alternate Mode
575 * @partner: USB Type-C Partner that supports the alternate mode
576 * @desc: Description of the alternate mode
578 * This routine is used to register each alternate mode individually that
579 * @partner has listed in response to Discover SVIDs command. The modes for a
580 * SVID listed in response to Discover Modes command need to be listed in an
583 * Returns handle to the alternate mode on success or ERR_PTR on failure.
585 struct typec_altmode *
586 typec_partner_register_altmode(struct typec_partner *partner,
587 const struct typec_altmode_desc *desc)
589 return typec_register_altmode(&partner->dev, desc);
591 EXPORT_SYMBOL_GPL(typec_partner_register_altmode);
594 * typec_register_partner - Register a USB Type-C Partner
595 * @port: The USB Type-C Port the partner is connected to
596 * @desc: Description of the partner
598 * Registers a device for USB Type-C Partner described in @desc.
600 * Returns handle to the partner on success or ERR_PTR on failure.
602 struct typec_partner *typec_register_partner(struct typec_port *port,
603 struct typec_partner_desc *desc)
605 struct typec_partner *partner;
608 partner = kzalloc(sizeof(*partner), GFP_KERNEL);
610 return ERR_PTR(-ENOMEM);
612 ida_init(&partner->mode_ids);
613 partner->usb_pd = desc->usb_pd;
614 partner->accessory = desc->accessory;
616 if (desc->identity) {
618 * Creating directory for the identity only if the driver is
619 * able to provide data to it.
621 partner->dev.groups = usb_pd_id_groups;
622 partner->identity = desc->identity;
625 partner->dev.class = typec_class;
626 partner->dev.parent = &port->dev;
627 partner->dev.type = &typec_partner_dev_type;
628 dev_set_name(&partner->dev, "%s-partner", dev_name(&port->dev));
630 ret = device_register(&partner->dev);
632 dev_err(&port->dev, "failed to register partner (%d)\n", ret);
633 put_device(&partner->dev);
639 EXPORT_SYMBOL_GPL(typec_register_partner);
642 * typec_unregister_partner - Unregister a USB Type-C Partner
643 * @partner: The partner to be unregistered
645 * Unregister device created with typec_register_partner().
647 void typec_unregister_partner(struct typec_partner *partner)
649 if (!IS_ERR_OR_NULL(partner))
650 device_unregister(&partner->dev);
652 EXPORT_SYMBOL_GPL(typec_unregister_partner);
654 /* ------------------------------------------------------------------------- */
655 /* Type-C Cable Plugs */
657 static void typec_plug_release(struct device *dev)
659 struct typec_plug *plug = to_typec_plug(dev);
661 ida_destroy(&plug->mode_ids);
665 static const struct device_type typec_plug_dev_type = {
666 .name = "typec_plug",
667 .release = typec_plug_release,
671 * typec_plug_register_altmode - Register USB Type-C Cable Plug Alternate Mode
672 * @plug: USB Type-C Cable Plug that supports the alternate mode
673 * @desc: Description of the alternate mode
675 * This routine is used to register each alternate mode individually that @plug
676 * has listed in response to Discover SVIDs command. The modes for a SVID that
677 * the plug lists in response to Discover Modes command need to be listed in an
680 * Returns handle to the alternate mode on success or ERR_PTR on failure.
682 struct typec_altmode *
683 typec_plug_register_altmode(struct typec_plug *plug,
684 const struct typec_altmode_desc *desc)
686 return typec_register_altmode(&plug->dev, desc);
688 EXPORT_SYMBOL_GPL(typec_plug_register_altmode);
691 * typec_register_plug - Register a USB Type-C Cable Plug
692 * @cable: USB Type-C Cable with the plug
693 * @desc: Description of the cable plug
695 * Registers a device for USB Type-C Cable Plug described in @desc. A USB Type-C
696 * Cable Plug represents a plug with electronics in it that can response to USB
697 * Power Delivery SOP Prime or SOP Double Prime packages.
699 * Returns handle to the cable plug on success or ERR_PTR on failure.
701 struct typec_plug *typec_register_plug(struct typec_cable *cable,
702 struct typec_plug_desc *desc)
704 struct typec_plug *plug;
708 plug = kzalloc(sizeof(*plug), GFP_KERNEL);
710 return ERR_PTR(-ENOMEM);
712 sprintf(name, "plug%d", desc->index);
714 ida_init(&plug->mode_ids);
715 plug->index = desc->index;
716 plug->dev.class = typec_class;
717 plug->dev.parent = &cable->dev;
718 plug->dev.type = &typec_plug_dev_type;
719 dev_set_name(&plug->dev, "%s-%s", dev_name(cable->dev.parent), name);
721 ret = device_register(&plug->dev);
723 dev_err(&cable->dev, "failed to register plug (%d)\n", ret);
724 put_device(&plug->dev);
730 EXPORT_SYMBOL_GPL(typec_register_plug);
733 * typec_unregister_plug - Unregister a USB Type-C Cable Plug
734 * @plug: The cable plug to be unregistered
736 * Unregister device created with typec_register_plug().
738 void typec_unregister_plug(struct typec_plug *plug)
740 if (!IS_ERR_OR_NULL(plug))
741 device_unregister(&plug->dev);
743 EXPORT_SYMBOL_GPL(typec_unregister_plug);
748 type_show(struct device *dev, struct device_attribute *attr, char *buf)
750 struct typec_cable *cable = to_typec_cable(dev);
752 return sprintf(buf, "%s\n", cable->active ? "active" : "passive");
754 static DEVICE_ATTR_RO(type);
756 static const char * const typec_plug_types[] = {
757 [USB_PLUG_NONE] = "unknown",
758 [USB_PLUG_TYPE_A] = "type-a",
759 [USB_PLUG_TYPE_B] = "type-b",
760 [USB_PLUG_TYPE_C] = "type-c",
761 [USB_PLUG_CAPTIVE] = "captive",
764 static ssize_t plug_type_show(struct device *dev,
765 struct device_attribute *attr, char *buf)
767 struct typec_cable *cable = to_typec_cable(dev);
769 return sprintf(buf, "%s\n", typec_plug_types[cable->type]);
771 static DEVICE_ATTR_RO(plug_type);
773 static struct attribute *typec_cable_attrs[] = {
775 &dev_attr_plug_type.attr,
778 ATTRIBUTE_GROUPS(typec_cable);
780 static void typec_cable_release(struct device *dev)
782 struct typec_cable *cable = to_typec_cable(dev);
787 static const struct device_type typec_cable_dev_type = {
788 .name = "typec_cable",
789 .groups = typec_cable_groups,
790 .release = typec_cable_release,
793 static int cable_match(struct device *dev, void *data)
795 return is_typec_cable(dev);
799 * typec_cable_get - Get a reference to the USB Type-C cable
800 * @port: The USB Type-C Port the cable is connected to
802 * The caller must decrement the reference count with typec_cable_put() after
805 struct typec_cable *typec_cable_get(struct typec_port *port)
809 dev = device_find_child(&port->dev, NULL, cable_match);
813 return to_typec_cable(dev);
815 EXPORT_SYMBOL_GPL(typec_cable_get);
818 * typec_cable_put - Decrement the reference count on USB Type-C cable
819 * @cable: The USB Type-C cable
821 void typec_cable_put(struct typec_cable *cable)
823 put_device(&cable->dev);
825 EXPORT_SYMBOL_GPL(typec_cable_put);
828 * typec_cable_is_active - Check is the USB Type-C cable active or passive
829 * @cable: The USB Type-C Cable
831 * Return 1 if the cable is active or 0 if it's passive.
833 int typec_cable_is_active(struct typec_cable *cable)
835 return cable->active;
837 EXPORT_SYMBOL_GPL(typec_cable_is_active);
840 * typec_cable_set_identity - Report result from Discover Identity command
841 * @cable: The cable updated identity values
843 * This routine is used to report that the result of Discover Identity USB power
844 * delivery command has become available.
846 int typec_cable_set_identity(struct typec_cable *cable)
848 if (!cable->identity)
851 typec_report_identity(&cable->dev);
854 EXPORT_SYMBOL_GPL(typec_cable_set_identity);
857 * typec_register_cable - Register a USB Type-C Cable
858 * @port: The USB Type-C Port the cable is connected to
859 * @desc: Description of the cable
861 * Registers a device for USB Type-C Cable described in @desc. The cable will be
862 * parent for the optional cable plug devises.
864 * Returns handle to the cable on success or ERR_PTR on failure.
866 struct typec_cable *typec_register_cable(struct typec_port *port,
867 struct typec_cable_desc *desc)
869 struct typec_cable *cable;
872 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
874 return ERR_PTR(-ENOMEM);
876 cable->type = desc->type;
877 cable->active = desc->active;
879 if (desc->identity) {
881 * Creating directory for the identity only if the driver is
882 * able to provide data to it.
884 cable->dev.groups = usb_pd_id_groups;
885 cable->identity = desc->identity;
888 cable->dev.class = typec_class;
889 cable->dev.parent = &port->dev;
890 cable->dev.type = &typec_cable_dev_type;
891 dev_set_name(&cable->dev, "%s-cable", dev_name(&port->dev));
893 ret = device_register(&cable->dev);
895 dev_err(&port->dev, "failed to register cable (%d)\n", ret);
896 put_device(&cable->dev);
902 EXPORT_SYMBOL_GPL(typec_register_cable);
905 * typec_unregister_cable - Unregister a USB Type-C Cable
906 * @cable: The cable to be unregistered
908 * Unregister device created with typec_register_cable().
910 void typec_unregister_cable(struct typec_cable *cable)
912 if (!IS_ERR_OR_NULL(cable))
913 device_unregister(&cable->dev);
915 EXPORT_SYMBOL_GPL(typec_unregister_cable);
917 /* ------------------------------------------------------------------------- */
918 /* USB Type-C ports */
920 static const char * const typec_orientations[] = {
921 [TYPEC_ORIENTATION_NONE] = "unknown",
922 [TYPEC_ORIENTATION_NORMAL] = "normal",
923 [TYPEC_ORIENTATION_REVERSE] = "reverse",
926 static const char * const typec_roles[] = {
927 [TYPEC_SINK] = "sink",
928 [TYPEC_SOURCE] = "source",
931 static const char * const typec_data_roles[] = {
932 [TYPEC_DEVICE] = "device",
933 [TYPEC_HOST] = "host",
936 static const char * const typec_port_power_roles[] = {
937 [TYPEC_PORT_SRC] = "source",
938 [TYPEC_PORT_SNK] = "sink",
939 [TYPEC_PORT_DRP] = "dual",
942 static const char * const typec_port_data_roles[] = {
943 [TYPEC_PORT_DFP] = "host",
944 [TYPEC_PORT_UFP] = "device",
945 [TYPEC_PORT_DRD] = "dual",
948 static const char * const typec_port_types_drp[] = {
949 [TYPEC_PORT_SRC] = "dual [source] sink",
950 [TYPEC_PORT_SNK] = "dual source [sink]",
951 [TYPEC_PORT_DRP] = "[dual] source sink",
955 preferred_role_store(struct device *dev, struct device_attribute *attr,
956 const char *buf, size_t size)
958 struct typec_port *port = to_typec_port(dev);
962 if (port->cap->type != TYPEC_PORT_DRP) {
963 dev_dbg(dev, "Preferred role only supported with DRP ports\n");
967 if (!port->ops || !port->ops->try_role) {
968 dev_dbg(dev, "Setting preferred role not supported\n");
972 role = sysfs_match_string(typec_roles, buf);
974 if (sysfs_streq(buf, "none"))
975 role = TYPEC_NO_PREFERRED_ROLE;
980 ret = port->ops->try_role(port, role);
984 port->prefer_role = role;
989 preferred_role_show(struct device *dev, struct device_attribute *attr,
992 struct typec_port *port = to_typec_port(dev);
994 if (port->cap->type != TYPEC_PORT_DRP)
997 if (port->prefer_role < 0)
1000 return sprintf(buf, "%s\n", typec_roles[port->prefer_role]);
1002 static DEVICE_ATTR_RW(preferred_role);
1004 static ssize_t data_role_store(struct device *dev,
1005 struct device_attribute *attr,
1006 const char *buf, size_t size)
1008 struct typec_port *port = to_typec_port(dev);
1011 if (!port->ops || !port->ops->dr_set) {
1012 dev_dbg(dev, "data role swapping not supported\n");
1016 ret = sysfs_match_string(typec_data_roles, buf);
1020 mutex_lock(&port->port_type_lock);
1021 if (port->cap->data != TYPEC_PORT_DRD) {
1023 goto unlock_and_ret;
1026 ret = port->ops->dr_set(port, ret);
1028 goto unlock_and_ret;
1032 mutex_unlock(&port->port_type_lock);
1036 static ssize_t data_role_show(struct device *dev,
1037 struct device_attribute *attr, char *buf)
1039 struct typec_port *port = to_typec_port(dev);
1041 if (port->cap->data == TYPEC_PORT_DRD)
1042 return sprintf(buf, "%s\n", port->data_role == TYPEC_HOST ?
1043 "[host] device" : "host [device]");
1045 return sprintf(buf, "[%s]\n", typec_data_roles[port->data_role]);
1047 static DEVICE_ATTR_RW(data_role);
1049 static ssize_t power_role_store(struct device *dev,
1050 struct device_attribute *attr,
1051 const char *buf, size_t size)
1053 struct typec_port *port = to_typec_port(dev);
1056 if (!port->ops || !port->ops->pr_set) {
1057 dev_dbg(dev, "power role swapping not supported\n");
1061 if (port->pwr_opmode != TYPEC_PWR_MODE_PD) {
1062 dev_dbg(dev, "partner unable to swap power role\n");
1066 ret = sysfs_match_string(typec_roles, buf);
1070 mutex_lock(&port->port_type_lock);
1071 if (port->port_type != TYPEC_PORT_DRP) {
1072 dev_dbg(dev, "port type fixed at \"%s\"",
1073 typec_port_power_roles[port->port_type]);
1075 goto unlock_and_ret;
1078 ret = port->ops->pr_set(port, ret);
1080 goto unlock_and_ret;
1084 mutex_unlock(&port->port_type_lock);
1088 static ssize_t power_role_show(struct device *dev,
1089 struct device_attribute *attr, char *buf)
1091 struct typec_port *port = to_typec_port(dev);
1093 if (port->cap->type == TYPEC_PORT_DRP)
1094 return sprintf(buf, "%s\n", port->pwr_role == TYPEC_SOURCE ?
1095 "[source] sink" : "source [sink]");
1097 return sprintf(buf, "[%s]\n", typec_roles[port->pwr_role]);
1099 static DEVICE_ATTR_RW(power_role);
1102 port_type_store(struct device *dev, struct device_attribute *attr,
1103 const char *buf, size_t size)
1105 struct typec_port *port = to_typec_port(dev);
1107 enum typec_port_type type;
1109 if (port->cap->type != TYPEC_PORT_DRP ||
1110 !port->ops || !port->ops->port_type_set) {
1111 dev_dbg(dev, "changing port type not supported\n");
1115 ret = sysfs_match_string(typec_port_power_roles, buf);
1120 mutex_lock(&port->port_type_lock);
1122 if (port->port_type == type) {
1124 goto unlock_and_ret;
1127 ret = port->ops->port_type_set(port, type);
1129 goto unlock_and_ret;
1131 port->port_type = type;
1135 mutex_unlock(&port->port_type_lock);
1140 port_type_show(struct device *dev, struct device_attribute *attr,
1143 struct typec_port *port = to_typec_port(dev);
1145 if (port->cap->type == TYPEC_PORT_DRP)
1146 return sprintf(buf, "%s\n",
1147 typec_port_types_drp[port->port_type]);
1149 return sprintf(buf, "[%s]\n", typec_port_power_roles[port->cap->type]);
1151 static DEVICE_ATTR_RW(port_type);
1153 static const char * const typec_pwr_opmodes[] = {
1154 [TYPEC_PWR_MODE_USB] = "default",
1155 [TYPEC_PWR_MODE_1_5A] = "1.5A",
1156 [TYPEC_PWR_MODE_3_0A] = "3.0A",
1157 [TYPEC_PWR_MODE_PD] = "usb_power_delivery",
1160 static ssize_t power_operation_mode_show(struct device *dev,
1161 struct device_attribute *attr,
1164 struct typec_port *port = to_typec_port(dev);
1166 return sprintf(buf, "%s\n", typec_pwr_opmodes[port->pwr_opmode]);
1168 static DEVICE_ATTR_RO(power_operation_mode);
1170 static ssize_t vconn_source_store(struct device *dev,
1171 struct device_attribute *attr,
1172 const char *buf, size_t size)
1174 struct typec_port *port = to_typec_port(dev);
1178 if (!port->cap->pd_revision) {
1179 dev_dbg(dev, "VCONN swap depends on USB Power Delivery\n");
1183 if (!port->ops || !port->ops->vconn_set) {
1184 dev_dbg(dev, "VCONN swapping not supported\n");
1188 ret = kstrtobool(buf, &source);
1192 ret = port->ops->vconn_set(port, (enum typec_role)source);
1199 static ssize_t vconn_source_show(struct device *dev,
1200 struct device_attribute *attr, char *buf)
1202 struct typec_port *port = to_typec_port(dev);
1204 return sprintf(buf, "%s\n",
1205 port->vconn_role == TYPEC_SOURCE ? "yes" : "no");
1207 static DEVICE_ATTR_RW(vconn_source);
1209 static ssize_t supported_accessory_modes_show(struct device *dev,
1210 struct device_attribute *attr,
1213 struct typec_port *port = to_typec_port(dev);
1217 for (i = 0; i < ARRAY_SIZE(port->cap->accessory); i++) {
1218 if (port->cap->accessory[i])
1219 ret += sprintf(buf + ret, "%s ",
1220 typec_accessory_modes[port->cap->accessory[i]]);
1224 return sprintf(buf, "none\n");
1226 buf[ret - 1] = '\n';
1230 static DEVICE_ATTR_RO(supported_accessory_modes);
1232 static ssize_t usb_typec_revision_show(struct device *dev,
1233 struct device_attribute *attr,
1236 struct typec_port *port = to_typec_port(dev);
1237 u16 rev = port->cap->revision;
1239 return sprintf(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
1241 static DEVICE_ATTR_RO(usb_typec_revision);
1243 static ssize_t usb_power_delivery_revision_show(struct device *dev,
1244 struct device_attribute *attr,
1247 struct typec_port *p = to_typec_port(dev);
1249 return sprintf(buf, "%d\n", (p->cap->pd_revision >> 8) & 0xff);
1251 static DEVICE_ATTR_RO(usb_power_delivery_revision);
1253 static ssize_t orientation_show(struct device *dev,
1254 struct device_attribute *attr,
1257 struct typec_port *port = to_typec_port(dev);
1259 return sprintf(buf, "%s\n", typec_orientations[port->orientation]);
1261 static DEVICE_ATTR_RO(orientation);
1263 static struct attribute *typec_attrs[] = {
1264 &dev_attr_data_role.attr,
1265 &dev_attr_power_operation_mode.attr,
1266 &dev_attr_power_role.attr,
1267 &dev_attr_preferred_role.attr,
1268 &dev_attr_supported_accessory_modes.attr,
1269 &dev_attr_usb_power_delivery_revision.attr,
1270 &dev_attr_usb_typec_revision.attr,
1271 &dev_attr_vconn_source.attr,
1272 &dev_attr_port_type.attr,
1273 &dev_attr_orientation.attr,
1277 static umode_t typec_attr_is_visible(struct kobject *kobj,
1278 struct attribute *attr, int n)
1280 struct typec_port *port = to_typec_port(kobj_to_dev(kobj));
1282 if (attr == &dev_attr_data_role.attr) {
1283 if (port->cap->data != TYPEC_PORT_DRD ||
1284 !port->ops || !port->ops->dr_set)
1286 } else if (attr == &dev_attr_power_role.attr) {
1287 if (port->cap->type != TYPEC_PORT_DRP ||
1288 !port->ops || !port->ops->pr_set)
1290 } else if (attr == &dev_attr_vconn_source.attr) {
1291 if (!port->cap->pd_revision ||
1292 !port->ops || !port->ops->vconn_set)
1294 } else if (attr == &dev_attr_preferred_role.attr) {
1295 if (port->cap->type != TYPEC_PORT_DRP ||
1296 !port->ops || !port->ops->try_role)
1298 } else if (attr == &dev_attr_port_type.attr) {
1299 if (!port->ops || !port->ops->port_type_set)
1301 if (port->cap->type != TYPEC_PORT_DRP)
1303 } else if (attr == &dev_attr_orientation.attr) {
1304 if (port->cap->orientation_aware)
1312 static struct attribute_group typec_group = {
1313 .is_visible = typec_attr_is_visible,
1314 .attrs = typec_attrs,
1317 static const struct attribute_group *typec_groups[] = {
1322 static int typec_uevent(struct device *dev, struct kobj_uevent_env *env)
1326 ret = add_uevent_var(env, "TYPEC_PORT=%s", dev_name(dev));
1328 dev_err(dev, "failed to add uevent TYPEC_PORT\n");
1333 static void typec_release(struct device *dev)
1335 struct typec_port *port = to_typec_port(dev);
1337 ida_simple_remove(&typec_index_ida, port->id);
1338 ida_destroy(&port->mode_ids);
1339 typec_switch_put(port->sw);
1340 typec_mux_put(port->mux);
1345 const struct device_type typec_port_dev_type = {
1346 .name = "typec_port",
1347 .groups = typec_groups,
1348 .uevent = typec_uevent,
1349 .release = typec_release,
1352 /* --------------------------------------- */
1353 /* Driver callbacks to report role updates */
1356 * typec_set_data_role - Report data role change
1357 * @port: The USB Type-C Port where the role was changed
1358 * @role: The new data role
1360 * This routine is used by the port drivers to report data role changes.
1362 void typec_set_data_role(struct typec_port *port, enum typec_data_role role)
1364 if (port->data_role == role)
1367 port->data_role = role;
1368 sysfs_notify(&port->dev.kobj, NULL, "data_role");
1369 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1371 EXPORT_SYMBOL_GPL(typec_set_data_role);
1374 * typec_set_pwr_role - Report power role change
1375 * @port: The USB Type-C Port where the role was changed
1376 * @role: The new data role
1378 * This routine is used by the port drivers to report power role changes.
1380 void typec_set_pwr_role(struct typec_port *port, enum typec_role role)
1382 if (port->pwr_role == role)
1385 port->pwr_role = role;
1386 sysfs_notify(&port->dev.kobj, NULL, "power_role");
1387 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1389 EXPORT_SYMBOL_GPL(typec_set_pwr_role);
1392 * typec_set_vconn_role - Report VCONN source change
1393 * @port: The USB Type-C Port which VCONN role changed
1394 * @role: Source when @port is sourcing VCONN, or Sink when it's not
1396 * This routine is used by the port drivers to report if the VCONN source is
1399 void typec_set_vconn_role(struct typec_port *port, enum typec_role role)
1401 if (port->vconn_role == role)
1404 port->vconn_role = role;
1405 sysfs_notify(&port->dev.kobj, NULL, "vconn_source");
1406 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1408 EXPORT_SYMBOL_GPL(typec_set_vconn_role);
1410 static int partner_match(struct device *dev, void *data)
1412 return is_typec_partner(dev);
1416 * typec_set_pwr_opmode - Report changed power operation mode
1417 * @port: The USB Type-C Port where the mode was changed
1418 * @opmode: New power operation mode
1420 * This routine is used by the port drivers to report changed power operation
1421 * mode in @port. The modes are USB (default), 1.5A, 3.0A as defined in USB
1422 * Type-C specification, and "USB Power Delivery" when the power levels are
1423 * negotiated with methods defined in USB Power Delivery specification.
1425 void typec_set_pwr_opmode(struct typec_port *port,
1426 enum typec_pwr_opmode opmode)
1428 struct device *partner_dev;
1430 if (port->pwr_opmode == opmode)
1433 port->pwr_opmode = opmode;
1434 sysfs_notify(&port->dev.kobj, NULL, "power_operation_mode");
1435 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1437 partner_dev = device_find_child(&port->dev, NULL, partner_match);
1439 struct typec_partner *partner = to_typec_partner(partner_dev);
1441 if (opmode == TYPEC_PWR_MODE_PD && !partner->usb_pd) {
1442 partner->usb_pd = 1;
1443 sysfs_notify(&partner_dev->kobj, NULL,
1444 "supports_usb_power_delivery");
1446 put_device(partner_dev);
1449 EXPORT_SYMBOL_GPL(typec_set_pwr_opmode);
1452 * typec_find_pwr_opmode - Get the typec power operation mode capability
1453 * @name: power operation mode string
1455 * This routine is used to find the typec_pwr_opmode by its string @name.
1457 * Returns typec_pwr_opmode if success, otherwise negative error code.
1459 int typec_find_pwr_opmode(const char *name)
1461 return match_string(typec_pwr_opmodes,
1462 ARRAY_SIZE(typec_pwr_opmodes), name);
1464 EXPORT_SYMBOL_GPL(typec_find_pwr_opmode);
1467 * typec_find_orientation - Convert orientation string to enum typec_orientation
1468 * @name: Orientation string
1470 * This routine is used to find the typec_orientation by its string name @name.
1472 * Returns the orientation value on success, otherwise negative error code.
1474 int typec_find_orientation(const char *name)
1476 return match_string(typec_orientations, ARRAY_SIZE(typec_orientations),
1479 EXPORT_SYMBOL_GPL(typec_find_orientation);
1482 * typec_find_port_power_role - Get the typec port power capability
1483 * @name: port power capability string
1485 * This routine is used to find the typec_port_type by its string name.
1487 * Returns typec_port_type if success, otherwise negative error code.
1489 int typec_find_port_power_role(const char *name)
1491 return match_string(typec_port_power_roles,
1492 ARRAY_SIZE(typec_port_power_roles), name);
1494 EXPORT_SYMBOL_GPL(typec_find_port_power_role);
1497 * typec_find_power_role - Find the typec one specific power role
1498 * @name: power role string
1500 * This routine is used to find the typec_role by its string name.
1502 * Returns typec_role if success, otherwise negative error code.
1504 int typec_find_power_role(const char *name)
1506 return match_string(typec_roles, ARRAY_SIZE(typec_roles), name);
1508 EXPORT_SYMBOL_GPL(typec_find_power_role);
1511 * typec_find_port_data_role - Get the typec port data capability
1512 * @name: port data capability string
1514 * This routine is used to find the typec_port_data by its string name.
1516 * Returns typec_port_data if success, otherwise negative error code.
1518 int typec_find_port_data_role(const char *name)
1520 return match_string(typec_port_data_roles,
1521 ARRAY_SIZE(typec_port_data_roles), name);
1523 EXPORT_SYMBOL_GPL(typec_find_port_data_role);
1525 /* ------------------------------------------ */
1526 /* API for Multiplexer/DeMultiplexer Switches */
1529 * typec_set_orientation - Set USB Type-C cable plug orientation
1530 * @port: USB Type-C Port
1531 * @orientation: USB Type-C cable plug orientation
1533 * Set cable plug orientation for @port.
1535 int typec_set_orientation(struct typec_port *port,
1536 enum typec_orientation orientation)
1540 ret = typec_switch_set(port->sw, orientation);
1544 port->orientation = orientation;
1545 sysfs_notify(&port->dev.kobj, NULL, "orientation");
1546 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1550 EXPORT_SYMBOL_GPL(typec_set_orientation);
1553 * typec_get_orientation - Get USB Type-C cable plug orientation
1554 * @port: USB Type-C Port
1556 * Get current cable plug orientation for @port.
1558 enum typec_orientation typec_get_orientation(struct typec_port *port)
1560 return port->orientation;
1562 EXPORT_SYMBOL_GPL(typec_get_orientation);
1565 * typec_set_mode - Set mode of operation for USB Type-C connector
1566 * @port: USB Type-C connector
1567 * @mode: Accessory Mode, USB Operation or Safe State
1569 * Configure @port for Accessory Mode @mode. This function will configure the
1570 * muxes needed for @mode.
1572 int typec_set_mode(struct typec_port *port, int mode)
1574 struct typec_mux_state state = { };
1578 return typec_mux_set(port->mux, &state);
1580 EXPORT_SYMBOL_GPL(typec_set_mode);
1582 /* --------------------------------------- */
1585 * typec_get_drvdata - Return private driver data pointer
1586 * @port: USB Type-C port
1588 void *typec_get_drvdata(struct typec_port *port)
1590 return dev_get_drvdata(&port->dev);
1592 EXPORT_SYMBOL_GPL(typec_get_drvdata);
1595 * typec_port_register_altmode - Register USB Type-C Port Alternate Mode
1596 * @port: USB Type-C Port that supports the alternate mode
1597 * @desc: Description of the alternate mode
1599 * This routine is used to register an alternate mode that @port is capable of
1602 * Returns handle to the alternate mode on success or ERR_PTR on failure.
1604 struct typec_altmode *
1605 typec_port_register_altmode(struct typec_port *port,
1606 const struct typec_altmode_desc *desc)
1608 struct typec_altmode *adev;
1609 struct typec_mux *mux;
1611 mux = typec_mux_get(&port->dev, desc);
1613 return ERR_CAST(mux);
1615 adev = typec_register_altmode(&port->dev, desc);
1619 to_altmode(adev)->mux = mux;
1623 EXPORT_SYMBOL_GPL(typec_port_register_altmode);
1626 * typec_register_port - Register a USB Type-C Port
1627 * @parent: Parent device
1628 * @cap: Description of the port
1630 * Registers a device for USB Type-C Port described in @cap.
1632 * Returns handle to the port on success or ERR_PTR on failure.
1634 struct typec_port *typec_register_port(struct device *parent,
1635 const struct typec_capability *cap)
1637 struct typec_port *port;
1641 port = kzalloc(sizeof(*port), GFP_KERNEL);
1643 return ERR_PTR(-ENOMEM);
1645 id = ida_simple_get(&typec_index_ida, 0, 0, GFP_KERNEL);
1651 switch (cap->type) {
1652 case TYPEC_PORT_SRC:
1653 port->pwr_role = TYPEC_SOURCE;
1654 port->vconn_role = TYPEC_SOURCE;
1656 case TYPEC_PORT_SNK:
1657 port->pwr_role = TYPEC_SINK;
1658 port->vconn_role = TYPEC_SINK;
1660 case TYPEC_PORT_DRP:
1661 if (cap->prefer_role != TYPEC_NO_PREFERRED_ROLE)
1662 port->pwr_role = cap->prefer_role;
1664 port->pwr_role = TYPEC_SINK;
1668 switch (cap->data) {
1669 case TYPEC_PORT_DFP:
1670 port->data_role = TYPEC_HOST;
1672 case TYPEC_PORT_UFP:
1673 port->data_role = TYPEC_DEVICE;
1675 case TYPEC_PORT_DRD:
1676 if (cap->prefer_role == TYPEC_SOURCE)
1677 port->data_role = TYPEC_HOST;
1679 port->data_role = TYPEC_DEVICE;
1683 ida_init(&port->mode_ids);
1684 mutex_init(&port->port_type_lock);
1687 port->ops = cap->ops;
1688 port->port_type = cap->type;
1689 port->prefer_role = cap->prefer_role;
1691 device_initialize(&port->dev);
1692 port->dev.class = typec_class;
1693 port->dev.parent = parent;
1694 port->dev.fwnode = cap->fwnode;
1695 port->dev.type = &typec_port_dev_type;
1696 dev_set_name(&port->dev, "port%d", id);
1697 dev_set_drvdata(&port->dev, cap->driver_data);
1699 port->cap = kmemdup(cap, sizeof(*cap), GFP_KERNEL);
1701 put_device(&port->dev);
1702 return ERR_PTR(-ENOMEM);
1705 port->sw = typec_switch_get(&port->dev);
1706 if (IS_ERR(port->sw)) {
1707 ret = PTR_ERR(port->sw);
1708 put_device(&port->dev);
1709 return ERR_PTR(ret);
1712 port->mux = typec_mux_get(&port->dev, NULL);
1713 if (IS_ERR(port->mux)) {
1714 ret = PTR_ERR(port->mux);
1715 put_device(&port->dev);
1716 return ERR_PTR(ret);
1719 ret = device_add(&port->dev);
1721 dev_err(parent, "failed to register port (%d)\n", ret);
1722 put_device(&port->dev);
1723 return ERR_PTR(ret);
1728 EXPORT_SYMBOL_GPL(typec_register_port);
1731 * typec_unregister_port - Unregister a USB Type-C Port
1732 * @port: The port to be unregistered
1734 * Unregister device created with typec_register_port().
1736 void typec_unregister_port(struct typec_port *port)
1738 if (!IS_ERR_OR_NULL(port))
1739 device_unregister(&port->dev);
1741 EXPORT_SYMBOL_GPL(typec_unregister_port);
1743 static int __init typec_init(void)
1747 ret = bus_register(&typec_bus);
1751 ret = class_register(&typec_mux_class);
1753 goto err_unregister_bus;
1755 typec_class = class_create(THIS_MODULE, "typec");
1756 if (IS_ERR(typec_class)) {
1757 ret = PTR_ERR(typec_class);
1758 goto err_unregister_mux_class;
1763 err_unregister_mux_class:
1764 class_unregister(&typec_mux_class);
1767 bus_unregister(&typec_bus);
1771 subsys_initcall(typec_init);
1773 static void __exit typec_exit(void)
1775 class_destroy(typec_class);
1776 ida_destroy(&typec_index_ida);
1777 bus_unregister(&typec_bus);
1778 class_unregister(&typec_mux_class);
1780 module_exit(typec_exit);
1783 MODULE_LICENSE("GPL v2");
1784 MODULE_DESCRIPTION("USB Type-C Connector Class");