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;
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 NULL 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_roles[] = {
921 [TYPEC_SINK] = "sink",
922 [TYPEC_SOURCE] = "source",
925 static const char * const typec_data_roles[] = {
926 [TYPEC_DEVICE] = "device",
927 [TYPEC_HOST] = "host",
930 static const char * const typec_port_power_roles[] = {
931 [TYPEC_PORT_SRC] = "source",
932 [TYPEC_PORT_SNK] = "sink",
933 [TYPEC_PORT_DRP] = "dual",
936 static const char * const typec_port_data_roles[] = {
937 [TYPEC_PORT_DFP] = "host",
938 [TYPEC_PORT_UFP] = "device",
939 [TYPEC_PORT_DRD] = "dual",
942 static const char * const typec_port_types_drp[] = {
943 [TYPEC_PORT_SRC] = "dual [source] sink",
944 [TYPEC_PORT_SNK] = "dual source [sink]",
945 [TYPEC_PORT_DRP] = "[dual] source sink",
949 preferred_role_store(struct device *dev, struct device_attribute *attr,
950 const char *buf, size_t size)
952 struct typec_port *port = to_typec_port(dev);
956 if (port->cap->type != TYPEC_PORT_DRP) {
957 dev_dbg(dev, "Preferred role only supported with DRP ports\n");
961 if (!port->ops || !port->ops->try_role) {
962 dev_dbg(dev, "Setting preferred role not supported\n");
966 role = sysfs_match_string(typec_roles, buf);
968 if (sysfs_streq(buf, "none"))
969 role = TYPEC_NO_PREFERRED_ROLE;
974 ret = port->ops->try_role(port, role);
978 port->prefer_role = role;
983 preferred_role_show(struct device *dev, struct device_attribute *attr,
986 struct typec_port *port = to_typec_port(dev);
988 if (port->cap->type != TYPEC_PORT_DRP)
991 if (port->prefer_role < 0)
994 return sprintf(buf, "%s\n", typec_roles[port->prefer_role]);
996 static DEVICE_ATTR_RW(preferred_role);
998 static ssize_t data_role_store(struct device *dev,
999 struct device_attribute *attr,
1000 const char *buf, size_t size)
1002 struct typec_port *port = to_typec_port(dev);
1005 if (!port->ops || !port->ops->dr_set) {
1006 dev_dbg(dev, "data role swapping not supported\n");
1010 ret = sysfs_match_string(typec_data_roles, buf);
1014 mutex_lock(&port->port_type_lock);
1015 if (port->cap->data != TYPEC_PORT_DRD) {
1017 goto unlock_and_ret;
1020 ret = port->ops->dr_set(port, ret);
1022 goto unlock_and_ret;
1026 mutex_unlock(&port->port_type_lock);
1030 static ssize_t data_role_show(struct device *dev,
1031 struct device_attribute *attr, char *buf)
1033 struct typec_port *port = to_typec_port(dev);
1035 if (port->cap->data == TYPEC_PORT_DRD)
1036 return sprintf(buf, "%s\n", port->data_role == TYPEC_HOST ?
1037 "[host] device" : "host [device]");
1039 return sprintf(buf, "[%s]\n", typec_data_roles[port->data_role]);
1041 static DEVICE_ATTR_RW(data_role);
1043 static ssize_t power_role_store(struct device *dev,
1044 struct device_attribute *attr,
1045 const char *buf, size_t size)
1047 struct typec_port *port = to_typec_port(dev);
1050 if (!port->ops || !port->ops->pr_set) {
1051 dev_dbg(dev, "power role swapping not supported\n");
1055 if (port->pwr_opmode != TYPEC_PWR_MODE_PD) {
1056 dev_dbg(dev, "partner unable to swap power role\n");
1060 ret = sysfs_match_string(typec_roles, buf);
1064 mutex_lock(&port->port_type_lock);
1065 if (port->port_type != TYPEC_PORT_DRP) {
1066 dev_dbg(dev, "port type fixed at \"%s\"",
1067 typec_port_power_roles[port->port_type]);
1069 goto unlock_and_ret;
1072 ret = port->ops->pr_set(port, ret);
1074 goto unlock_and_ret;
1078 mutex_unlock(&port->port_type_lock);
1082 static ssize_t power_role_show(struct device *dev,
1083 struct device_attribute *attr, char *buf)
1085 struct typec_port *port = to_typec_port(dev);
1087 if (port->cap->type == TYPEC_PORT_DRP)
1088 return sprintf(buf, "%s\n", port->pwr_role == TYPEC_SOURCE ?
1089 "[source] sink" : "source [sink]");
1091 return sprintf(buf, "[%s]\n", typec_roles[port->pwr_role]);
1093 static DEVICE_ATTR_RW(power_role);
1096 port_type_store(struct device *dev, struct device_attribute *attr,
1097 const char *buf, size_t size)
1099 struct typec_port *port = to_typec_port(dev);
1101 enum typec_port_type type;
1103 if (port->cap->type != TYPEC_PORT_DRP ||
1104 !port->ops || !port->ops->port_type_set) {
1105 dev_dbg(dev, "changing port type not supported\n");
1109 ret = sysfs_match_string(typec_port_power_roles, buf);
1114 mutex_lock(&port->port_type_lock);
1116 if (port->port_type == type) {
1118 goto unlock_and_ret;
1121 ret = port->ops->port_type_set(port, type);
1123 goto unlock_and_ret;
1125 port->port_type = type;
1129 mutex_unlock(&port->port_type_lock);
1134 port_type_show(struct device *dev, struct device_attribute *attr,
1137 struct typec_port *port = to_typec_port(dev);
1139 if (port->cap->type == TYPEC_PORT_DRP)
1140 return sprintf(buf, "%s\n",
1141 typec_port_types_drp[port->port_type]);
1143 return sprintf(buf, "[%s]\n", typec_port_power_roles[port->cap->type]);
1145 static DEVICE_ATTR_RW(port_type);
1147 static const char * const typec_pwr_opmodes[] = {
1148 [TYPEC_PWR_MODE_USB] = "default",
1149 [TYPEC_PWR_MODE_1_5A] = "1.5A",
1150 [TYPEC_PWR_MODE_3_0A] = "3.0A",
1151 [TYPEC_PWR_MODE_PD] = "usb_power_delivery",
1154 static ssize_t power_operation_mode_show(struct device *dev,
1155 struct device_attribute *attr,
1158 struct typec_port *port = to_typec_port(dev);
1160 return sprintf(buf, "%s\n", typec_pwr_opmodes[port->pwr_opmode]);
1162 static DEVICE_ATTR_RO(power_operation_mode);
1164 static ssize_t vconn_source_store(struct device *dev,
1165 struct device_attribute *attr,
1166 const char *buf, size_t size)
1168 struct typec_port *port = to_typec_port(dev);
1172 if (!port->cap->pd_revision) {
1173 dev_dbg(dev, "VCONN swap depends on USB Power Delivery\n");
1177 if (!port->ops || !port->ops->vconn_set) {
1178 dev_dbg(dev, "VCONN swapping not supported\n");
1182 ret = kstrtobool(buf, &source);
1186 ret = port->ops->vconn_set(port, (enum typec_role)source);
1193 static ssize_t vconn_source_show(struct device *dev,
1194 struct device_attribute *attr, char *buf)
1196 struct typec_port *port = to_typec_port(dev);
1198 return sprintf(buf, "%s\n",
1199 port->vconn_role == TYPEC_SOURCE ? "yes" : "no");
1201 static DEVICE_ATTR_RW(vconn_source);
1203 static ssize_t supported_accessory_modes_show(struct device *dev,
1204 struct device_attribute *attr,
1207 struct typec_port *port = to_typec_port(dev);
1211 for (i = 0; i < ARRAY_SIZE(port->cap->accessory); i++) {
1212 if (port->cap->accessory[i])
1213 ret += sprintf(buf + ret, "%s ",
1214 typec_accessory_modes[port->cap->accessory[i]]);
1218 return sprintf(buf, "none\n");
1220 buf[ret - 1] = '\n';
1224 static DEVICE_ATTR_RO(supported_accessory_modes);
1226 static ssize_t usb_typec_revision_show(struct device *dev,
1227 struct device_attribute *attr,
1230 struct typec_port *port = to_typec_port(dev);
1231 u16 rev = port->cap->revision;
1233 return sprintf(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
1235 static DEVICE_ATTR_RO(usb_typec_revision);
1237 static ssize_t usb_power_delivery_revision_show(struct device *dev,
1238 struct device_attribute *attr,
1241 struct typec_port *p = to_typec_port(dev);
1243 return sprintf(buf, "%d\n", (p->cap->pd_revision >> 8) & 0xff);
1245 static DEVICE_ATTR_RO(usb_power_delivery_revision);
1247 static ssize_t orientation_show(struct device *dev,
1248 struct device_attribute *attr,
1251 struct typec_port *p = to_typec_port(dev);
1252 enum typec_orientation orientation = typec_get_orientation(p);
1254 switch (orientation) {
1255 case TYPEC_ORIENTATION_NORMAL:
1256 return sprintf(buf, "%s\n", "normal");
1257 case TYPEC_ORIENTATION_REVERSE:
1258 return sprintf(buf, "%s\n", "reverse");
1259 case TYPEC_ORIENTATION_NONE:
1261 return sprintf(buf, "%s\n", "unknown");
1264 static DEVICE_ATTR_RO(orientation);
1266 static struct attribute *typec_attrs[] = {
1267 &dev_attr_data_role.attr,
1268 &dev_attr_power_operation_mode.attr,
1269 &dev_attr_power_role.attr,
1270 &dev_attr_preferred_role.attr,
1271 &dev_attr_supported_accessory_modes.attr,
1272 &dev_attr_usb_power_delivery_revision.attr,
1273 &dev_attr_usb_typec_revision.attr,
1274 &dev_attr_vconn_source.attr,
1275 &dev_attr_port_type.attr,
1276 &dev_attr_orientation.attr,
1280 static umode_t typec_attr_is_visible(struct kobject *kobj,
1281 struct attribute *attr, int n)
1283 struct typec_port *port = to_typec_port(kobj_to_dev(kobj));
1285 if (attr == &dev_attr_data_role.attr) {
1286 if (port->cap->data != TYPEC_PORT_DRD ||
1287 !port->ops || !port->ops->dr_set)
1289 } else if (attr == &dev_attr_power_role.attr) {
1290 if (port->cap->type != TYPEC_PORT_DRP ||
1291 !port->ops || !port->ops->pr_set)
1293 } else if (attr == &dev_attr_vconn_source.attr) {
1294 if (!port->cap->pd_revision ||
1295 !port->ops || !port->ops->vconn_set)
1297 } else if (attr == &dev_attr_preferred_role.attr) {
1298 if (port->cap->type != TYPEC_PORT_DRP ||
1299 !port->ops || !port->ops->try_role)
1301 } else if (attr == &dev_attr_port_type.attr) {
1302 if (!port->ops || !port->ops->port_type_set)
1304 if (port->cap->type != TYPEC_PORT_DRP)
1306 } else if (attr == &dev_attr_orientation.attr) {
1307 if (port->cap->orientation_aware)
1315 static struct attribute_group typec_group = {
1316 .is_visible = typec_attr_is_visible,
1317 .attrs = typec_attrs,
1320 static const struct attribute_group *typec_groups[] = {
1325 static int typec_uevent(struct device *dev, struct kobj_uevent_env *env)
1329 ret = add_uevent_var(env, "TYPEC_PORT=%s", dev_name(dev));
1331 dev_err(dev, "failed to add uevent TYPEC_PORT\n");
1336 static void typec_release(struct device *dev)
1338 struct typec_port *port = to_typec_port(dev);
1340 ida_simple_remove(&typec_index_ida, port->id);
1341 ida_destroy(&port->mode_ids);
1342 typec_switch_put(port->sw);
1343 typec_mux_put(port->mux);
1348 const struct device_type typec_port_dev_type = {
1349 .name = "typec_port",
1350 .groups = typec_groups,
1351 .uevent = typec_uevent,
1352 .release = typec_release,
1355 /* --------------------------------------- */
1356 /* Driver callbacks to report role updates */
1359 * typec_set_data_role - Report data role change
1360 * @port: The USB Type-C Port where the role was changed
1361 * @role: The new data role
1363 * This routine is used by the port drivers to report data role changes.
1365 void typec_set_data_role(struct typec_port *port, enum typec_data_role role)
1367 if (port->data_role == role)
1370 port->data_role = role;
1371 sysfs_notify(&port->dev.kobj, NULL, "data_role");
1372 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1374 EXPORT_SYMBOL_GPL(typec_set_data_role);
1377 * typec_set_pwr_role - Report power role change
1378 * @port: The USB Type-C Port where the role was changed
1379 * @role: The new data role
1381 * This routine is used by the port drivers to report power role changes.
1383 void typec_set_pwr_role(struct typec_port *port, enum typec_role role)
1385 if (port->pwr_role == role)
1388 port->pwr_role = role;
1389 sysfs_notify(&port->dev.kobj, NULL, "power_role");
1390 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1392 EXPORT_SYMBOL_GPL(typec_set_pwr_role);
1395 * typec_set_vconn_role - Report VCONN source change
1396 * @port: The USB Type-C Port which VCONN role changed
1397 * @role: Source when @port is sourcing VCONN, or Sink when it's not
1399 * This routine is used by the port drivers to report if the VCONN source is
1402 void typec_set_vconn_role(struct typec_port *port, enum typec_role role)
1404 if (port->vconn_role == role)
1407 port->vconn_role = role;
1408 sysfs_notify(&port->dev.kobj, NULL, "vconn_source");
1409 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1411 EXPORT_SYMBOL_GPL(typec_set_vconn_role);
1413 static int partner_match(struct device *dev, void *data)
1415 return is_typec_partner(dev);
1419 * typec_set_pwr_opmode - Report changed power operation mode
1420 * @port: The USB Type-C Port where the mode was changed
1421 * @opmode: New power operation mode
1423 * This routine is used by the port drivers to report changed power operation
1424 * mode in @port. The modes are USB (default), 1.5A, 3.0A as defined in USB
1425 * Type-C specification, and "USB Power Delivery" when the power levels are
1426 * negotiated with methods defined in USB Power Delivery specification.
1428 void typec_set_pwr_opmode(struct typec_port *port,
1429 enum typec_pwr_opmode opmode)
1431 struct device *partner_dev;
1433 if (port->pwr_opmode == opmode)
1436 port->pwr_opmode = opmode;
1437 sysfs_notify(&port->dev.kobj, NULL, "power_operation_mode");
1438 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1440 partner_dev = device_find_child(&port->dev, NULL, partner_match);
1442 struct typec_partner *partner = to_typec_partner(partner_dev);
1444 if (opmode == TYPEC_PWR_MODE_PD && !partner->usb_pd) {
1445 partner->usb_pd = 1;
1446 sysfs_notify(&partner_dev->kobj, NULL,
1447 "supports_usb_power_delivery");
1449 put_device(partner_dev);
1452 EXPORT_SYMBOL_GPL(typec_set_pwr_opmode);
1455 * typec_find_port_power_role - Get the typec port power capability
1456 * @name: port power capability string
1458 * This routine is used to find the typec_port_type by its string name.
1460 * Returns typec_port_type if success, otherwise negative error code.
1462 int typec_find_port_power_role(const char *name)
1464 return match_string(typec_port_power_roles,
1465 ARRAY_SIZE(typec_port_power_roles), name);
1467 EXPORT_SYMBOL_GPL(typec_find_port_power_role);
1470 * typec_find_power_role - Find the typec one specific power role
1471 * @name: power role string
1473 * This routine is used to find the typec_role by its string name.
1475 * Returns typec_role if success, otherwise negative error code.
1477 int typec_find_power_role(const char *name)
1479 return match_string(typec_roles, ARRAY_SIZE(typec_roles), name);
1481 EXPORT_SYMBOL_GPL(typec_find_power_role);
1484 * typec_find_port_data_role - Get the typec port data capability
1485 * @name: port data capability string
1487 * This routine is used to find the typec_port_data by its string name.
1489 * Returns typec_port_data if success, otherwise negative error code.
1491 int typec_find_port_data_role(const char *name)
1493 return match_string(typec_port_data_roles,
1494 ARRAY_SIZE(typec_port_data_roles), name);
1496 EXPORT_SYMBOL_GPL(typec_find_port_data_role);
1498 /* ------------------------------------------ */
1499 /* API for Multiplexer/DeMultiplexer Switches */
1502 * typec_set_orientation - Set USB Type-C cable plug orientation
1503 * @port: USB Type-C Port
1504 * @orientation: USB Type-C cable plug orientation
1506 * Set cable plug orientation for @port.
1508 int typec_set_orientation(struct typec_port *port,
1509 enum typec_orientation orientation)
1513 ret = typec_switch_set(port->sw, orientation);
1517 port->orientation = orientation;
1518 sysfs_notify(&port->dev.kobj, NULL, "orientation");
1519 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1523 EXPORT_SYMBOL_GPL(typec_set_orientation);
1526 * typec_get_orientation - Get USB Type-C cable plug orientation
1527 * @port: USB Type-C Port
1529 * Get current cable plug orientation for @port.
1531 enum typec_orientation typec_get_orientation(struct typec_port *port)
1533 return port->orientation;
1535 EXPORT_SYMBOL_GPL(typec_get_orientation);
1538 * typec_set_mode - Set mode of operation for USB Type-C connector
1539 * @port: USB Type-C connector
1540 * @mode: Accessory Mode, USB Operation or Safe State
1542 * Configure @port for Accessory Mode @mode. This function will configure the
1543 * muxes needed for @mode.
1545 int typec_set_mode(struct typec_port *port, int mode)
1547 struct typec_mux_state state = { };
1551 return typec_mux_set(port->mux, &state);
1553 EXPORT_SYMBOL_GPL(typec_set_mode);
1555 /* --------------------------------------- */
1558 * typec_get_drvdata - Return private driver data pointer
1559 * @port: USB Type-C port
1561 void *typec_get_drvdata(struct typec_port *port)
1563 return dev_get_drvdata(&port->dev);
1565 EXPORT_SYMBOL_GPL(typec_get_drvdata);
1568 * typec_port_register_altmode - Register USB Type-C Port Alternate Mode
1569 * @port: USB Type-C Port that supports the alternate mode
1570 * @desc: Description of the alternate mode
1572 * This routine is used to register an alternate mode that @port is capable of
1575 * Returns handle to the alternate mode on success or ERR_PTR on failure.
1577 struct typec_altmode *
1578 typec_port_register_altmode(struct typec_port *port,
1579 const struct typec_altmode_desc *desc)
1581 struct typec_altmode *adev;
1582 struct typec_mux *mux;
1584 mux = typec_mux_get(&port->dev, desc);
1586 return ERR_CAST(mux);
1588 adev = typec_register_altmode(&port->dev, desc);
1592 to_altmode(adev)->mux = mux;
1596 EXPORT_SYMBOL_GPL(typec_port_register_altmode);
1599 * typec_register_port - Register a USB Type-C Port
1600 * @parent: Parent device
1601 * @cap: Description of the port
1603 * Registers a device for USB Type-C Port described in @cap.
1605 * Returns handle to the port on success or ERR_PTR on failure.
1607 struct typec_port *typec_register_port(struct device *parent,
1608 const struct typec_capability *cap)
1610 struct typec_port *port;
1614 port = kzalloc(sizeof(*port), GFP_KERNEL);
1616 return ERR_PTR(-ENOMEM);
1618 id = ida_simple_get(&typec_index_ida, 0, 0, GFP_KERNEL);
1624 switch (cap->type) {
1625 case TYPEC_PORT_SRC:
1626 port->pwr_role = TYPEC_SOURCE;
1627 port->vconn_role = TYPEC_SOURCE;
1629 case TYPEC_PORT_SNK:
1630 port->pwr_role = TYPEC_SINK;
1631 port->vconn_role = TYPEC_SINK;
1633 case TYPEC_PORT_DRP:
1634 if (cap->prefer_role != TYPEC_NO_PREFERRED_ROLE)
1635 port->pwr_role = cap->prefer_role;
1637 port->pwr_role = TYPEC_SINK;
1641 switch (cap->data) {
1642 case TYPEC_PORT_DFP:
1643 port->data_role = TYPEC_HOST;
1645 case TYPEC_PORT_UFP:
1646 port->data_role = TYPEC_DEVICE;
1648 case TYPEC_PORT_DRD:
1649 if (cap->prefer_role == TYPEC_SOURCE)
1650 port->data_role = TYPEC_HOST;
1652 port->data_role = TYPEC_DEVICE;
1656 ida_init(&port->mode_ids);
1657 mutex_init(&port->port_type_lock);
1660 port->ops = cap->ops;
1661 port->port_type = cap->type;
1662 port->prefer_role = cap->prefer_role;
1664 device_initialize(&port->dev);
1665 port->dev.class = typec_class;
1666 port->dev.parent = parent;
1667 port->dev.fwnode = cap->fwnode;
1668 port->dev.type = &typec_port_dev_type;
1669 dev_set_name(&port->dev, "port%d", id);
1670 dev_set_drvdata(&port->dev, cap->driver_data);
1672 port->cap = kmemdup(cap, sizeof(*cap), GFP_KERNEL);
1674 put_device(&port->dev);
1675 return ERR_PTR(-ENOMEM);
1678 port->sw = typec_switch_get(&port->dev);
1679 if (IS_ERR(port->sw)) {
1680 ret = PTR_ERR(port->sw);
1681 put_device(&port->dev);
1682 return ERR_PTR(ret);
1685 port->mux = typec_mux_get(&port->dev, NULL);
1686 if (IS_ERR(port->mux)) {
1687 ret = PTR_ERR(port->mux);
1688 put_device(&port->dev);
1689 return ERR_PTR(ret);
1692 ret = device_add(&port->dev);
1694 dev_err(parent, "failed to register port (%d)\n", ret);
1695 put_device(&port->dev);
1696 return ERR_PTR(ret);
1701 EXPORT_SYMBOL_GPL(typec_register_port);
1704 * typec_unregister_port - Unregister a USB Type-C Port
1705 * @port: The port to be unregistered
1707 * Unregister device created with typec_register_port().
1709 void typec_unregister_port(struct typec_port *port)
1711 if (!IS_ERR_OR_NULL(port))
1712 device_unregister(&port->dev);
1714 EXPORT_SYMBOL_GPL(typec_unregister_port);
1716 static int __init typec_init(void)
1720 ret = bus_register(&typec_bus);
1724 ret = class_register(&typec_mux_class);
1726 goto err_unregister_bus;
1728 typec_class = class_create(THIS_MODULE, "typec");
1729 if (IS_ERR(typec_class)) {
1730 ret = PTR_ERR(typec_class);
1731 goto err_unregister_mux_class;
1736 err_unregister_mux_class:
1737 class_unregister(&typec_mux_class);
1740 bus_unregister(&typec_bus);
1744 subsys_initcall(typec_init);
1746 static void __exit typec_exit(void)
1748 class_destroy(typec_class);
1749 ida_destroy(&typec_index_ida);
1750 bus_unregister(&typec_bus);
1751 class_unregister(&typec_mux_class);
1753 module_exit(typec_exit);
1755 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1756 MODULE_LICENSE("GPL v2");
1757 MODULE_DESCRIPTION("USB Type-C Connector Class");