1 // SPDX-License-Identifier: GPL-2.0
3 * USB Type-C Connector Class
5 * Copyright (C) 2017, Intel Corporation
9 #include <linux/module.h>
10 #include <linux/mutex.h>
11 #include <linux/property.h>
12 #include <linux/slab.h>
13 #include <linux/usb/pd_vdo.h>
14 #include <linux/usb/typec_mux.h>
15 #include <linux/usb/typec_retimer.h>
21 static DEFINE_IDA(typec_index_ida);
23 struct class typec_class = {
27 /* ------------------------------------------------------------------------- */
28 /* Common attributes */
30 static const char * const typec_accessory_modes[] = {
31 [TYPEC_ACCESSORY_NONE] = "none",
32 [TYPEC_ACCESSORY_AUDIO] = "analog_audio",
33 [TYPEC_ACCESSORY_DEBUG] = "debug",
36 /* Product types defined in USB PD Specification R3.0 V2.0 */
37 static const char * const product_type_ufp[8] = {
38 [IDH_PTYPE_NOT_UFP] = "not_ufp",
39 [IDH_PTYPE_HUB] = "hub",
40 [IDH_PTYPE_PERIPH] = "peripheral",
41 [IDH_PTYPE_PSD] = "psd",
42 [IDH_PTYPE_AMA] = "ama",
45 static const char * const product_type_dfp[8] = {
46 [IDH_PTYPE_NOT_DFP] = "not_dfp",
47 [IDH_PTYPE_DFP_HUB] = "hub",
48 [IDH_PTYPE_DFP_HOST] = "host",
49 [IDH_PTYPE_DFP_PB] = "power_brick",
52 static const char * const product_type_cable[8] = {
53 [IDH_PTYPE_NOT_CABLE] = "not_cable",
54 [IDH_PTYPE_PCABLE] = "passive",
55 [IDH_PTYPE_ACABLE] = "active",
56 [IDH_PTYPE_VPD] = "vpd",
59 static struct usb_pd_identity *get_pd_identity(struct device *dev)
61 if (is_typec_partner(dev)) {
62 struct typec_partner *partner = to_typec_partner(dev);
64 return partner->identity;
65 } else if (is_typec_cable(dev)) {
66 struct typec_cable *cable = to_typec_cable(dev);
68 return cable->identity;
73 static const char *get_pd_product_type(struct device *dev)
75 struct typec_port *port = to_typec_port(dev->parent);
76 struct usb_pd_identity *id = get_pd_identity(dev);
77 const char *ptype = NULL;
79 if (is_typec_partner(dev)) {
83 if (port->data_role == TYPEC_HOST)
84 ptype = product_type_ufp[PD_IDH_PTYPE(id->id_header)];
86 ptype = product_type_dfp[PD_IDH_DFP_PTYPE(id->id_header)];
87 } else if (is_typec_cable(dev)) {
89 ptype = product_type_cable[PD_IDH_PTYPE(id->id_header)];
91 ptype = to_typec_cable(dev)->active ?
92 product_type_cable[IDH_PTYPE_ACABLE] :
93 product_type_cable[IDH_PTYPE_PCABLE];
99 static ssize_t id_header_show(struct device *dev, struct device_attribute *attr,
102 struct usb_pd_identity *id = get_pd_identity(dev);
104 return sprintf(buf, "0x%08x\n", id->id_header);
106 static DEVICE_ATTR_RO(id_header);
108 static ssize_t cert_stat_show(struct device *dev, struct device_attribute *attr,
111 struct usb_pd_identity *id = get_pd_identity(dev);
113 return sprintf(buf, "0x%08x\n", id->cert_stat);
115 static DEVICE_ATTR_RO(cert_stat);
117 static ssize_t product_show(struct device *dev, struct device_attribute *attr,
120 struct usb_pd_identity *id = get_pd_identity(dev);
122 return sprintf(buf, "0x%08x\n", id->product);
124 static DEVICE_ATTR_RO(product);
126 static ssize_t product_type_vdo1_show(struct device *dev, struct device_attribute *attr,
129 struct usb_pd_identity *id = get_pd_identity(dev);
131 return sysfs_emit(buf, "0x%08x\n", id->vdo[0]);
133 static DEVICE_ATTR_RO(product_type_vdo1);
135 static ssize_t product_type_vdo2_show(struct device *dev, struct device_attribute *attr,
138 struct usb_pd_identity *id = get_pd_identity(dev);
140 return sysfs_emit(buf, "0x%08x\n", id->vdo[1]);
142 static DEVICE_ATTR_RO(product_type_vdo2);
144 static ssize_t product_type_vdo3_show(struct device *dev, struct device_attribute *attr,
147 struct usb_pd_identity *id = get_pd_identity(dev);
149 return sysfs_emit(buf, "0x%08x\n", id->vdo[2]);
151 static DEVICE_ATTR_RO(product_type_vdo3);
153 static struct attribute *usb_pd_id_attrs[] = {
154 &dev_attr_id_header.attr,
155 &dev_attr_cert_stat.attr,
156 &dev_attr_product.attr,
157 &dev_attr_product_type_vdo1.attr,
158 &dev_attr_product_type_vdo2.attr,
159 &dev_attr_product_type_vdo3.attr,
163 static const struct attribute_group usb_pd_id_group = {
165 .attrs = usb_pd_id_attrs,
168 static const struct attribute_group *usb_pd_id_groups[] = {
173 static void typec_product_type_notify(struct device *dev)
178 ptype = get_pd_product_type(dev);
182 sysfs_notify(&dev->kobj, NULL, "type");
184 envp[0] = kasprintf(GFP_KERNEL, "PRODUCT_TYPE=%s", ptype);
188 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
192 static void typec_report_identity(struct device *dev)
194 sysfs_notify(&dev->kobj, "identity", "id_header");
195 sysfs_notify(&dev->kobj, "identity", "cert_stat");
196 sysfs_notify(&dev->kobj, "identity", "product");
197 sysfs_notify(&dev->kobj, "identity", "product_type_vdo1");
198 sysfs_notify(&dev->kobj, "identity", "product_type_vdo2");
199 sysfs_notify(&dev->kobj, "identity", "product_type_vdo3");
200 typec_product_type_notify(dev);
204 type_show(struct device *dev, struct device_attribute *attr, char *buf)
208 ptype = get_pd_product_type(dev);
212 return sysfs_emit(buf, "%s\n", ptype);
214 static DEVICE_ATTR_RO(type);
216 static ssize_t usb_power_delivery_revision_show(struct device *dev,
217 struct device_attribute *attr,
219 static DEVICE_ATTR_RO(usb_power_delivery_revision);
221 /* ------------------------------------------------------------------------- */
222 /* Alternate Modes */
224 static int altmode_match(struct device *dev, void *data)
226 struct typec_altmode *adev = to_typec_altmode(dev);
227 struct typec_device_id *id = data;
229 if (!is_typec_altmode(dev))
232 return ((adev->svid == id->svid) && (adev->mode == id->mode));
235 static void typec_altmode_set_partner(struct altmode *altmode)
237 struct typec_altmode *adev = &altmode->adev;
238 struct typec_device_id id = { adev->svid, adev->mode, };
239 struct typec_port *port = typec_altmode2port(adev);
240 struct altmode *partner;
243 dev = device_find_child(&port->dev, &id, altmode_match);
247 /* Bind the port alt mode to the partner/plug alt mode. */
248 partner = to_altmode(to_typec_altmode(dev));
249 altmode->partner = partner;
251 /* Bind the partner/plug alt mode to the port alt mode. */
252 if (is_typec_plug(adev->dev.parent)) {
253 struct typec_plug *plug = to_typec_plug(adev->dev.parent);
255 partner->plug[plug->index] = altmode;
257 partner->partner = altmode;
261 static void typec_altmode_put_partner(struct altmode *altmode)
263 struct altmode *partner = altmode->partner;
264 struct typec_altmode *adev;
269 adev = &partner->adev;
271 if (is_typec_plug(adev->dev.parent)) {
272 struct typec_plug *plug = to_typec_plug(adev->dev.parent);
274 partner->plug[plug->index] = NULL;
276 partner->partner = NULL;
278 put_device(&adev->dev);
282 * typec_altmode_update_active - Report Enter/Exit mode
283 * @adev: Handle to the alternate mode
284 * @active: True when the mode has been entered
286 * If a partner or cable plug executes Enter/Exit Mode command successfully, the
287 * drivers use this routine to report the updated state of the mode.
289 void typec_altmode_update_active(struct typec_altmode *adev, bool active)
293 if (adev->active == active)
296 if (!is_typec_port(adev->dev.parent) && adev->dev.driver) {
298 module_put(adev->dev.driver->owner);
300 WARN_ON(!try_module_get(adev->dev.driver->owner));
303 adev->active = active;
304 snprintf(dir, sizeof(dir), "mode%d", adev->mode);
305 sysfs_notify(&adev->dev.kobj, dir, "active");
306 sysfs_notify(&adev->dev.kobj, NULL, "active");
307 kobject_uevent(&adev->dev.kobj, KOBJ_CHANGE);
309 EXPORT_SYMBOL_GPL(typec_altmode_update_active);
312 * typec_altmode2port - Alternate Mode to USB Type-C port
313 * @alt: The Alternate Mode
315 * Returns handle to the port that a cable plug or partner with @alt is
318 struct typec_port *typec_altmode2port(struct typec_altmode *alt)
320 if (is_typec_plug(alt->dev.parent))
321 return to_typec_port(alt->dev.parent->parent->parent);
322 if (is_typec_partner(alt->dev.parent))
323 return to_typec_port(alt->dev.parent->parent);
324 if (is_typec_port(alt->dev.parent))
325 return to_typec_port(alt->dev.parent);
329 EXPORT_SYMBOL_GPL(typec_altmode2port);
332 vdo_show(struct device *dev, struct device_attribute *attr, char *buf)
334 struct typec_altmode *alt = to_typec_altmode(dev);
336 return sprintf(buf, "0x%08x\n", alt->vdo);
338 static DEVICE_ATTR_RO(vdo);
341 description_show(struct device *dev, struct device_attribute *attr, char *buf)
343 struct typec_altmode *alt = to_typec_altmode(dev);
345 return sprintf(buf, "%s\n", alt->desc ? alt->desc : "");
347 static DEVICE_ATTR_RO(description);
350 active_show(struct device *dev, struct device_attribute *attr, char *buf)
352 struct typec_altmode *alt = to_typec_altmode(dev);
354 return sprintf(buf, "%s\n", alt->active ? "yes" : "no");
357 static ssize_t active_store(struct device *dev, struct device_attribute *attr,
358 const char *buf, size_t size)
360 struct typec_altmode *adev = to_typec_altmode(dev);
361 struct altmode *altmode = to_altmode(adev);
365 ret = kstrtobool(buf, &enter);
369 if (adev->active == enter)
372 if (is_typec_port(adev->dev.parent)) {
373 typec_altmode_update_active(adev, enter);
375 /* Make sure that the partner exits the mode before disabling */
376 if (altmode->partner && !enter && altmode->partner->adev.active)
377 typec_altmode_exit(&altmode->partner->adev);
378 } else if (altmode->partner) {
379 if (enter && !altmode->partner->adev.active) {
380 dev_warn(dev, "port has the mode disabled\n");
385 /* Note: If there is no driver, the mode will not be entered */
386 if (adev->ops && adev->ops->activate) {
387 ret = adev->ops->activate(adev, enter);
394 static DEVICE_ATTR_RW(active);
397 supported_roles_show(struct device *dev, struct device_attribute *attr,
400 struct altmode *alt = to_altmode(to_typec_altmode(dev));
403 switch (alt->roles) {
405 ret = sprintf(buf, "source\n");
408 ret = sprintf(buf, "sink\n");
412 ret = sprintf(buf, "source sink\n");
417 static DEVICE_ATTR_RO(supported_roles);
420 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
422 struct typec_altmode *adev = to_typec_altmode(dev);
424 return sprintf(buf, "%u\n", adev->mode);
426 static DEVICE_ATTR_RO(mode);
429 svid_show(struct device *dev, struct device_attribute *attr, char *buf)
431 struct typec_altmode *adev = to_typec_altmode(dev);
433 return sprintf(buf, "%04x\n", adev->svid);
435 static DEVICE_ATTR_RO(svid);
437 static struct attribute *typec_altmode_attrs[] = {
438 &dev_attr_active.attr,
445 static umode_t typec_altmode_attr_is_visible(struct kobject *kobj,
446 struct attribute *attr, int n)
448 struct typec_altmode *adev = to_typec_altmode(kobj_to_dev(kobj));
450 if (attr == &dev_attr_active.attr)
451 if (!adev->ops || !adev->ops->activate)
457 static const struct attribute_group typec_altmode_group = {
458 .is_visible = typec_altmode_attr_is_visible,
459 .attrs = typec_altmode_attrs,
462 static const struct attribute_group *typec_altmode_groups[] = {
463 &typec_altmode_group,
467 static int altmode_id_get(struct device *dev)
471 if (is_typec_partner(dev))
472 ids = &to_typec_partner(dev)->mode_ids;
473 else if (is_typec_plug(dev))
474 ids = &to_typec_plug(dev)->mode_ids;
476 ids = &to_typec_port(dev)->mode_ids;
478 return ida_simple_get(ids, 0, 0, GFP_KERNEL);
481 static void altmode_id_remove(struct device *dev, int id)
485 if (is_typec_partner(dev))
486 ids = &to_typec_partner(dev)->mode_ids;
487 else if (is_typec_plug(dev))
488 ids = &to_typec_plug(dev)->mode_ids;
490 ids = &to_typec_port(dev)->mode_ids;
492 ida_simple_remove(ids, id);
495 static void typec_altmode_release(struct device *dev)
497 struct altmode *alt = to_altmode(to_typec_altmode(dev));
499 typec_altmode_put_partner(alt);
501 altmode_id_remove(alt->adev.dev.parent, alt->id);
505 const struct device_type typec_altmode_dev_type = {
506 .name = "typec_alternate_mode",
507 .groups = typec_altmode_groups,
508 .release = typec_altmode_release,
511 static struct typec_altmode *
512 typec_register_altmode(struct device *parent,
513 const struct typec_altmode_desc *desc)
515 unsigned int id = altmode_id_get(parent);
516 bool is_port = is_typec_port(parent);
520 alt = kzalloc(sizeof(*alt), GFP_KERNEL);
522 altmode_id_remove(parent, id);
523 return ERR_PTR(-ENOMEM);
526 alt->adev.svid = desc->svid;
527 alt->adev.mode = desc->mode;
528 alt->adev.vdo = desc->vdo;
529 alt->roles = desc->roles;
532 alt->attrs[0] = &dev_attr_vdo.attr;
533 alt->attrs[1] = &dev_attr_description.attr;
534 alt->attrs[2] = &dev_attr_active.attr;
537 alt->attrs[3] = &dev_attr_supported_roles.attr;
538 alt->adev.active = true; /* Enabled by default */
541 sprintf(alt->group_name, "mode%d", desc->mode);
542 alt->group.name = alt->group_name;
543 alt->group.attrs = alt->attrs;
544 alt->groups[0] = &alt->group;
546 alt->adev.dev.parent = parent;
547 alt->adev.dev.groups = alt->groups;
548 alt->adev.dev.type = &typec_altmode_dev_type;
549 dev_set_name(&alt->adev.dev, "%s.%u", dev_name(parent), id);
551 /* Link partners and plugs with the ports */
553 typec_altmode_set_partner(alt);
555 /* The partners are bind to drivers */
556 if (is_typec_partner(parent))
557 alt->adev.dev.bus = &typec_bus;
559 /* Plug alt modes need a class to generate udev events. */
560 if (is_typec_plug(parent))
561 alt->adev.dev.class = &typec_class;
563 ret = device_register(&alt->adev.dev);
565 dev_err(parent, "failed to register alternate mode (%d)\n",
567 put_device(&alt->adev.dev);
575 * typec_unregister_altmode - Unregister Alternate Mode
576 * @adev: The alternate mode to be unregistered
578 * Unregister device created with typec_partner_register_altmode(),
579 * typec_plug_register_altmode() or typec_port_register_altmode().
581 void typec_unregister_altmode(struct typec_altmode *adev)
583 if (IS_ERR_OR_NULL(adev))
585 typec_retimer_put(to_altmode(adev)->retimer);
586 typec_mux_put(to_altmode(adev)->mux);
587 device_unregister(&adev->dev);
589 EXPORT_SYMBOL_GPL(typec_unregister_altmode);
591 /* ------------------------------------------------------------------------- */
592 /* Type-C Partners */
594 static ssize_t accessory_mode_show(struct device *dev,
595 struct device_attribute *attr,
598 struct typec_partner *p = to_typec_partner(dev);
600 return sprintf(buf, "%s\n", typec_accessory_modes[p->accessory]);
602 static DEVICE_ATTR_RO(accessory_mode);
604 static ssize_t supports_usb_power_delivery_show(struct device *dev,
605 struct device_attribute *attr,
608 struct typec_partner *p = to_typec_partner(dev);
610 return sprintf(buf, "%s\n", p->usb_pd ? "yes" : "no");
612 static DEVICE_ATTR_RO(supports_usb_power_delivery);
614 static ssize_t number_of_alternate_modes_show(struct device *dev, struct device_attribute *attr,
617 struct typec_partner *partner;
618 struct typec_plug *plug;
621 if (is_typec_partner(dev)) {
622 partner = to_typec_partner(dev);
623 num_altmodes = partner->num_altmodes;
624 } else if (is_typec_plug(dev)) {
625 plug = to_typec_plug(dev);
626 num_altmodes = plug->num_altmodes;
631 return sysfs_emit(buf, "%d\n", num_altmodes);
633 static DEVICE_ATTR_RO(number_of_alternate_modes);
635 static struct attribute *typec_partner_attrs[] = {
636 &dev_attr_accessory_mode.attr,
637 &dev_attr_supports_usb_power_delivery.attr,
638 &dev_attr_number_of_alternate_modes.attr,
640 &dev_attr_usb_power_delivery_revision.attr,
644 static umode_t typec_partner_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
646 struct typec_partner *partner = to_typec_partner(kobj_to_dev(kobj));
648 if (attr == &dev_attr_number_of_alternate_modes.attr) {
649 if (partner->num_altmodes < 0)
653 if (attr == &dev_attr_type.attr)
654 if (!get_pd_product_type(kobj_to_dev(kobj)))
660 static const struct attribute_group typec_partner_group = {
661 .is_visible = typec_partner_attr_is_visible,
662 .attrs = typec_partner_attrs
665 static const struct attribute_group *typec_partner_groups[] = {
666 &typec_partner_group,
670 static void typec_partner_release(struct device *dev)
672 struct typec_partner *partner = to_typec_partner(dev);
674 ida_destroy(&partner->mode_ids);
678 const struct device_type typec_partner_dev_type = {
679 .name = "typec_partner",
680 .groups = typec_partner_groups,
681 .release = typec_partner_release,
685 * typec_partner_set_identity - Report result from Discover Identity command
686 * @partner: The partner updated identity values
688 * This routine is used to report that the result of Discover Identity USB power
689 * delivery command has become available.
691 int typec_partner_set_identity(struct typec_partner *partner)
693 if (!partner->identity)
696 typec_report_identity(&partner->dev);
699 EXPORT_SYMBOL_GPL(typec_partner_set_identity);
702 * typec_partner_set_pd_revision - Set the PD revision supported by the partner
703 * @partner: The partner to be updated.
704 * @pd_revision: USB Power Delivery Specification Revision supported by partner
706 * This routine is used to report that the PD revision of the port partner has
709 void typec_partner_set_pd_revision(struct typec_partner *partner, u16 pd_revision)
711 if (partner->pd_revision == pd_revision)
714 partner->pd_revision = pd_revision;
715 sysfs_notify(&partner->dev.kobj, NULL, "usb_power_delivery_revision");
716 if (pd_revision != 0 && !partner->usb_pd) {
718 sysfs_notify(&partner->dev.kobj, NULL,
719 "supports_usb_power_delivery");
721 kobject_uevent(&partner->dev.kobj, KOBJ_CHANGE);
723 EXPORT_SYMBOL_GPL(typec_partner_set_pd_revision);
726 * typec_partner_set_usb_power_delivery - Declare USB Power Delivery Contract.
727 * @partner: The partner device.
728 * @pd: The USB PD instance.
730 * This routine can be used to declare USB Power Delivery Contract with @partner
731 * by linking @partner to @pd which contains the objects that were used during the
732 * negotiation of the contract.
734 * If @pd is NULL, the link is removed and the contract with @partner has ended.
736 int typec_partner_set_usb_power_delivery(struct typec_partner *partner,
737 struct usb_power_delivery *pd)
741 if (IS_ERR_OR_NULL(partner) || partner->pd == pd)
745 ret = usb_power_delivery_link_device(pd, &partner->dev);
749 usb_power_delivery_unlink_device(partner->pd, &partner->dev);
756 EXPORT_SYMBOL_GPL(typec_partner_set_usb_power_delivery);
759 * typec_partner_set_num_altmodes - Set the number of available partner altmodes
760 * @partner: The partner to be updated.
761 * @num_altmodes: The number of altmodes we want to specify as available.
763 * This routine is used to report the number of alternate modes supported by the
764 * partner. This value is *not* enforced in alternate mode registration routines.
766 * @partner.num_altmodes is set to -1 on partner registration, denoting that
767 * a valid value has not been set for it yet.
769 * Returns 0 on success or negative error number on failure.
771 int typec_partner_set_num_altmodes(struct typec_partner *partner, int num_altmodes)
775 if (num_altmodes < 0)
778 partner->num_altmodes = num_altmodes;
779 ret = sysfs_update_group(&partner->dev.kobj, &typec_partner_group);
783 sysfs_notify(&partner->dev.kobj, NULL, "number_of_alternate_modes");
784 kobject_uevent(&partner->dev.kobj, KOBJ_CHANGE);
788 EXPORT_SYMBOL_GPL(typec_partner_set_num_altmodes);
791 * typec_partner_register_altmode - Register USB Type-C Partner Alternate Mode
792 * @partner: USB Type-C Partner that supports the alternate mode
793 * @desc: Description of the alternate mode
795 * This routine is used to register each alternate mode individually that
796 * @partner has listed in response to Discover SVIDs command. The modes for a
797 * SVID listed in response to Discover Modes command need to be listed in an
800 * Returns handle to the alternate mode on success or ERR_PTR on failure.
802 struct typec_altmode *
803 typec_partner_register_altmode(struct typec_partner *partner,
804 const struct typec_altmode_desc *desc)
806 return typec_register_altmode(&partner->dev, desc);
808 EXPORT_SYMBOL_GPL(typec_partner_register_altmode);
811 * typec_partner_set_svdm_version - Set negotiated Structured VDM (SVDM) Version
812 * @partner: USB Type-C Partner that supports SVDM
813 * @svdm_version: Negotiated SVDM Version
815 * This routine is used to save the negotiated SVDM Version.
817 void typec_partner_set_svdm_version(struct typec_partner *partner,
818 enum usb_pd_svdm_ver svdm_version)
820 partner->svdm_version = svdm_version;
822 EXPORT_SYMBOL_GPL(typec_partner_set_svdm_version);
825 * typec_partner_usb_power_delivery_register - Register Type-C partner USB Power Delivery Support
826 * @partner: Type-C partner device.
827 * @desc: Description of the USB PD contract.
829 * This routine is a wrapper around usb_power_delivery_register(). It registers
830 * USB Power Delivery Capabilities for a Type-C partner device. Specifically,
831 * it sets the Type-C partner device as a parent for the resulting USB Power Delivery object.
833 * Returns handle to struct usb_power_delivery or ERR_PTR.
835 struct usb_power_delivery *
836 typec_partner_usb_power_delivery_register(struct typec_partner *partner,
837 struct usb_power_delivery_desc *desc)
839 return usb_power_delivery_register(&partner->dev, desc);
841 EXPORT_SYMBOL_GPL(typec_partner_usb_power_delivery_register);
844 * typec_register_partner - Register a USB Type-C Partner
845 * @port: The USB Type-C Port the partner is connected to
846 * @desc: Description of the partner
848 * Registers a device for USB Type-C Partner described in @desc.
850 * Returns handle to the partner on success or ERR_PTR on failure.
852 struct typec_partner *typec_register_partner(struct typec_port *port,
853 struct typec_partner_desc *desc)
855 struct typec_partner *partner;
858 partner = kzalloc(sizeof(*partner), GFP_KERNEL);
860 return ERR_PTR(-ENOMEM);
862 ida_init(&partner->mode_ids);
863 partner->usb_pd = desc->usb_pd;
864 partner->accessory = desc->accessory;
865 partner->num_altmodes = -1;
866 partner->pd_revision = desc->pd_revision;
867 partner->svdm_version = port->cap->svdm_version;
869 if (desc->identity) {
871 * Creating directory for the identity only if the driver is
872 * able to provide data to it.
874 partner->dev.groups = usb_pd_id_groups;
875 partner->identity = desc->identity;
878 partner->dev.class = &typec_class;
879 partner->dev.parent = &port->dev;
880 partner->dev.type = &typec_partner_dev_type;
881 dev_set_name(&partner->dev, "%s-partner", dev_name(&port->dev));
883 ret = device_register(&partner->dev);
885 dev_err(&port->dev, "failed to register partner (%d)\n", ret);
886 put_device(&partner->dev);
892 EXPORT_SYMBOL_GPL(typec_register_partner);
895 * typec_unregister_partner - Unregister a USB Type-C Partner
896 * @partner: The partner to be unregistered
898 * Unregister device created with typec_register_partner().
900 void typec_unregister_partner(struct typec_partner *partner)
902 if (!IS_ERR_OR_NULL(partner))
903 device_unregister(&partner->dev);
905 EXPORT_SYMBOL_GPL(typec_unregister_partner);
907 /* ------------------------------------------------------------------------- */
908 /* Type-C Cable Plugs */
910 static void typec_plug_release(struct device *dev)
912 struct typec_plug *plug = to_typec_plug(dev);
914 ida_destroy(&plug->mode_ids);
918 static struct attribute *typec_plug_attrs[] = {
919 &dev_attr_number_of_alternate_modes.attr,
923 static umode_t typec_plug_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
925 struct typec_plug *plug = to_typec_plug(kobj_to_dev(kobj));
927 if (attr == &dev_attr_number_of_alternate_modes.attr) {
928 if (plug->num_altmodes < 0)
935 static const struct attribute_group typec_plug_group = {
936 .is_visible = typec_plug_attr_is_visible,
937 .attrs = typec_plug_attrs
940 static const struct attribute_group *typec_plug_groups[] = {
945 const struct device_type typec_plug_dev_type = {
946 .name = "typec_plug",
947 .groups = typec_plug_groups,
948 .release = typec_plug_release,
952 * typec_plug_set_num_altmodes - Set the number of available plug altmodes
953 * @plug: The plug to be updated.
954 * @num_altmodes: The number of altmodes we want to specify as available.
956 * This routine is used to report the number of alternate modes supported by the
957 * plug. This value is *not* enforced in alternate mode registration routines.
959 * @plug.num_altmodes is set to -1 on plug registration, denoting that
960 * a valid value has not been set for it yet.
962 * Returns 0 on success or negative error number on failure.
964 int typec_plug_set_num_altmodes(struct typec_plug *plug, int num_altmodes)
968 if (num_altmodes < 0)
971 plug->num_altmodes = num_altmodes;
972 ret = sysfs_update_group(&plug->dev.kobj, &typec_plug_group);
976 sysfs_notify(&plug->dev.kobj, NULL, "number_of_alternate_modes");
977 kobject_uevent(&plug->dev.kobj, KOBJ_CHANGE);
981 EXPORT_SYMBOL_GPL(typec_plug_set_num_altmodes);
984 * typec_plug_register_altmode - Register USB Type-C Cable Plug Alternate Mode
985 * @plug: USB Type-C Cable Plug that supports the alternate mode
986 * @desc: Description of the alternate mode
988 * This routine is used to register each alternate mode individually that @plug
989 * has listed in response to Discover SVIDs command. The modes for a SVID that
990 * the plug lists in response to Discover Modes command need to be listed in an
993 * Returns handle to the alternate mode on success or ERR_PTR on failure.
995 struct typec_altmode *
996 typec_plug_register_altmode(struct typec_plug *plug,
997 const struct typec_altmode_desc *desc)
999 return typec_register_altmode(&plug->dev, desc);
1001 EXPORT_SYMBOL_GPL(typec_plug_register_altmode);
1004 * typec_register_plug - Register a USB Type-C Cable Plug
1005 * @cable: USB Type-C Cable with the plug
1006 * @desc: Description of the cable plug
1008 * Registers a device for USB Type-C Cable Plug described in @desc. A USB Type-C
1009 * Cable Plug represents a plug with electronics in it that can response to USB
1010 * Power Delivery SOP Prime or SOP Double Prime packages.
1012 * Returns handle to the cable plug on success or ERR_PTR on failure.
1014 struct typec_plug *typec_register_plug(struct typec_cable *cable,
1015 struct typec_plug_desc *desc)
1017 struct typec_plug *plug;
1021 plug = kzalloc(sizeof(*plug), GFP_KERNEL);
1023 return ERR_PTR(-ENOMEM);
1025 sprintf(name, "plug%d", desc->index);
1027 ida_init(&plug->mode_ids);
1028 plug->num_altmodes = -1;
1029 plug->index = desc->index;
1030 plug->dev.class = &typec_class;
1031 plug->dev.parent = &cable->dev;
1032 plug->dev.type = &typec_plug_dev_type;
1033 dev_set_name(&plug->dev, "%s-%s", dev_name(cable->dev.parent), name);
1035 ret = device_register(&plug->dev);
1037 dev_err(&cable->dev, "failed to register plug (%d)\n", ret);
1038 put_device(&plug->dev);
1039 return ERR_PTR(ret);
1044 EXPORT_SYMBOL_GPL(typec_register_plug);
1047 * typec_unregister_plug - Unregister a USB Type-C Cable Plug
1048 * @plug: The cable plug to be unregistered
1050 * Unregister device created with typec_register_plug().
1052 void typec_unregister_plug(struct typec_plug *plug)
1054 if (!IS_ERR_OR_NULL(plug))
1055 device_unregister(&plug->dev);
1057 EXPORT_SYMBOL_GPL(typec_unregister_plug);
1061 static const char * const typec_plug_types[] = {
1062 [USB_PLUG_NONE] = "unknown",
1063 [USB_PLUG_TYPE_A] = "type-a",
1064 [USB_PLUG_TYPE_B] = "type-b",
1065 [USB_PLUG_TYPE_C] = "type-c",
1066 [USB_PLUG_CAPTIVE] = "captive",
1069 static ssize_t plug_type_show(struct device *dev,
1070 struct device_attribute *attr, char *buf)
1072 struct typec_cable *cable = to_typec_cable(dev);
1074 return sprintf(buf, "%s\n", typec_plug_types[cable->type]);
1076 static DEVICE_ATTR_RO(plug_type);
1078 static struct attribute *typec_cable_attrs[] = {
1079 &dev_attr_type.attr,
1080 &dev_attr_plug_type.attr,
1081 &dev_attr_usb_power_delivery_revision.attr,
1084 ATTRIBUTE_GROUPS(typec_cable);
1086 static void typec_cable_release(struct device *dev)
1088 struct typec_cable *cable = to_typec_cable(dev);
1093 const struct device_type typec_cable_dev_type = {
1094 .name = "typec_cable",
1095 .groups = typec_cable_groups,
1096 .release = typec_cable_release,
1099 static int cable_match(struct device *dev, void *data)
1101 return is_typec_cable(dev);
1105 * typec_cable_get - Get a reference to the USB Type-C cable
1106 * @port: The USB Type-C Port the cable is connected to
1108 * The caller must decrement the reference count with typec_cable_put() after
1111 struct typec_cable *typec_cable_get(struct typec_port *port)
1115 dev = device_find_child(&port->dev, NULL, cable_match);
1119 return to_typec_cable(dev);
1121 EXPORT_SYMBOL_GPL(typec_cable_get);
1124 * typec_cable_put - Decrement the reference count on USB Type-C cable
1125 * @cable: The USB Type-C cable
1127 void typec_cable_put(struct typec_cable *cable)
1129 put_device(&cable->dev);
1131 EXPORT_SYMBOL_GPL(typec_cable_put);
1134 * typec_cable_is_active - Check is the USB Type-C cable active or passive
1135 * @cable: The USB Type-C Cable
1137 * Return 1 if the cable is active or 0 if it's passive.
1139 int typec_cable_is_active(struct typec_cable *cable)
1141 return cable->active;
1143 EXPORT_SYMBOL_GPL(typec_cable_is_active);
1146 * typec_cable_set_identity - Report result from Discover Identity command
1147 * @cable: The cable updated identity values
1149 * This routine is used to report that the result of Discover Identity USB power
1150 * delivery command has become available.
1152 int typec_cable_set_identity(struct typec_cable *cable)
1154 if (!cable->identity)
1157 typec_report_identity(&cable->dev);
1160 EXPORT_SYMBOL_GPL(typec_cable_set_identity);
1163 * typec_register_cable - Register a USB Type-C Cable
1164 * @port: The USB Type-C Port the cable is connected to
1165 * @desc: Description of the cable
1167 * Registers a device for USB Type-C Cable described in @desc. The cable will be
1168 * parent for the optional cable plug devises.
1170 * Returns handle to the cable on success or ERR_PTR on failure.
1172 struct typec_cable *typec_register_cable(struct typec_port *port,
1173 struct typec_cable_desc *desc)
1175 struct typec_cable *cable;
1178 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
1180 return ERR_PTR(-ENOMEM);
1182 cable->type = desc->type;
1183 cable->active = desc->active;
1184 cable->pd_revision = desc->pd_revision;
1186 if (desc->identity) {
1188 * Creating directory for the identity only if the driver is
1189 * able to provide data to it.
1191 cable->dev.groups = usb_pd_id_groups;
1192 cable->identity = desc->identity;
1195 cable->dev.class = &typec_class;
1196 cable->dev.parent = &port->dev;
1197 cable->dev.type = &typec_cable_dev_type;
1198 dev_set_name(&cable->dev, "%s-cable", dev_name(&port->dev));
1200 ret = device_register(&cable->dev);
1202 dev_err(&port->dev, "failed to register cable (%d)\n", ret);
1203 put_device(&cable->dev);
1204 return ERR_PTR(ret);
1209 EXPORT_SYMBOL_GPL(typec_register_cable);
1212 * typec_unregister_cable - Unregister a USB Type-C Cable
1213 * @cable: The cable to be unregistered
1215 * Unregister device created with typec_register_cable().
1217 void typec_unregister_cable(struct typec_cable *cable)
1219 if (!IS_ERR_OR_NULL(cable))
1220 device_unregister(&cable->dev);
1222 EXPORT_SYMBOL_GPL(typec_unregister_cable);
1224 /* ------------------------------------------------------------------------- */
1225 /* USB Type-C ports */
1228 * typec_port_set_usb_power_delivery - Assign USB PD for port.
1229 * @port: USB Type-C port.
1230 * @pd: USB PD instance.
1232 * This routine can be used to set the USB Power Delivery Capabilities for @port
1233 * that it will advertise to the partner.
1235 * If @pd is NULL, the assignment is removed.
1237 int typec_port_set_usb_power_delivery(struct typec_port *port, struct usb_power_delivery *pd)
1241 if (IS_ERR_OR_NULL(port) || port->pd == pd)
1245 ret = usb_power_delivery_link_device(pd, &port->dev);
1249 usb_power_delivery_unlink_device(port->pd, &port->dev);
1256 EXPORT_SYMBOL_GPL(typec_port_set_usb_power_delivery);
1258 static ssize_t select_usb_power_delivery_store(struct device *dev,
1259 struct device_attribute *attr,
1260 const char *buf, size_t size)
1262 struct typec_port *port = to_typec_port(dev);
1263 struct usb_power_delivery *pd;
1265 if (!port->ops || !port->ops->pd_set)
1268 pd = usb_power_delivery_find(buf);
1272 return port->ops->pd_set(port, pd);
1275 static ssize_t select_usb_power_delivery_show(struct device *dev,
1276 struct device_attribute *attr, char *buf)
1278 struct typec_port *port = to_typec_port(dev);
1279 struct usb_power_delivery **pds;
1280 struct usb_power_delivery *pd;
1283 if (!port->ops || !port->ops->pd_get)
1286 pds = port->ops->pd_get(port);
1290 for (pd = pds[0]; pd; pd++) {
1292 ret += sysfs_emit(buf + ret, "[%s] ", dev_name(&pd->dev));
1294 ret += sysfs_emit(buf + ret, "%s ", dev_name(&pd->dev));
1297 buf[ret - 1] = '\n';
1301 static DEVICE_ATTR_RW(select_usb_power_delivery);
1303 static struct attribute *port_attrs[] = {
1304 &dev_attr_select_usb_power_delivery.attr,
1308 static umode_t port_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
1310 struct typec_port *port = to_typec_port(kobj_to_dev(kobj));
1312 if (!port->pd || !port->ops || !port->ops->pd_get)
1314 if (!port->ops->pd_set)
1320 static const struct attribute_group pd_group = {
1321 .is_visible = port_attr_is_visible,
1322 .attrs = port_attrs,
1325 static const char * const typec_orientations[] = {
1326 [TYPEC_ORIENTATION_NONE] = "unknown",
1327 [TYPEC_ORIENTATION_NORMAL] = "normal",
1328 [TYPEC_ORIENTATION_REVERSE] = "reverse",
1331 static const char * const typec_roles[] = {
1332 [TYPEC_SINK] = "sink",
1333 [TYPEC_SOURCE] = "source",
1336 static const char * const typec_data_roles[] = {
1337 [TYPEC_DEVICE] = "device",
1338 [TYPEC_HOST] = "host",
1341 static const char * const typec_port_power_roles[] = {
1342 [TYPEC_PORT_SRC] = "source",
1343 [TYPEC_PORT_SNK] = "sink",
1344 [TYPEC_PORT_DRP] = "dual",
1347 static const char * const typec_port_data_roles[] = {
1348 [TYPEC_PORT_DFP] = "host",
1349 [TYPEC_PORT_UFP] = "device",
1350 [TYPEC_PORT_DRD] = "dual",
1353 static const char * const typec_port_types_drp[] = {
1354 [TYPEC_PORT_SRC] = "dual [source] sink",
1355 [TYPEC_PORT_SNK] = "dual source [sink]",
1356 [TYPEC_PORT_DRP] = "[dual] source sink",
1360 preferred_role_store(struct device *dev, struct device_attribute *attr,
1361 const char *buf, size_t size)
1363 struct typec_port *port = to_typec_port(dev);
1367 if (port->cap->type != TYPEC_PORT_DRP) {
1368 dev_dbg(dev, "Preferred role only supported with DRP ports\n");
1372 if (!port->ops || !port->ops->try_role) {
1373 dev_dbg(dev, "Setting preferred role not supported\n");
1377 role = sysfs_match_string(typec_roles, buf);
1379 if (sysfs_streq(buf, "none"))
1380 role = TYPEC_NO_PREFERRED_ROLE;
1385 ret = port->ops->try_role(port, role);
1389 port->prefer_role = role;
1394 preferred_role_show(struct device *dev, struct device_attribute *attr,
1397 struct typec_port *port = to_typec_port(dev);
1399 if (port->cap->type != TYPEC_PORT_DRP)
1402 if (port->prefer_role < 0)
1405 return sprintf(buf, "%s\n", typec_roles[port->prefer_role]);
1407 static DEVICE_ATTR_RW(preferred_role);
1409 static ssize_t data_role_store(struct device *dev,
1410 struct device_attribute *attr,
1411 const char *buf, size_t size)
1413 struct typec_port *port = to_typec_port(dev);
1416 if (!port->ops || !port->ops->dr_set) {
1417 dev_dbg(dev, "data role swapping not supported\n");
1421 ret = sysfs_match_string(typec_data_roles, buf);
1425 mutex_lock(&port->port_type_lock);
1426 if (port->cap->data != TYPEC_PORT_DRD) {
1428 goto unlock_and_ret;
1431 ret = port->ops->dr_set(port, ret);
1433 goto unlock_and_ret;
1437 mutex_unlock(&port->port_type_lock);
1441 static ssize_t data_role_show(struct device *dev,
1442 struct device_attribute *attr, char *buf)
1444 struct typec_port *port = to_typec_port(dev);
1446 if (port->cap->data == TYPEC_PORT_DRD)
1447 return sprintf(buf, "%s\n", port->data_role == TYPEC_HOST ?
1448 "[host] device" : "host [device]");
1450 return sprintf(buf, "[%s]\n", typec_data_roles[port->data_role]);
1452 static DEVICE_ATTR_RW(data_role);
1454 static ssize_t power_role_store(struct device *dev,
1455 struct device_attribute *attr,
1456 const char *buf, size_t size)
1458 struct typec_port *port = to_typec_port(dev);
1461 if (!port->ops || !port->ops->pr_set) {
1462 dev_dbg(dev, "power role swapping not supported\n");
1466 if (port->pwr_opmode != TYPEC_PWR_MODE_PD) {
1467 dev_dbg(dev, "partner unable to swap power role\n");
1471 ret = sysfs_match_string(typec_roles, buf);
1475 mutex_lock(&port->port_type_lock);
1476 if (port->port_type != TYPEC_PORT_DRP) {
1477 dev_dbg(dev, "port type fixed at \"%s\"",
1478 typec_port_power_roles[port->port_type]);
1480 goto unlock_and_ret;
1483 ret = port->ops->pr_set(port, ret);
1485 goto unlock_and_ret;
1489 mutex_unlock(&port->port_type_lock);
1493 static ssize_t power_role_show(struct device *dev,
1494 struct device_attribute *attr, char *buf)
1496 struct typec_port *port = to_typec_port(dev);
1498 if (port->cap->type == TYPEC_PORT_DRP)
1499 return sprintf(buf, "%s\n", port->pwr_role == TYPEC_SOURCE ?
1500 "[source] sink" : "source [sink]");
1502 return sprintf(buf, "[%s]\n", typec_roles[port->pwr_role]);
1504 static DEVICE_ATTR_RW(power_role);
1507 port_type_store(struct device *dev, struct device_attribute *attr,
1508 const char *buf, size_t size)
1510 struct typec_port *port = to_typec_port(dev);
1512 enum typec_port_type type;
1514 if (port->cap->type != TYPEC_PORT_DRP ||
1515 !port->ops || !port->ops->port_type_set) {
1516 dev_dbg(dev, "changing port type not supported\n");
1520 ret = sysfs_match_string(typec_port_power_roles, buf);
1525 mutex_lock(&port->port_type_lock);
1527 if (port->port_type == type) {
1529 goto unlock_and_ret;
1532 ret = port->ops->port_type_set(port, type);
1534 goto unlock_and_ret;
1536 port->port_type = type;
1540 mutex_unlock(&port->port_type_lock);
1545 port_type_show(struct device *dev, struct device_attribute *attr,
1548 struct typec_port *port = to_typec_port(dev);
1550 if (port->cap->type == TYPEC_PORT_DRP)
1551 return sprintf(buf, "%s\n",
1552 typec_port_types_drp[port->port_type]);
1554 return sprintf(buf, "[%s]\n", typec_port_power_roles[port->cap->type]);
1556 static DEVICE_ATTR_RW(port_type);
1558 static const char * const typec_pwr_opmodes[] = {
1559 [TYPEC_PWR_MODE_USB] = "default",
1560 [TYPEC_PWR_MODE_1_5A] = "1.5A",
1561 [TYPEC_PWR_MODE_3_0A] = "3.0A",
1562 [TYPEC_PWR_MODE_PD] = "usb_power_delivery",
1565 static ssize_t power_operation_mode_show(struct device *dev,
1566 struct device_attribute *attr,
1569 struct typec_port *port = to_typec_port(dev);
1571 return sprintf(buf, "%s\n", typec_pwr_opmodes[port->pwr_opmode]);
1573 static DEVICE_ATTR_RO(power_operation_mode);
1575 static ssize_t vconn_source_store(struct device *dev,
1576 struct device_attribute *attr,
1577 const char *buf, size_t size)
1579 struct typec_port *port = to_typec_port(dev);
1583 if (!port->cap->pd_revision) {
1584 dev_dbg(dev, "VCONN swap depends on USB Power Delivery\n");
1588 if (!port->ops || !port->ops->vconn_set) {
1589 dev_dbg(dev, "VCONN swapping not supported\n");
1593 ret = kstrtobool(buf, &source);
1597 ret = port->ops->vconn_set(port, (enum typec_role)source);
1604 static ssize_t vconn_source_show(struct device *dev,
1605 struct device_attribute *attr, char *buf)
1607 struct typec_port *port = to_typec_port(dev);
1609 return sprintf(buf, "%s\n",
1610 port->vconn_role == TYPEC_SOURCE ? "yes" : "no");
1612 static DEVICE_ATTR_RW(vconn_source);
1614 static ssize_t supported_accessory_modes_show(struct device *dev,
1615 struct device_attribute *attr,
1618 struct typec_port *port = to_typec_port(dev);
1622 for (i = 0; i < ARRAY_SIZE(port->cap->accessory); i++) {
1623 if (port->cap->accessory[i])
1624 ret += sprintf(buf + ret, "%s ",
1625 typec_accessory_modes[port->cap->accessory[i]]);
1629 return sprintf(buf, "none\n");
1631 buf[ret - 1] = '\n';
1635 static DEVICE_ATTR_RO(supported_accessory_modes);
1637 static ssize_t usb_typec_revision_show(struct device *dev,
1638 struct device_attribute *attr,
1641 struct typec_port *port = to_typec_port(dev);
1642 u16 rev = port->cap->revision;
1644 return sprintf(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
1646 static DEVICE_ATTR_RO(usb_typec_revision);
1648 static ssize_t usb_power_delivery_revision_show(struct device *dev,
1649 struct device_attribute *attr,
1654 if (is_typec_partner(dev)) {
1655 struct typec_partner *partner = to_typec_partner(dev);
1657 rev = partner->pd_revision;
1658 } else if (is_typec_cable(dev)) {
1659 struct typec_cable *cable = to_typec_cable(dev);
1661 rev = cable->pd_revision;
1662 } else if (is_typec_port(dev)) {
1663 struct typec_port *p = to_typec_port(dev);
1665 rev = p->cap->pd_revision;
1667 return sysfs_emit(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
1670 static ssize_t orientation_show(struct device *dev,
1671 struct device_attribute *attr,
1674 struct typec_port *port = to_typec_port(dev);
1676 return sprintf(buf, "%s\n", typec_orientations[port->orientation]);
1678 static DEVICE_ATTR_RO(orientation);
1680 static struct attribute *typec_attrs[] = {
1681 &dev_attr_data_role.attr,
1682 &dev_attr_power_operation_mode.attr,
1683 &dev_attr_power_role.attr,
1684 &dev_attr_preferred_role.attr,
1685 &dev_attr_supported_accessory_modes.attr,
1686 &dev_attr_usb_power_delivery_revision.attr,
1687 &dev_attr_usb_typec_revision.attr,
1688 &dev_attr_vconn_source.attr,
1689 &dev_attr_port_type.attr,
1690 &dev_attr_orientation.attr,
1694 static umode_t typec_attr_is_visible(struct kobject *kobj,
1695 struct attribute *attr, int n)
1697 struct typec_port *port = to_typec_port(kobj_to_dev(kobj));
1699 if (attr == &dev_attr_data_role.attr) {
1700 if (port->cap->data != TYPEC_PORT_DRD ||
1701 !port->ops || !port->ops->dr_set)
1703 } else if (attr == &dev_attr_power_role.attr) {
1704 if (port->cap->type != TYPEC_PORT_DRP ||
1705 !port->ops || !port->ops->pr_set)
1707 } else if (attr == &dev_attr_vconn_source.attr) {
1708 if (!port->cap->pd_revision ||
1709 !port->ops || !port->ops->vconn_set)
1711 } else if (attr == &dev_attr_preferred_role.attr) {
1712 if (port->cap->type != TYPEC_PORT_DRP ||
1713 !port->ops || !port->ops->try_role)
1715 } else if (attr == &dev_attr_port_type.attr) {
1716 if (!port->ops || !port->ops->port_type_set)
1718 if (port->cap->type != TYPEC_PORT_DRP)
1720 } else if (attr == &dev_attr_orientation.attr) {
1721 if (port->cap->orientation_aware)
1729 static const struct attribute_group typec_group = {
1730 .is_visible = typec_attr_is_visible,
1731 .attrs = typec_attrs,
1734 static const struct attribute_group *typec_groups[] = {
1740 static int typec_uevent(const struct device *dev, struct kobj_uevent_env *env)
1744 ret = add_uevent_var(env, "TYPEC_PORT=%s", dev_name(dev));
1746 dev_err(dev, "failed to add uevent TYPEC_PORT\n");
1751 static void typec_release(struct device *dev)
1753 struct typec_port *port = to_typec_port(dev);
1755 ida_simple_remove(&typec_index_ida, port->id);
1756 ida_destroy(&port->mode_ids);
1757 typec_switch_put(port->sw);
1758 typec_mux_put(port->mux);
1759 typec_retimer_put(port->retimer);
1764 const struct device_type typec_port_dev_type = {
1765 .name = "typec_port",
1766 .groups = typec_groups,
1767 .uevent = typec_uevent,
1768 .release = typec_release,
1771 /* --------------------------------------- */
1772 /* Driver callbacks to report role updates */
1774 static int partner_match(struct device *dev, void *data)
1776 return is_typec_partner(dev);
1780 * typec_set_data_role - Report data role change
1781 * @port: The USB Type-C Port where the role was changed
1782 * @role: The new data role
1784 * This routine is used by the port drivers to report data role changes.
1786 void typec_set_data_role(struct typec_port *port, enum typec_data_role role)
1788 struct device *partner_dev;
1790 if (port->data_role == role)
1793 port->data_role = role;
1794 sysfs_notify(&port->dev.kobj, NULL, "data_role");
1795 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1797 partner_dev = device_find_child(&port->dev, NULL, partner_match);
1801 if (to_typec_partner(partner_dev)->identity)
1802 typec_product_type_notify(partner_dev);
1804 put_device(partner_dev);
1806 EXPORT_SYMBOL_GPL(typec_set_data_role);
1809 * typec_set_pwr_role - Report power role change
1810 * @port: The USB Type-C Port where the role was changed
1811 * @role: The new data role
1813 * This routine is used by the port drivers to report power role changes.
1815 void typec_set_pwr_role(struct typec_port *port, enum typec_role role)
1817 if (port->pwr_role == role)
1820 port->pwr_role = role;
1821 sysfs_notify(&port->dev.kobj, NULL, "power_role");
1822 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1824 EXPORT_SYMBOL_GPL(typec_set_pwr_role);
1827 * typec_set_vconn_role - Report VCONN source change
1828 * @port: The USB Type-C Port which VCONN role changed
1829 * @role: Source when @port is sourcing VCONN, or Sink when it's not
1831 * This routine is used by the port drivers to report if the VCONN source is
1834 void typec_set_vconn_role(struct typec_port *port, enum typec_role role)
1836 if (port->vconn_role == role)
1839 port->vconn_role = role;
1840 sysfs_notify(&port->dev.kobj, NULL, "vconn_source");
1841 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1843 EXPORT_SYMBOL_GPL(typec_set_vconn_role);
1846 * typec_set_pwr_opmode - Report changed power operation mode
1847 * @port: The USB Type-C Port where the mode was changed
1848 * @opmode: New power operation mode
1850 * This routine is used by the port drivers to report changed power operation
1851 * mode in @port. The modes are USB (default), 1.5A, 3.0A as defined in USB
1852 * Type-C specification, and "USB Power Delivery" when the power levels are
1853 * negotiated with methods defined in USB Power Delivery specification.
1855 void typec_set_pwr_opmode(struct typec_port *port,
1856 enum typec_pwr_opmode opmode)
1858 struct device *partner_dev;
1860 if (port->pwr_opmode == opmode)
1863 port->pwr_opmode = opmode;
1864 sysfs_notify(&port->dev.kobj, NULL, "power_operation_mode");
1865 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1867 partner_dev = device_find_child(&port->dev, NULL, partner_match);
1869 struct typec_partner *partner = to_typec_partner(partner_dev);
1871 if (opmode == TYPEC_PWR_MODE_PD && !partner->usb_pd) {
1872 partner->usb_pd = 1;
1873 sysfs_notify(&partner_dev->kobj, NULL,
1874 "supports_usb_power_delivery");
1875 kobject_uevent(&partner_dev->kobj, KOBJ_CHANGE);
1877 put_device(partner_dev);
1880 EXPORT_SYMBOL_GPL(typec_set_pwr_opmode);
1883 * typec_find_pwr_opmode - Get the typec power operation mode capability
1884 * @name: power operation mode string
1886 * This routine is used to find the typec_pwr_opmode by its string @name.
1888 * Returns typec_pwr_opmode if success, otherwise negative error code.
1890 int typec_find_pwr_opmode(const char *name)
1892 return match_string(typec_pwr_opmodes,
1893 ARRAY_SIZE(typec_pwr_opmodes), name);
1895 EXPORT_SYMBOL_GPL(typec_find_pwr_opmode);
1898 * typec_find_orientation - Convert orientation string to enum typec_orientation
1899 * @name: Orientation string
1901 * This routine is used to find the typec_orientation by its string name @name.
1903 * Returns the orientation value on success, otherwise negative error code.
1905 int typec_find_orientation(const char *name)
1907 return match_string(typec_orientations, ARRAY_SIZE(typec_orientations),
1910 EXPORT_SYMBOL_GPL(typec_find_orientation);
1913 * typec_find_port_power_role - Get the typec port power capability
1914 * @name: port power capability string
1916 * This routine is used to find the typec_port_type by its string name.
1918 * Returns typec_port_type if success, otherwise negative error code.
1920 int typec_find_port_power_role(const char *name)
1922 return match_string(typec_port_power_roles,
1923 ARRAY_SIZE(typec_port_power_roles), name);
1925 EXPORT_SYMBOL_GPL(typec_find_port_power_role);
1928 * typec_find_power_role - Find the typec one specific power role
1929 * @name: power role string
1931 * This routine is used to find the typec_role by its string name.
1933 * Returns typec_role if success, otherwise negative error code.
1935 int typec_find_power_role(const char *name)
1937 return match_string(typec_roles, ARRAY_SIZE(typec_roles), name);
1939 EXPORT_SYMBOL_GPL(typec_find_power_role);
1942 * typec_find_port_data_role - Get the typec port data capability
1943 * @name: port data capability string
1945 * This routine is used to find the typec_port_data by its string name.
1947 * Returns typec_port_data if success, otherwise negative error code.
1949 int typec_find_port_data_role(const char *name)
1951 return match_string(typec_port_data_roles,
1952 ARRAY_SIZE(typec_port_data_roles), name);
1954 EXPORT_SYMBOL_GPL(typec_find_port_data_role);
1956 /* ------------------------------------------ */
1957 /* API for Multiplexer/DeMultiplexer Switches */
1960 * typec_set_orientation - Set USB Type-C cable plug orientation
1961 * @port: USB Type-C Port
1962 * @orientation: USB Type-C cable plug orientation
1964 * Set cable plug orientation for @port.
1966 int typec_set_orientation(struct typec_port *port,
1967 enum typec_orientation orientation)
1971 ret = typec_switch_set(port->sw, orientation);
1975 port->orientation = orientation;
1976 sysfs_notify(&port->dev.kobj, NULL, "orientation");
1977 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1981 EXPORT_SYMBOL_GPL(typec_set_orientation);
1984 * typec_get_orientation - Get USB Type-C cable plug orientation
1985 * @port: USB Type-C Port
1987 * Get current cable plug orientation for @port.
1989 enum typec_orientation typec_get_orientation(struct typec_port *port)
1991 return port->orientation;
1993 EXPORT_SYMBOL_GPL(typec_get_orientation);
1996 * typec_set_mode - Set mode of operation for USB Type-C connector
1997 * @port: USB Type-C connector
1998 * @mode: Accessory Mode, USB Operation or Safe State
2000 * Configure @port for Accessory Mode @mode. This function will configure the
2001 * muxes needed for @mode.
2003 int typec_set_mode(struct typec_port *port, int mode)
2005 struct typec_mux_state state = { };
2009 return typec_mux_set(port->mux, &state);
2011 EXPORT_SYMBOL_GPL(typec_set_mode);
2013 /* --------------------------------------- */
2016 * typec_get_negotiated_svdm_version - Get negotiated SVDM Version
2017 * @port: USB Type-C Port.
2019 * Get the negotiated SVDM Version. The Version is set to the port default
2020 * value stored in typec_capability on partner registration, and updated after
2021 * a successful Discover Identity if the negotiated value is less than the
2024 * Returns usb_pd_svdm_ver if the partner has been registered otherwise -ENODEV.
2026 int typec_get_negotiated_svdm_version(struct typec_port *port)
2028 enum usb_pd_svdm_ver svdm_version;
2029 struct device *partner_dev;
2031 partner_dev = device_find_child(&port->dev, NULL, partner_match);
2035 svdm_version = to_typec_partner(partner_dev)->svdm_version;
2036 put_device(partner_dev);
2038 return svdm_version;
2040 EXPORT_SYMBOL_GPL(typec_get_negotiated_svdm_version);
2043 * typec_get_drvdata - Return private driver data pointer
2044 * @port: USB Type-C port
2046 void *typec_get_drvdata(struct typec_port *port)
2048 return dev_get_drvdata(&port->dev);
2050 EXPORT_SYMBOL_GPL(typec_get_drvdata);
2052 int typec_get_fw_cap(struct typec_capability *cap,
2053 struct fwnode_handle *fwnode)
2055 const char *cap_str;
2058 cap->fwnode = fwnode;
2060 ret = fwnode_property_read_string(fwnode, "power-role", &cap_str);
2064 ret = typec_find_port_power_role(cap_str);
2069 /* USB data support is optional */
2070 ret = fwnode_property_read_string(fwnode, "data-role", &cap_str);
2072 ret = typec_find_port_data_role(cap_str);
2078 /* Get the preferred power role for a DRP */
2079 if (cap->type == TYPEC_PORT_DRP) {
2080 cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
2082 ret = fwnode_property_read_string(fwnode, "try-power-role", &cap_str);
2084 ret = typec_find_power_role(cap_str);
2087 cap->prefer_role = ret;
2093 EXPORT_SYMBOL_GPL(typec_get_fw_cap);
2096 * typec_port_register_altmode - Register USB Type-C Port Alternate Mode
2097 * @port: USB Type-C Port that supports the alternate mode
2098 * @desc: Description of the alternate mode
2100 * This routine is used to register an alternate mode that @port is capable of
2103 * Returns handle to the alternate mode on success or ERR_PTR on failure.
2105 struct typec_altmode *
2106 typec_port_register_altmode(struct typec_port *port,
2107 const struct typec_altmode_desc *desc)
2109 struct typec_altmode *adev;
2110 struct typec_mux *mux;
2111 struct typec_retimer *retimer;
2113 mux = typec_mux_get(&port->dev, desc);
2115 return ERR_CAST(mux);
2117 retimer = typec_retimer_get(&port->dev);
2118 if (IS_ERR(retimer)) {
2120 return ERR_CAST(retimer);
2123 adev = typec_register_altmode(&port->dev, desc);
2125 typec_retimer_put(retimer);
2128 to_altmode(adev)->mux = mux;
2129 to_altmode(adev)->retimer = retimer;
2134 EXPORT_SYMBOL_GPL(typec_port_register_altmode);
2136 void typec_port_register_altmodes(struct typec_port *port,
2137 const struct typec_altmode_ops *ops, void *drvdata,
2138 struct typec_altmode **altmodes, size_t n)
2140 struct fwnode_handle *altmodes_node, *child;
2141 struct typec_altmode_desc desc;
2142 struct typec_altmode *alt;
2147 altmodes_node = device_get_named_child_node(&port->dev, "altmodes");
2149 return; /* No altmodes specified */
2151 fwnode_for_each_child_node(altmodes_node, child) {
2152 ret = fwnode_property_read_u32(child, "svid", &svid);
2154 dev_err(&port->dev, "Error reading svid for altmode %s\n",
2155 fwnode_get_name(child));
2159 ret = fwnode_property_read_u32(child, "vdo", &vdo);
2161 dev_err(&port->dev, "Error reading vdo for altmode %s\n",
2162 fwnode_get_name(child));
2167 dev_err(&port->dev, "Error not enough space for altmode %s\n",
2168 fwnode_get_name(child));
2174 desc.mode = index + 1;
2175 alt = typec_port_register_altmode(port, &desc);
2177 dev_err(&port->dev, "Error registering altmode %s\n",
2178 fwnode_get_name(child));
2183 typec_altmode_set_drvdata(alt, drvdata);
2184 altmodes[index] = alt;
2188 EXPORT_SYMBOL_GPL(typec_port_register_altmodes);
2191 * typec_register_port - Register a USB Type-C Port
2192 * @parent: Parent device
2193 * @cap: Description of the port
2195 * Registers a device for USB Type-C Port described in @cap.
2197 * Returns handle to the port on success or ERR_PTR on failure.
2199 struct typec_port *typec_register_port(struct device *parent,
2200 const struct typec_capability *cap)
2202 struct typec_port *port;
2206 port = kzalloc(sizeof(*port), GFP_KERNEL);
2208 return ERR_PTR(-ENOMEM);
2210 id = ida_simple_get(&typec_index_ida, 0, 0, GFP_KERNEL);
2216 switch (cap->type) {
2217 case TYPEC_PORT_SRC:
2218 port->pwr_role = TYPEC_SOURCE;
2219 port->vconn_role = TYPEC_SOURCE;
2221 case TYPEC_PORT_SNK:
2222 port->pwr_role = TYPEC_SINK;
2223 port->vconn_role = TYPEC_SINK;
2225 case TYPEC_PORT_DRP:
2226 if (cap->prefer_role != TYPEC_NO_PREFERRED_ROLE)
2227 port->pwr_role = cap->prefer_role;
2229 port->pwr_role = TYPEC_SINK;
2233 switch (cap->data) {
2234 case TYPEC_PORT_DFP:
2235 port->data_role = TYPEC_HOST;
2237 case TYPEC_PORT_UFP:
2238 port->data_role = TYPEC_DEVICE;
2240 case TYPEC_PORT_DRD:
2241 if (cap->prefer_role == TYPEC_SOURCE)
2242 port->data_role = TYPEC_HOST;
2244 port->data_role = TYPEC_DEVICE;
2248 ida_init(&port->mode_ids);
2249 mutex_init(&port->port_type_lock);
2252 port->ops = cap->ops;
2253 port->port_type = cap->type;
2254 port->prefer_role = cap->prefer_role;
2256 device_initialize(&port->dev);
2257 port->dev.class = &typec_class;
2258 port->dev.parent = parent;
2259 port->dev.fwnode = cap->fwnode;
2260 port->dev.type = &typec_port_dev_type;
2261 dev_set_name(&port->dev, "port%d", id);
2262 dev_set_drvdata(&port->dev, cap->driver_data);
2264 port->cap = kmemdup(cap, sizeof(*cap), GFP_KERNEL);
2266 put_device(&port->dev);
2267 return ERR_PTR(-ENOMEM);
2270 port->sw = typec_switch_get(&port->dev);
2271 if (IS_ERR(port->sw)) {
2272 ret = PTR_ERR(port->sw);
2273 put_device(&port->dev);
2274 return ERR_PTR(ret);
2277 port->mux = typec_mux_get(&port->dev, NULL);
2278 if (IS_ERR(port->mux)) {
2279 ret = PTR_ERR(port->mux);
2280 put_device(&port->dev);
2281 return ERR_PTR(ret);
2284 port->retimer = typec_retimer_get(&port->dev);
2285 if (IS_ERR(port->retimer)) {
2286 ret = PTR_ERR(port->retimer);
2287 put_device(&port->dev);
2288 return ERR_PTR(ret);
2291 ret = device_add(&port->dev);
2293 dev_err(parent, "failed to register port (%d)\n", ret);
2294 put_device(&port->dev);
2295 return ERR_PTR(ret);
2298 ret = typec_port_set_usb_power_delivery(port, cap->pd);
2300 dev_err(&port->dev, "failed to link pd\n");
2301 device_unregister(&port->dev);
2302 return ERR_PTR(ret);
2305 ret = typec_link_ports(port);
2307 dev_warn(&port->dev, "failed to create symlinks (%d)\n", ret);
2311 EXPORT_SYMBOL_GPL(typec_register_port);
2314 * typec_unregister_port - Unregister a USB Type-C Port
2315 * @port: The port to be unregistered
2317 * Unregister device created with typec_register_port().
2319 void typec_unregister_port(struct typec_port *port)
2321 if (!IS_ERR_OR_NULL(port)) {
2322 typec_unlink_ports(port);
2323 typec_port_set_usb_power_delivery(port, NULL);
2324 device_unregister(&port->dev);
2327 EXPORT_SYMBOL_GPL(typec_unregister_port);
2329 static int __init typec_init(void)
2333 ret = bus_register(&typec_bus);
2337 ret = class_register(&typec_mux_class);
2339 goto err_unregister_bus;
2341 ret = class_register(&retimer_class);
2343 goto err_unregister_mux_class;
2345 ret = class_register(&typec_class);
2347 goto err_unregister_retimer_class;
2349 ret = usb_power_delivery_init();
2351 goto err_unregister_class;
2355 err_unregister_class:
2356 class_unregister(&typec_class);
2358 err_unregister_retimer_class:
2359 class_unregister(&retimer_class);
2361 err_unregister_mux_class:
2362 class_unregister(&typec_mux_class);
2365 bus_unregister(&typec_bus);
2369 subsys_initcall(typec_init);
2371 static void __exit typec_exit(void)
2373 usb_power_delivery_exit();
2374 class_unregister(&typec_class);
2375 ida_destroy(&typec_index_ida);
2376 bus_unregister(&typec_bus);
2377 class_unregister(&typec_mux_class);
2378 class_unregister(&retimer_class);
2380 module_exit(typec_exit);
2383 MODULE_LICENSE("GPL v2");
2384 MODULE_DESCRIPTION("USB Type-C Connector Class");