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>
19 static DEFINE_IDA(typec_index_ida);
21 struct class typec_class = {
26 /* ------------------------------------------------------------------------- */
27 /* Common attributes */
29 static const char * const typec_accessory_modes[] = {
30 [TYPEC_ACCESSORY_NONE] = "none",
31 [TYPEC_ACCESSORY_AUDIO] = "analog_audio",
32 [TYPEC_ACCESSORY_DEBUG] = "debug",
35 /* Product types defined in USB PD Specification R3.0 V2.0 */
36 static const char * const product_type_ufp[8] = {
37 [IDH_PTYPE_NOT_UFP] = "not_ufp",
38 [IDH_PTYPE_HUB] = "hub",
39 [IDH_PTYPE_PERIPH] = "peripheral",
40 [IDH_PTYPE_PSD] = "psd",
41 [IDH_PTYPE_AMA] = "ama",
44 static const char * const product_type_dfp[8] = {
45 [IDH_PTYPE_NOT_DFP] = "not_dfp",
46 [IDH_PTYPE_DFP_HUB] = "hub",
47 [IDH_PTYPE_DFP_HOST] = "host",
48 [IDH_PTYPE_DFP_PB] = "power_brick",
51 static const char * const product_type_cable[8] = {
52 [IDH_PTYPE_NOT_CABLE] = "not_cable",
53 [IDH_PTYPE_PCABLE] = "passive",
54 [IDH_PTYPE_ACABLE] = "active",
55 [IDH_PTYPE_VPD] = "vpd",
58 static struct usb_pd_identity *get_pd_identity(struct device *dev)
60 if (is_typec_partner(dev)) {
61 struct typec_partner *partner = to_typec_partner(dev);
63 return partner->identity;
64 } else if (is_typec_cable(dev)) {
65 struct typec_cable *cable = to_typec_cable(dev);
67 return cable->identity;
72 static const char *get_pd_product_type(struct device *dev)
74 struct typec_port *port = to_typec_port(dev->parent);
75 struct usb_pd_identity *id = get_pd_identity(dev);
76 const char *ptype = NULL;
78 if (is_typec_partner(dev)) {
82 if (port->data_role == TYPEC_HOST)
83 ptype = product_type_ufp[PD_IDH_PTYPE(id->id_header)];
85 ptype = product_type_dfp[PD_IDH_DFP_PTYPE(id->id_header)];
86 } else if (is_typec_cable(dev)) {
88 ptype = product_type_cable[PD_IDH_PTYPE(id->id_header)];
90 ptype = to_typec_cable(dev)->active ?
91 product_type_cable[IDH_PTYPE_ACABLE] :
92 product_type_cable[IDH_PTYPE_PCABLE];
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 ssize_t product_type_vdo1_show(struct device *dev, struct device_attribute *attr,
128 struct usb_pd_identity *id = get_pd_identity(dev);
130 return sysfs_emit(buf, "0x%08x\n", id->vdo[0]);
132 static DEVICE_ATTR_RO(product_type_vdo1);
134 static ssize_t product_type_vdo2_show(struct device *dev, struct device_attribute *attr,
137 struct usb_pd_identity *id = get_pd_identity(dev);
139 return sysfs_emit(buf, "0x%08x\n", id->vdo[1]);
141 static DEVICE_ATTR_RO(product_type_vdo2);
143 static ssize_t product_type_vdo3_show(struct device *dev, struct device_attribute *attr,
146 struct usb_pd_identity *id = get_pd_identity(dev);
148 return sysfs_emit(buf, "0x%08x\n", id->vdo[2]);
150 static DEVICE_ATTR_RO(product_type_vdo3);
152 static struct attribute *usb_pd_id_attrs[] = {
153 &dev_attr_id_header.attr,
154 &dev_attr_cert_stat.attr,
155 &dev_attr_product.attr,
156 &dev_attr_product_type_vdo1.attr,
157 &dev_attr_product_type_vdo2.attr,
158 &dev_attr_product_type_vdo3.attr,
162 static const struct attribute_group usb_pd_id_group = {
164 .attrs = usb_pd_id_attrs,
167 static const struct attribute_group *usb_pd_id_groups[] = {
172 static void typec_product_type_notify(struct device *dev)
177 ptype = get_pd_product_type(dev);
181 sysfs_notify(&dev->kobj, NULL, "type");
183 envp[0] = kasprintf(GFP_KERNEL, "PRODUCT_TYPE=%s", ptype);
187 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
191 static void typec_report_identity(struct device *dev)
193 sysfs_notify(&dev->kobj, "identity", "id_header");
194 sysfs_notify(&dev->kobj, "identity", "cert_stat");
195 sysfs_notify(&dev->kobj, "identity", "product");
196 sysfs_notify(&dev->kobj, "identity", "product_type_vdo1");
197 sysfs_notify(&dev->kobj, "identity", "product_type_vdo2");
198 sysfs_notify(&dev->kobj, "identity", "product_type_vdo3");
199 typec_product_type_notify(dev);
203 type_show(struct device *dev, struct device_attribute *attr, char *buf)
207 ptype = get_pd_product_type(dev);
211 return sysfs_emit(buf, "%s\n", ptype);
213 static DEVICE_ATTR_RO(type);
215 static ssize_t usb_power_delivery_revision_show(struct device *dev,
216 struct device_attribute *attr,
218 static DEVICE_ATTR_RO(usb_power_delivery_revision);
220 /* ------------------------------------------------------------------------- */
221 /* Alternate Modes */
223 static int altmode_match(struct device *dev, void *data)
225 struct typec_altmode *adev = to_typec_altmode(dev);
226 struct typec_device_id *id = data;
228 if (!is_typec_altmode(dev))
231 return ((adev->svid == id->svid) && (adev->mode == id->mode));
234 static void typec_altmode_set_partner(struct altmode *altmode)
236 struct typec_altmode *adev = &altmode->adev;
237 struct typec_device_id id = { adev->svid, adev->mode, };
238 struct typec_port *port = typec_altmode2port(adev);
239 struct altmode *partner;
242 dev = device_find_child(&port->dev, &id, altmode_match);
246 /* Bind the port alt mode to the partner/plug alt mode. */
247 partner = to_altmode(to_typec_altmode(dev));
248 altmode->partner = partner;
250 /* Bind the partner/plug alt mode to the port alt mode. */
251 if (is_typec_plug(adev->dev.parent)) {
252 struct typec_plug *plug = to_typec_plug(adev->dev.parent);
254 partner->plug[plug->index] = altmode;
256 partner->partner = altmode;
260 static void typec_altmode_put_partner(struct altmode *altmode)
262 struct altmode *partner = altmode->partner;
263 struct typec_altmode *adev;
268 adev = &partner->adev;
270 if (is_typec_plug(adev->dev.parent)) {
271 struct typec_plug *plug = to_typec_plug(adev->dev.parent);
273 partner->plug[plug->index] = NULL;
275 partner->partner = NULL;
277 put_device(&adev->dev);
281 * typec_altmode_update_active - Report Enter/Exit mode
282 * @adev: Handle to the alternate mode
283 * @active: True when the mode has been entered
285 * If a partner or cable plug executes Enter/Exit Mode command successfully, the
286 * drivers use this routine to report the updated state of the mode.
288 void typec_altmode_update_active(struct typec_altmode *adev, bool active)
292 if (adev->active == active)
295 if (!is_typec_port(adev->dev.parent) && adev->dev.driver) {
297 module_put(adev->dev.driver->owner);
299 WARN_ON(!try_module_get(adev->dev.driver->owner));
302 adev->active = active;
303 snprintf(dir, sizeof(dir), "mode%d", adev->mode);
304 sysfs_notify(&adev->dev.kobj, dir, "active");
305 sysfs_notify(&adev->dev.kobj, NULL, "active");
306 kobject_uevent(&adev->dev.kobj, KOBJ_CHANGE);
308 EXPORT_SYMBOL_GPL(typec_altmode_update_active);
311 * typec_altmode2port - Alternate Mode to USB Type-C port
312 * @alt: The Alternate Mode
314 * Returns handle to the port that a cable plug or partner with @alt is
317 struct typec_port *typec_altmode2port(struct typec_altmode *alt)
319 if (is_typec_plug(alt->dev.parent))
320 return to_typec_port(alt->dev.parent->parent->parent);
321 if (is_typec_partner(alt->dev.parent))
322 return to_typec_port(alt->dev.parent->parent);
323 if (is_typec_port(alt->dev.parent))
324 return to_typec_port(alt->dev.parent);
328 EXPORT_SYMBOL_GPL(typec_altmode2port);
331 vdo_show(struct device *dev, struct device_attribute *attr, char *buf)
333 struct typec_altmode *alt = to_typec_altmode(dev);
335 return sprintf(buf, "0x%08x\n", alt->vdo);
337 static DEVICE_ATTR_RO(vdo);
340 description_show(struct device *dev, struct device_attribute *attr, char *buf)
342 struct typec_altmode *alt = to_typec_altmode(dev);
344 return sprintf(buf, "%s\n", alt->desc ? alt->desc : "");
346 static DEVICE_ATTR_RO(description);
349 active_show(struct device *dev, struct device_attribute *attr, char *buf)
351 struct typec_altmode *alt = to_typec_altmode(dev);
353 return sprintf(buf, "%s\n", alt->active ? "yes" : "no");
356 static ssize_t active_store(struct device *dev, struct device_attribute *attr,
357 const char *buf, size_t size)
359 struct typec_altmode *adev = to_typec_altmode(dev);
360 struct altmode *altmode = to_altmode(adev);
364 ret = kstrtobool(buf, &enter);
368 if (adev->active == enter)
371 if (is_typec_port(adev->dev.parent)) {
372 typec_altmode_update_active(adev, enter);
374 /* Make sure that the partner exits the mode before disabling */
375 if (altmode->partner && !enter && altmode->partner->adev.active)
376 typec_altmode_exit(&altmode->partner->adev);
377 } else if (altmode->partner) {
378 if (enter && !altmode->partner->adev.active) {
379 dev_warn(dev, "port has the mode disabled\n");
384 /* Note: If there is no driver, the mode will not be entered */
385 if (adev->ops && adev->ops->activate) {
386 ret = adev->ops->activate(adev, enter);
393 static DEVICE_ATTR_RW(active);
396 supported_roles_show(struct device *dev, struct device_attribute *attr,
399 struct altmode *alt = to_altmode(to_typec_altmode(dev));
402 switch (alt->roles) {
404 ret = sprintf(buf, "source\n");
407 ret = sprintf(buf, "sink\n");
411 ret = sprintf(buf, "source sink\n");
416 static DEVICE_ATTR_RO(supported_roles);
419 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
421 struct typec_altmode *adev = to_typec_altmode(dev);
423 return sprintf(buf, "%u\n", adev->mode);
425 static DEVICE_ATTR_RO(mode);
428 svid_show(struct device *dev, struct device_attribute *attr, char *buf)
430 struct typec_altmode *adev = to_typec_altmode(dev);
432 return sprintf(buf, "%04x\n", adev->svid);
434 static DEVICE_ATTR_RO(svid);
436 static struct attribute *typec_altmode_attrs[] = {
437 &dev_attr_active.attr,
444 static umode_t typec_altmode_attr_is_visible(struct kobject *kobj,
445 struct attribute *attr, int n)
447 struct typec_altmode *adev = to_typec_altmode(kobj_to_dev(kobj));
449 if (attr == &dev_attr_active.attr)
450 if (!adev->ops || !adev->ops->activate)
456 static const struct attribute_group typec_altmode_group = {
457 .is_visible = typec_altmode_attr_is_visible,
458 .attrs = typec_altmode_attrs,
461 static const struct attribute_group *typec_altmode_groups[] = {
462 &typec_altmode_group,
466 static int altmode_id_get(struct device *dev)
470 if (is_typec_partner(dev))
471 ids = &to_typec_partner(dev)->mode_ids;
472 else if (is_typec_plug(dev))
473 ids = &to_typec_plug(dev)->mode_ids;
475 ids = &to_typec_port(dev)->mode_ids;
477 return ida_simple_get(ids, 0, 0, GFP_KERNEL);
480 static void altmode_id_remove(struct device *dev, int id)
484 if (is_typec_partner(dev))
485 ids = &to_typec_partner(dev)->mode_ids;
486 else if (is_typec_plug(dev))
487 ids = &to_typec_plug(dev)->mode_ids;
489 ids = &to_typec_port(dev)->mode_ids;
491 ida_simple_remove(ids, id);
494 static void typec_altmode_release(struct device *dev)
496 struct altmode *alt = to_altmode(to_typec_altmode(dev));
498 typec_altmode_put_partner(alt);
500 altmode_id_remove(alt->adev.dev.parent, alt->id);
504 const struct device_type typec_altmode_dev_type = {
505 .name = "typec_alternate_mode",
506 .groups = typec_altmode_groups,
507 .release = typec_altmode_release,
510 static struct typec_altmode *
511 typec_register_altmode(struct device *parent,
512 const struct typec_altmode_desc *desc)
514 unsigned int id = altmode_id_get(parent);
515 bool is_port = is_typec_port(parent);
519 alt = kzalloc(sizeof(*alt), GFP_KERNEL);
521 altmode_id_remove(parent, id);
522 return ERR_PTR(-ENOMEM);
525 alt->adev.svid = desc->svid;
526 alt->adev.mode = desc->mode;
527 alt->adev.vdo = desc->vdo;
528 alt->roles = desc->roles;
531 alt->attrs[0] = &dev_attr_vdo.attr;
532 alt->attrs[1] = &dev_attr_description.attr;
533 alt->attrs[2] = &dev_attr_active.attr;
536 alt->attrs[3] = &dev_attr_supported_roles.attr;
537 alt->adev.active = true; /* Enabled by default */
540 sprintf(alt->group_name, "mode%d", desc->mode);
541 alt->group.name = alt->group_name;
542 alt->group.attrs = alt->attrs;
543 alt->groups[0] = &alt->group;
545 alt->adev.dev.parent = parent;
546 alt->adev.dev.groups = alt->groups;
547 alt->adev.dev.type = &typec_altmode_dev_type;
548 dev_set_name(&alt->adev.dev, "%s.%u", dev_name(parent), id);
550 /* Link partners and plugs with the ports */
552 typec_altmode_set_partner(alt);
554 /* The partners are bind to drivers */
555 if (is_typec_partner(parent))
556 alt->adev.dev.bus = &typec_bus;
558 /* Plug alt modes need a class to generate udev events. */
559 if (is_typec_plug(parent))
560 alt->adev.dev.class = &typec_class;
562 ret = device_register(&alt->adev.dev);
564 dev_err(parent, "failed to register alternate mode (%d)\n",
566 put_device(&alt->adev.dev);
574 * typec_unregister_altmode - Unregister Alternate Mode
575 * @adev: The alternate mode to be unregistered
577 * Unregister device created with typec_partner_register_altmode(),
578 * typec_plug_register_altmode() or typec_port_register_altmode().
580 void typec_unregister_altmode(struct typec_altmode *adev)
582 if (IS_ERR_OR_NULL(adev))
584 typec_mux_put(to_altmode(adev)->mux);
585 device_unregister(&adev->dev);
587 EXPORT_SYMBOL_GPL(typec_unregister_altmode);
589 /* ------------------------------------------------------------------------- */
590 /* Type-C Partners */
592 static ssize_t accessory_mode_show(struct device *dev,
593 struct device_attribute *attr,
596 struct typec_partner *p = to_typec_partner(dev);
598 return sprintf(buf, "%s\n", typec_accessory_modes[p->accessory]);
600 static DEVICE_ATTR_RO(accessory_mode);
602 static ssize_t supports_usb_power_delivery_show(struct device *dev,
603 struct device_attribute *attr,
606 struct typec_partner *p = to_typec_partner(dev);
608 return sprintf(buf, "%s\n", p->usb_pd ? "yes" : "no");
610 static DEVICE_ATTR_RO(supports_usb_power_delivery);
612 static ssize_t number_of_alternate_modes_show(struct device *dev, struct device_attribute *attr,
615 struct typec_partner *partner;
616 struct typec_plug *plug;
619 if (is_typec_partner(dev)) {
620 partner = to_typec_partner(dev);
621 num_altmodes = partner->num_altmodes;
622 } else if (is_typec_plug(dev)) {
623 plug = to_typec_plug(dev);
624 num_altmodes = plug->num_altmodes;
629 return sysfs_emit(buf, "%d\n", num_altmodes);
631 static DEVICE_ATTR_RO(number_of_alternate_modes);
633 static struct attribute *typec_partner_attrs[] = {
634 &dev_attr_accessory_mode.attr,
635 &dev_attr_supports_usb_power_delivery.attr,
636 &dev_attr_number_of_alternate_modes.attr,
638 &dev_attr_usb_power_delivery_revision.attr,
642 static umode_t typec_partner_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
644 struct typec_partner *partner = to_typec_partner(kobj_to_dev(kobj));
646 if (attr == &dev_attr_number_of_alternate_modes.attr) {
647 if (partner->num_altmodes < 0)
651 if (attr == &dev_attr_type.attr)
652 if (!get_pd_product_type(kobj_to_dev(kobj)))
658 static const struct attribute_group typec_partner_group = {
659 .is_visible = typec_partner_attr_is_visible,
660 .attrs = typec_partner_attrs
663 static const struct attribute_group *typec_partner_groups[] = {
664 &typec_partner_group,
668 static void typec_partner_release(struct device *dev)
670 struct typec_partner *partner = to_typec_partner(dev);
672 ida_destroy(&partner->mode_ids);
676 const struct device_type typec_partner_dev_type = {
677 .name = "typec_partner",
678 .groups = typec_partner_groups,
679 .release = typec_partner_release,
683 * typec_partner_set_identity - Report result from Discover Identity command
684 * @partner: The partner updated identity values
686 * This routine is used to report that the result of Discover Identity USB power
687 * delivery command has become available.
689 int typec_partner_set_identity(struct typec_partner *partner)
691 if (!partner->identity)
694 typec_report_identity(&partner->dev);
697 EXPORT_SYMBOL_GPL(typec_partner_set_identity);
700 * typec_partner_set_pd_revision - Set the PD revision supported by the partner
701 * @partner: The partner to be updated.
702 * @pd_revision: USB Power Delivery Specification Revision supported by partner
704 * This routine is used to report that the PD revision of the port partner has
707 void typec_partner_set_pd_revision(struct typec_partner *partner, u16 pd_revision)
709 if (partner->pd_revision == pd_revision)
712 partner->pd_revision = pd_revision;
713 sysfs_notify(&partner->dev.kobj, NULL, "usb_power_delivery_revision");
714 if (pd_revision != 0 && !partner->usb_pd) {
716 sysfs_notify(&partner->dev.kobj, NULL,
717 "supports_usb_power_delivery");
719 kobject_uevent(&partner->dev.kobj, KOBJ_CHANGE);
721 EXPORT_SYMBOL_GPL(typec_partner_set_pd_revision);
724 * typec_partner_set_num_altmodes - Set the number of available partner altmodes
725 * @partner: The partner to be updated.
726 * @num_altmodes: The number of altmodes we want to specify as available.
728 * This routine is used to report the number of alternate modes supported by the
729 * partner. This value is *not* enforced in alternate mode registration routines.
731 * @partner.num_altmodes is set to -1 on partner registration, denoting that
732 * a valid value has not been set for it yet.
734 * Returns 0 on success or negative error number on failure.
736 int typec_partner_set_num_altmodes(struct typec_partner *partner, int num_altmodes)
740 if (num_altmodes < 0)
743 partner->num_altmodes = num_altmodes;
744 ret = sysfs_update_group(&partner->dev.kobj, &typec_partner_group);
748 sysfs_notify(&partner->dev.kobj, NULL, "number_of_alternate_modes");
749 kobject_uevent(&partner->dev.kobj, KOBJ_CHANGE);
753 EXPORT_SYMBOL_GPL(typec_partner_set_num_altmodes);
756 * typec_partner_register_altmode - Register USB Type-C Partner Alternate Mode
757 * @partner: USB Type-C Partner that supports the alternate mode
758 * @desc: Description of the alternate mode
760 * This routine is used to register each alternate mode individually that
761 * @partner has listed in response to Discover SVIDs command. The modes for a
762 * SVID listed in response to Discover Modes command need to be listed in an
765 * Returns handle to the alternate mode on success or ERR_PTR on failure.
767 struct typec_altmode *
768 typec_partner_register_altmode(struct typec_partner *partner,
769 const struct typec_altmode_desc *desc)
771 return typec_register_altmode(&partner->dev, desc);
773 EXPORT_SYMBOL_GPL(typec_partner_register_altmode);
776 * typec_partner_set_svdm_version - Set negotiated Structured VDM (SVDM) Version
777 * @partner: USB Type-C Partner that supports SVDM
778 * @svdm_version: Negotiated SVDM Version
780 * This routine is used to save the negotiated SVDM Version.
782 void typec_partner_set_svdm_version(struct typec_partner *partner,
783 enum usb_pd_svdm_ver svdm_version)
785 partner->svdm_version = svdm_version;
787 EXPORT_SYMBOL_GPL(typec_partner_set_svdm_version);
790 * typec_register_partner - Register a USB Type-C Partner
791 * @port: The USB Type-C Port the partner is connected to
792 * @desc: Description of the partner
794 * Registers a device for USB Type-C Partner described in @desc.
796 * Returns handle to the partner on success or ERR_PTR on failure.
798 struct typec_partner *typec_register_partner(struct typec_port *port,
799 struct typec_partner_desc *desc)
801 struct typec_partner *partner;
804 partner = kzalloc(sizeof(*partner), GFP_KERNEL);
806 return ERR_PTR(-ENOMEM);
808 ida_init(&partner->mode_ids);
809 partner->usb_pd = desc->usb_pd;
810 partner->accessory = desc->accessory;
811 partner->num_altmodes = -1;
812 partner->pd_revision = desc->pd_revision;
813 partner->svdm_version = port->cap->svdm_version;
815 if (desc->identity) {
817 * Creating directory for the identity only if the driver is
818 * able to provide data to it.
820 partner->dev.groups = usb_pd_id_groups;
821 partner->identity = desc->identity;
824 partner->dev.class = &typec_class;
825 partner->dev.parent = &port->dev;
826 partner->dev.type = &typec_partner_dev_type;
827 dev_set_name(&partner->dev, "%s-partner", dev_name(&port->dev));
829 ret = device_register(&partner->dev);
831 dev_err(&port->dev, "failed to register partner (%d)\n", ret);
832 put_device(&partner->dev);
838 EXPORT_SYMBOL_GPL(typec_register_partner);
841 * typec_unregister_partner - Unregister a USB Type-C Partner
842 * @partner: The partner to be unregistered
844 * Unregister device created with typec_register_partner().
846 void typec_unregister_partner(struct typec_partner *partner)
848 if (!IS_ERR_OR_NULL(partner))
849 device_unregister(&partner->dev);
851 EXPORT_SYMBOL_GPL(typec_unregister_partner);
853 /* ------------------------------------------------------------------------- */
854 /* Type-C Cable Plugs */
856 static void typec_plug_release(struct device *dev)
858 struct typec_plug *plug = to_typec_plug(dev);
860 ida_destroy(&plug->mode_ids);
864 static struct attribute *typec_plug_attrs[] = {
865 &dev_attr_number_of_alternate_modes.attr,
869 static umode_t typec_plug_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
871 struct typec_plug *plug = to_typec_plug(kobj_to_dev(kobj));
873 if (attr == &dev_attr_number_of_alternate_modes.attr) {
874 if (plug->num_altmodes < 0)
881 static const struct attribute_group typec_plug_group = {
882 .is_visible = typec_plug_attr_is_visible,
883 .attrs = typec_plug_attrs
886 static const struct attribute_group *typec_plug_groups[] = {
891 const struct device_type typec_plug_dev_type = {
892 .name = "typec_plug",
893 .groups = typec_plug_groups,
894 .release = typec_plug_release,
898 * typec_plug_set_num_altmodes - Set the number of available plug altmodes
899 * @plug: The plug to be updated.
900 * @num_altmodes: The number of altmodes we want to specify as available.
902 * This routine is used to report the number of alternate modes supported by the
903 * plug. This value is *not* enforced in alternate mode registration routines.
905 * @plug.num_altmodes is set to -1 on plug registration, denoting that
906 * a valid value has not been set for it yet.
908 * Returns 0 on success or negative error number on failure.
910 int typec_plug_set_num_altmodes(struct typec_plug *plug, int num_altmodes)
914 if (num_altmodes < 0)
917 plug->num_altmodes = num_altmodes;
918 ret = sysfs_update_group(&plug->dev.kobj, &typec_plug_group);
922 sysfs_notify(&plug->dev.kobj, NULL, "number_of_alternate_modes");
923 kobject_uevent(&plug->dev.kobj, KOBJ_CHANGE);
927 EXPORT_SYMBOL_GPL(typec_plug_set_num_altmodes);
930 * typec_plug_register_altmode - Register USB Type-C Cable Plug Alternate Mode
931 * @plug: USB Type-C Cable Plug that supports the alternate mode
932 * @desc: Description of the alternate mode
934 * This routine is used to register each alternate mode individually that @plug
935 * has listed in response to Discover SVIDs command. The modes for a SVID that
936 * the plug lists in response to Discover Modes command need to be listed in an
939 * Returns handle to the alternate mode on success or ERR_PTR on failure.
941 struct typec_altmode *
942 typec_plug_register_altmode(struct typec_plug *plug,
943 const struct typec_altmode_desc *desc)
945 return typec_register_altmode(&plug->dev, desc);
947 EXPORT_SYMBOL_GPL(typec_plug_register_altmode);
950 * typec_register_plug - Register a USB Type-C Cable Plug
951 * @cable: USB Type-C Cable with the plug
952 * @desc: Description of the cable plug
954 * Registers a device for USB Type-C Cable Plug described in @desc. A USB Type-C
955 * Cable Plug represents a plug with electronics in it that can response to USB
956 * Power Delivery SOP Prime or SOP Double Prime packages.
958 * Returns handle to the cable plug on success or ERR_PTR on failure.
960 struct typec_plug *typec_register_plug(struct typec_cable *cable,
961 struct typec_plug_desc *desc)
963 struct typec_plug *plug;
967 plug = kzalloc(sizeof(*plug), GFP_KERNEL);
969 return ERR_PTR(-ENOMEM);
971 sprintf(name, "plug%d", desc->index);
973 ida_init(&plug->mode_ids);
974 plug->num_altmodes = -1;
975 plug->index = desc->index;
976 plug->dev.class = &typec_class;
977 plug->dev.parent = &cable->dev;
978 plug->dev.type = &typec_plug_dev_type;
979 dev_set_name(&plug->dev, "%s-%s", dev_name(cable->dev.parent), name);
981 ret = device_register(&plug->dev);
983 dev_err(&cable->dev, "failed to register plug (%d)\n", ret);
984 put_device(&plug->dev);
990 EXPORT_SYMBOL_GPL(typec_register_plug);
993 * typec_unregister_plug - Unregister a USB Type-C Cable Plug
994 * @plug: The cable plug to be unregistered
996 * Unregister device created with typec_register_plug().
998 void typec_unregister_plug(struct typec_plug *plug)
1000 if (!IS_ERR_OR_NULL(plug))
1001 device_unregister(&plug->dev);
1003 EXPORT_SYMBOL_GPL(typec_unregister_plug);
1007 static const char * const typec_plug_types[] = {
1008 [USB_PLUG_NONE] = "unknown",
1009 [USB_PLUG_TYPE_A] = "type-a",
1010 [USB_PLUG_TYPE_B] = "type-b",
1011 [USB_PLUG_TYPE_C] = "type-c",
1012 [USB_PLUG_CAPTIVE] = "captive",
1015 static ssize_t plug_type_show(struct device *dev,
1016 struct device_attribute *attr, char *buf)
1018 struct typec_cable *cable = to_typec_cable(dev);
1020 return sprintf(buf, "%s\n", typec_plug_types[cable->type]);
1022 static DEVICE_ATTR_RO(plug_type);
1024 static struct attribute *typec_cable_attrs[] = {
1025 &dev_attr_type.attr,
1026 &dev_attr_plug_type.attr,
1027 &dev_attr_usb_power_delivery_revision.attr,
1030 ATTRIBUTE_GROUPS(typec_cable);
1032 static void typec_cable_release(struct device *dev)
1034 struct typec_cable *cable = to_typec_cable(dev);
1039 const struct device_type typec_cable_dev_type = {
1040 .name = "typec_cable",
1041 .groups = typec_cable_groups,
1042 .release = typec_cable_release,
1045 static int cable_match(struct device *dev, void *data)
1047 return is_typec_cable(dev);
1051 * typec_cable_get - Get a reference to the USB Type-C cable
1052 * @port: The USB Type-C Port the cable is connected to
1054 * The caller must decrement the reference count with typec_cable_put() after
1057 struct typec_cable *typec_cable_get(struct typec_port *port)
1061 dev = device_find_child(&port->dev, NULL, cable_match);
1065 return to_typec_cable(dev);
1067 EXPORT_SYMBOL_GPL(typec_cable_get);
1070 * typec_cable_put - Decrement the reference count on USB Type-C cable
1071 * @cable: The USB Type-C cable
1073 void typec_cable_put(struct typec_cable *cable)
1075 put_device(&cable->dev);
1077 EXPORT_SYMBOL_GPL(typec_cable_put);
1080 * typec_cable_is_active - Check is the USB Type-C cable active or passive
1081 * @cable: The USB Type-C Cable
1083 * Return 1 if the cable is active or 0 if it's passive.
1085 int typec_cable_is_active(struct typec_cable *cable)
1087 return cable->active;
1089 EXPORT_SYMBOL_GPL(typec_cable_is_active);
1092 * typec_cable_set_identity - Report result from Discover Identity command
1093 * @cable: The cable updated identity values
1095 * This routine is used to report that the result of Discover Identity USB power
1096 * delivery command has become available.
1098 int typec_cable_set_identity(struct typec_cable *cable)
1100 if (!cable->identity)
1103 typec_report_identity(&cable->dev);
1106 EXPORT_SYMBOL_GPL(typec_cable_set_identity);
1109 * typec_register_cable - Register a USB Type-C Cable
1110 * @port: The USB Type-C Port the cable is connected to
1111 * @desc: Description of the cable
1113 * Registers a device for USB Type-C Cable described in @desc. The cable will be
1114 * parent for the optional cable plug devises.
1116 * Returns handle to the cable on success or ERR_PTR on failure.
1118 struct typec_cable *typec_register_cable(struct typec_port *port,
1119 struct typec_cable_desc *desc)
1121 struct typec_cable *cable;
1124 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
1126 return ERR_PTR(-ENOMEM);
1128 cable->type = desc->type;
1129 cable->active = desc->active;
1130 cable->pd_revision = desc->pd_revision;
1132 if (desc->identity) {
1134 * Creating directory for the identity only if the driver is
1135 * able to provide data to it.
1137 cable->dev.groups = usb_pd_id_groups;
1138 cable->identity = desc->identity;
1141 cable->dev.class = &typec_class;
1142 cable->dev.parent = &port->dev;
1143 cable->dev.type = &typec_cable_dev_type;
1144 dev_set_name(&cable->dev, "%s-cable", dev_name(&port->dev));
1146 ret = device_register(&cable->dev);
1148 dev_err(&port->dev, "failed to register cable (%d)\n", ret);
1149 put_device(&cable->dev);
1150 return ERR_PTR(ret);
1155 EXPORT_SYMBOL_GPL(typec_register_cable);
1158 * typec_unregister_cable - Unregister a USB Type-C Cable
1159 * @cable: The cable to be unregistered
1161 * Unregister device created with typec_register_cable().
1163 void typec_unregister_cable(struct typec_cable *cable)
1165 if (!IS_ERR_OR_NULL(cable))
1166 device_unregister(&cable->dev);
1168 EXPORT_SYMBOL_GPL(typec_unregister_cable);
1170 /* ------------------------------------------------------------------------- */
1171 /* USB Type-C ports */
1173 static const char * const typec_orientations[] = {
1174 [TYPEC_ORIENTATION_NONE] = "unknown",
1175 [TYPEC_ORIENTATION_NORMAL] = "normal",
1176 [TYPEC_ORIENTATION_REVERSE] = "reverse",
1179 static const char * const typec_roles[] = {
1180 [TYPEC_SINK] = "sink",
1181 [TYPEC_SOURCE] = "source",
1184 static const char * const typec_data_roles[] = {
1185 [TYPEC_DEVICE] = "device",
1186 [TYPEC_HOST] = "host",
1189 static const char * const typec_port_power_roles[] = {
1190 [TYPEC_PORT_SRC] = "source",
1191 [TYPEC_PORT_SNK] = "sink",
1192 [TYPEC_PORT_DRP] = "dual",
1195 static const char * const typec_port_data_roles[] = {
1196 [TYPEC_PORT_DFP] = "host",
1197 [TYPEC_PORT_UFP] = "device",
1198 [TYPEC_PORT_DRD] = "dual",
1201 static const char * const typec_port_types_drp[] = {
1202 [TYPEC_PORT_SRC] = "dual [source] sink",
1203 [TYPEC_PORT_SNK] = "dual source [sink]",
1204 [TYPEC_PORT_DRP] = "[dual] source sink",
1208 preferred_role_store(struct device *dev, struct device_attribute *attr,
1209 const char *buf, size_t size)
1211 struct typec_port *port = to_typec_port(dev);
1215 if (port->cap->type != TYPEC_PORT_DRP) {
1216 dev_dbg(dev, "Preferred role only supported with DRP ports\n");
1220 if (!port->ops || !port->ops->try_role) {
1221 dev_dbg(dev, "Setting preferred role not supported\n");
1225 role = sysfs_match_string(typec_roles, buf);
1227 if (sysfs_streq(buf, "none"))
1228 role = TYPEC_NO_PREFERRED_ROLE;
1233 ret = port->ops->try_role(port, role);
1237 port->prefer_role = role;
1242 preferred_role_show(struct device *dev, struct device_attribute *attr,
1245 struct typec_port *port = to_typec_port(dev);
1247 if (port->cap->type != TYPEC_PORT_DRP)
1250 if (port->prefer_role < 0)
1253 return sprintf(buf, "%s\n", typec_roles[port->prefer_role]);
1255 static DEVICE_ATTR_RW(preferred_role);
1257 static ssize_t data_role_store(struct device *dev,
1258 struct device_attribute *attr,
1259 const char *buf, size_t size)
1261 struct typec_port *port = to_typec_port(dev);
1264 if (!port->ops || !port->ops->dr_set) {
1265 dev_dbg(dev, "data role swapping not supported\n");
1269 ret = sysfs_match_string(typec_data_roles, buf);
1273 mutex_lock(&port->port_type_lock);
1274 if (port->cap->data != TYPEC_PORT_DRD) {
1276 goto unlock_and_ret;
1279 ret = port->ops->dr_set(port, ret);
1281 goto unlock_and_ret;
1285 mutex_unlock(&port->port_type_lock);
1289 static ssize_t data_role_show(struct device *dev,
1290 struct device_attribute *attr, char *buf)
1292 struct typec_port *port = to_typec_port(dev);
1294 if (port->cap->data == TYPEC_PORT_DRD)
1295 return sprintf(buf, "%s\n", port->data_role == TYPEC_HOST ?
1296 "[host] device" : "host [device]");
1298 return sprintf(buf, "[%s]\n", typec_data_roles[port->data_role]);
1300 static DEVICE_ATTR_RW(data_role);
1302 static ssize_t power_role_store(struct device *dev,
1303 struct device_attribute *attr,
1304 const char *buf, size_t size)
1306 struct typec_port *port = to_typec_port(dev);
1309 if (!port->ops || !port->ops->pr_set) {
1310 dev_dbg(dev, "power role swapping not supported\n");
1314 if (port->pwr_opmode != TYPEC_PWR_MODE_PD) {
1315 dev_dbg(dev, "partner unable to swap power role\n");
1319 ret = sysfs_match_string(typec_roles, buf);
1323 mutex_lock(&port->port_type_lock);
1324 if (port->port_type != TYPEC_PORT_DRP) {
1325 dev_dbg(dev, "port type fixed at \"%s\"",
1326 typec_port_power_roles[port->port_type]);
1328 goto unlock_and_ret;
1331 ret = port->ops->pr_set(port, ret);
1333 goto unlock_and_ret;
1337 mutex_unlock(&port->port_type_lock);
1341 static ssize_t power_role_show(struct device *dev,
1342 struct device_attribute *attr, char *buf)
1344 struct typec_port *port = to_typec_port(dev);
1346 if (port->cap->type == TYPEC_PORT_DRP)
1347 return sprintf(buf, "%s\n", port->pwr_role == TYPEC_SOURCE ?
1348 "[source] sink" : "source [sink]");
1350 return sprintf(buf, "[%s]\n", typec_roles[port->pwr_role]);
1352 static DEVICE_ATTR_RW(power_role);
1355 port_type_store(struct device *dev, struct device_attribute *attr,
1356 const char *buf, size_t size)
1358 struct typec_port *port = to_typec_port(dev);
1360 enum typec_port_type type;
1362 if (port->cap->type != TYPEC_PORT_DRP ||
1363 !port->ops || !port->ops->port_type_set) {
1364 dev_dbg(dev, "changing port type not supported\n");
1368 ret = sysfs_match_string(typec_port_power_roles, buf);
1373 mutex_lock(&port->port_type_lock);
1375 if (port->port_type == type) {
1377 goto unlock_and_ret;
1380 ret = port->ops->port_type_set(port, type);
1382 goto unlock_and_ret;
1384 port->port_type = type;
1388 mutex_unlock(&port->port_type_lock);
1393 port_type_show(struct device *dev, struct device_attribute *attr,
1396 struct typec_port *port = to_typec_port(dev);
1398 if (port->cap->type == TYPEC_PORT_DRP)
1399 return sprintf(buf, "%s\n",
1400 typec_port_types_drp[port->port_type]);
1402 return sprintf(buf, "[%s]\n", typec_port_power_roles[port->cap->type]);
1404 static DEVICE_ATTR_RW(port_type);
1406 static const char * const typec_pwr_opmodes[] = {
1407 [TYPEC_PWR_MODE_USB] = "default",
1408 [TYPEC_PWR_MODE_1_5A] = "1.5A",
1409 [TYPEC_PWR_MODE_3_0A] = "3.0A",
1410 [TYPEC_PWR_MODE_PD] = "usb_power_delivery",
1413 static ssize_t power_operation_mode_show(struct device *dev,
1414 struct device_attribute *attr,
1417 struct typec_port *port = to_typec_port(dev);
1419 return sprintf(buf, "%s\n", typec_pwr_opmodes[port->pwr_opmode]);
1421 static DEVICE_ATTR_RO(power_operation_mode);
1423 static ssize_t vconn_source_store(struct device *dev,
1424 struct device_attribute *attr,
1425 const char *buf, size_t size)
1427 struct typec_port *port = to_typec_port(dev);
1431 if (!port->cap->pd_revision) {
1432 dev_dbg(dev, "VCONN swap depends on USB Power Delivery\n");
1436 if (!port->ops || !port->ops->vconn_set) {
1437 dev_dbg(dev, "VCONN swapping not supported\n");
1441 ret = kstrtobool(buf, &source);
1445 ret = port->ops->vconn_set(port, (enum typec_role)source);
1452 static ssize_t vconn_source_show(struct device *dev,
1453 struct device_attribute *attr, char *buf)
1455 struct typec_port *port = to_typec_port(dev);
1457 return sprintf(buf, "%s\n",
1458 port->vconn_role == TYPEC_SOURCE ? "yes" : "no");
1460 static DEVICE_ATTR_RW(vconn_source);
1462 static ssize_t supported_accessory_modes_show(struct device *dev,
1463 struct device_attribute *attr,
1466 struct typec_port *port = to_typec_port(dev);
1470 for (i = 0; i < ARRAY_SIZE(port->cap->accessory); i++) {
1471 if (port->cap->accessory[i])
1472 ret += sprintf(buf + ret, "%s ",
1473 typec_accessory_modes[port->cap->accessory[i]]);
1477 return sprintf(buf, "none\n");
1479 buf[ret - 1] = '\n';
1483 static DEVICE_ATTR_RO(supported_accessory_modes);
1485 static ssize_t usb_typec_revision_show(struct device *dev,
1486 struct device_attribute *attr,
1489 struct typec_port *port = to_typec_port(dev);
1490 u16 rev = port->cap->revision;
1492 return sprintf(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
1494 static DEVICE_ATTR_RO(usb_typec_revision);
1496 static ssize_t usb_power_delivery_revision_show(struct device *dev,
1497 struct device_attribute *attr,
1502 if (is_typec_partner(dev)) {
1503 struct typec_partner *partner = to_typec_partner(dev);
1505 rev = partner->pd_revision;
1506 } else if (is_typec_cable(dev)) {
1507 struct typec_cable *cable = to_typec_cable(dev);
1509 rev = cable->pd_revision;
1510 } else if (is_typec_port(dev)) {
1511 struct typec_port *p = to_typec_port(dev);
1513 rev = p->cap->pd_revision;
1515 return sysfs_emit(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
1518 static ssize_t orientation_show(struct device *dev,
1519 struct device_attribute *attr,
1522 struct typec_port *port = to_typec_port(dev);
1524 return sprintf(buf, "%s\n", typec_orientations[port->orientation]);
1526 static DEVICE_ATTR_RO(orientation);
1528 static struct attribute *typec_attrs[] = {
1529 &dev_attr_data_role.attr,
1530 &dev_attr_power_operation_mode.attr,
1531 &dev_attr_power_role.attr,
1532 &dev_attr_preferred_role.attr,
1533 &dev_attr_supported_accessory_modes.attr,
1534 &dev_attr_usb_power_delivery_revision.attr,
1535 &dev_attr_usb_typec_revision.attr,
1536 &dev_attr_vconn_source.attr,
1537 &dev_attr_port_type.attr,
1538 &dev_attr_orientation.attr,
1542 static umode_t typec_attr_is_visible(struct kobject *kobj,
1543 struct attribute *attr, int n)
1545 struct typec_port *port = to_typec_port(kobj_to_dev(kobj));
1547 if (attr == &dev_attr_data_role.attr) {
1548 if (port->cap->data != TYPEC_PORT_DRD ||
1549 !port->ops || !port->ops->dr_set)
1551 } else if (attr == &dev_attr_power_role.attr) {
1552 if (port->cap->type != TYPEC_PORT_DRP ||
1553 !port->ops || !port->ops->pr_set)
1555 } else if (attr == &dev_attr_vconn_source.attr) {
1556 if (!port->cap->pd_revision ||
1557 !port->ops || !port->ops->vconn_set)
1559 } else if (attr == &dev_attr_preferred_role.attr) {
1560 if (port->cap->type != TYPEC_PORT_DRP ||
1561 !port->ops || !port->ops->try_role)
1563 } else if (attr == &dev_attr_port_type.attr) {
1564 if (!port->ops || !port->ops->port_type_set)
1566 if (port->cap->type != TYPEC_PORT_DRP)
1568 } else if (attr == &dev_attr_orientation.attr) {
1569 if (port->cap->orientation_aware)
1577 static const struct attribute_group typec_group = {
1578 .is_visible = typec_attr_is_visible,
1579 .attrs = typec_attrs,
1582 static const struct attribute_group *typec_groups[] = {
1587 static int typec_uevent(struct device *dev, struct kobj_uevent_env *env)
1591 ret = add_uevent_var(env, "TYPEC_PORT=%s", dev_name(dev));
1593 dev_err(dev, "failed to add uevent TYPEC_PORT\n");
1598 static void typec_release(struct device *dev)
1600 struct typec_port *port = to_typec_port(dev);
1602 ida_simple_remove(&typec_index_ida, port->id);
1603 ida_destroy(&port->mode_ids);
1604 typec_switch_put(port->sw);
1605 typec_mux_put(port->mux);
1610 const struct device_type typec_port_dev_type = {
1611 .name = "typec_port",
1612 .groups = typec_groups,
1613 .uevent = typec_uevent,
1614 .release = typec_release,
1617 /* --------------------------------------- */
1618 /* Driver callbacks to report role updates */
1620 static int partner_match(struct device *dev, void *data)
1622 return is_typec_partner(dev);
1626 * typec_set_data_role - Report data role change
1627 * @port: The USB Type-C Port where the role was changed
1628 * @role: The new data role
1630 * This routine is used by the port drivers to report data role changes.
1632 void typec_set_data_role(struct typec_port *port, enum typec_data_role role)
1634 struct device *partner_dev;
1636 if (port->data_role == role)
1639 port->data_role = role;
1640 sysfs_notify(&port->dev.kobj, NULL, "data_role");
1641 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1643 partner_dev = device_find_child(&port->dev, NULL, partner_match);
1647 if (to_typec_partner(partner_dev)->identity)
1648 typec_product_type_notify(partner_dev);
1650 put_device(partner_dev);
1652 EXPORT_SYMBOL_GPL(typec_set_data_role);
1655 * typec_set_pwr_role - Report power role change
1656 * @port: The USB Type-C Port where the role was changed
1657 * @role: The new data role
1659 * This routine is used by the port drivers to report power role changes.
1661 void typec_set_pwr_role(struct typec_port *port, enum typec_role role)
1663 if (port->pwr_role == role)
1666 port->pwr_role = role;
1667 sysfs_notify(&port->dev.kobj, NULL, "power_role");
1668 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1670 EXPORT_SYMBOL_GPL(typec_set_pwr_role);
1673 * typec_set_vconn_role - Report VCONN source change
1674 * @port: The USB Type-C Port which VCONN role changed
1675 * @role: Source when @port is sourcing VCONN, or Sink when it's not
1677 * This routine is used by the port drivers to report if the VCONN source is
1680 void typec_set_vconn_role(struct typec_port *port, enum typec_role role)
1682 if (port->vconn_role == role)
1685 port->vconn_role = role;
1686 sysfs_notify(&port->dev.kobj, NULL, "vconn_source");
1687 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1689 EXPORT_SYMBOL_GPL(typec_set_vconn_role);
1692 * typec_set_pwr_opmode - Report changed power operation mode
1693 * @port: The USB Type-C Port where the mode was changed
1694 * @opmode: New power operation mode
1696 * This routine is used by the port drivers to report changed power operation
1697 * mode in @port. The modes are USB (default), 1.5A, 3.0A as defined in USB
1698 * Type-C specification, and "USB Power Delivery" when the power levels are
1699 * negotiated with methods defined in USB Power Delivery specification.
1701 void typec_set_pwr_opmode(struct typec_port *port,
1702 enum typec_pwr_opmode opmode)
1704 struct device *partner_dev;
1706 if (port->pwr_opmode == opmode)
1709 port->pwr_opmode = opmode;
1710 sysfs_notify(&port->dev.kobj, NULL, "power_operation_mode");
1711 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1713 partner_dev = device_find_child(&port->dev, NULL, partner_match);
1715 struct typec_partner *partner = to_typec_partner(partner_dev);
1717 if (opmode == TYPEC_PWR_MODE_PD && !partner->usb_pd) {
1718 partner->usb_pd = 1;
1719 sysfs_notify(&partner_dev->kobj, NULL,
1720 "supports_usb_power_delivery");
1722 put_device(partner_dev);
1725 EXPORT_SYMBOL_GPL(typec_set_pwr_opmode);
1728 * typec_find_pwr_opmode - Get the typec power operation mode capability
1729 * @name: power operation mode string
1731 * This routine is used to find the typec_pwr_opmode by its string @name.
1733 * Returns typec_pwr_opmode if success, otherwise negative error code.
1735 int typec_find_pwr_opmode(const char *name)
1737 return match_string(typec_pwr_opmodes,
1738 ARRAY_SIZE(typec_pwr_opmodes), name);
1740 EXPORT_SYMBOL_GPL(typec_find_pwr_opmode);
1743 * typec_find_orientation - Convert orientation string to enum typec_orientation
1744 * @name: Orientation string
1746 * This routine is used to find the typec_orientation by its string name @name.
1748 * Returns the orientation value on success, otherwise negative error code.
1750 int typec_find_orientation(const char *name)
1752 return match_string(typec_orientations, ARRAY_SIZE(typec_orientations),
1755 EXPORT_SYMBOL_GPL(typec_find_orientation);
1758 * typec_find_port_power_role - Get the typec port power capability
1759 * @name: port power capability string
1761 * This routine is used to find the typec_port_type by its string name.
1763 * Returns typec_port_type if success, otherwise negative error code.
1765 int typec_find_port_power_role(const char *name)
1767 return match_string(typec_port_power_roles,
1768 ARRAY_SIZE(typec_port_power_roles), name);
1770 EXPORT_SYMBOL_GPL(typec_find_port_power_role);
1773 * typec_find_power_role - Find the typec one specific power role
1774 * @name: power role string
1776 * This routine is used to find the typec_role by its string name.
1778 * Returns typec_role if success, otherwise negative error code.
1780 int typec_find_power_role(const char *name)
1782 return match_string(typec_roles, ARRAY_SIZE(typec_roles), name);
1784 EXPORT_SYMBOL_GPL(typec_find_power_role);
1787 * typec_find_port_data_role - Get the typec port data capability
1788 * @name: port data capability string
1790 * This routine is used to find the typec_port_data by its string name.
1792 * Returns typec_port_data if success, otherwise negative error code.
1794 int typec_find_port_data_role(const char *name)
1796 return match_string(typec_port_data_roles,
1797 ARRAY_SIZE(typec_port_data_roles), name);
1799 EXPORT_SYMBOL_GPL(typec_find_port_data_role);
1801 /* ------------------------------------------ */
1802 /* API for Multiplexer/DeMultiplexer Switches */
1805 * typec_set_orientation - Set USB Type-C cable plug orientation
1806 * @port: USB Type-C Port
1807 * @orientation: USB Type-C cable plug orientation
1809 * Set cable plug orientation for @port.
1811 int typec_set_orientation(struct typec_port *port,
1812 enum typec_orientation orientation)
1816 ret = typec_switch_set(port->sw, orientation);
1820 port->orientation = orientation;
1821 sysfs_notify(&port->dev.kobj, NULL, "orientation");
1822 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1826 EXPORT_SYMBOL_GPL(typec_set_orientation);
1829 * typec_get_orientation - Get USB Type-C cable plug orientation
1830 * @port: USB Type-C Port
1832 * Get current cable plug orientation for @port.
1834 enum typec_orientation typec_get_orientation(struct typec_port *port)
1836 return port->orientation;
1838 EXPORT_SYMBOL_GPL(typec_get_orientation);
1841 * typec_set_mode - Set mode of operation for USB Type-C connector
1842 * @port: USB Type-C connector
1843 * @mode: Accessory Mode, USB Operation or Safe State
1845 * Configure @port for Accessory Mode @mode. This function will configure the
1846 * muxes needed for @mode.
1848 int typec_set_mode(struct typec_port *port, int mode)
1850 struct typec_mux_state state = { };
1854 return typec_mux_set(port->mux, &state);
1856 EXPORT_SYMBOL_GPL(typec_set_mode);
1858 /* --------------------------------------- */
1861 * typec_get_negotiated_svdm_version - Get negotiated SVDM Version
1862 * @port: USB Type-C Port.
1864 * Get the negotiated SVDM Version. The Version is set to the port default
1865 * value stored in typec_capability on partner registration, and updated after
1866 * a successful Discover Identity if the negotiated value is less than the
1869 * Returns usb_pd_svdm_ver if the partner has been registered otherwise -ENODEV.
1871 int typec_get_negotiated_svdm_version(struct typec_port *port)
1873 enum usb_pd_svdm_ver svdm_version;
1874 struct device *partner_dev;
1876 partner_dev = device_find_child(&port->dev, NULL, partner_match);
1880 svdm_version = to_typec_partner(partner_dev)->svdm_version;
1881 put_device(partner_dev);
1883 return svdm_version;
1885 EXPORT_SYMBOL_GPL(typec_get_negotiated_svdm_version);
1888 * typec_get_drvdata - Return private driver data pointer
1889 * @port: USB Type-C port
1891 void *typec_get_drvdata(struct typec_port *port)
1893 return dev_get_drvdata(&port->dev);
1895 EXPORT_SYMBOL_GPL(typec_get_drvdata);
1898 * typec_port_register_altmode - Register USB Type-C Port Alternate Mode
1899 * @port: USB Type-C Port that supports the alternate mode
1900 * @desc: Description of the alternate mode
1902 * This routine is used to register an alternate mode that @port is capable of
1905 * Returns handle to the alternate mode on success or ERR_PTR on failure.
1907 struct typec_altmode *
1908 typec_port_register_altmode(struct typec_port *port,
1909 const struct typec_altmode_desc *desc)
1911 struct typec_altmode *adev;
1912 struct typec_mux *mux;
1914 mux = typec_mux_get(&port->dev, desc);
1916 return ERR_CAST(mux);
1918 adev = typec_register_altmode(&port->dev, desc);
1922 to_altmode(adev)->mux = mux;
1926 EXPORT_SYMBOL_GPL(typec_port_register_altmode);
1928 void typec_port_register_altmodes(struct typec_port *port,
1929 const struct typec_altmode_ops *ops, void *drvdata,
1930 struct typec_altmode **altmodes, size_t n)
1932 struct fwnode_handle *altmodes_node, *child;
1933 struct typec_altmode_desc desc;
1934 struct typec_altmode *alt;
1939 altmodes_node = device_get_named_child_node(&port->dev, "altmodes");
1941 return; /* No altmodes specified */
1943 fwnode_for_each_child_node(altmodes_node, child) {
1944 ret = fwnode_property_read_u32(child, "svid", &svid);
1946 dev_err(&port->dev, "Error reading svid for altmode %s\n",
1947 fwnode_get_name(child));
1951 ret = fwnode_property_read_u32(child, "vdo", &vdo);
1953 dev_err(&port->dev, "Error reading vdo for altmode %s\n",
1954 fwnode_get_name(child));
1959 dev_err(&port->dev, "Error not enough space for altmode %s\n",
1960 fwnode_get_name(child));
1966 desc.mode = index + 1;
1967 alt = typec_port_register_altmode(port, &desc);
1969 dev_err(&port->dev, "Error registering altmode %s\n",
1970 fwnode_get_name(child));
1975 typec_altmode_set_drvdata(alt, drvdata);
1976 altmodes[index] = alt;
1980 EXPORT_SYMBOL_GPL(typec_port_register_altmodes);
1983 * typec_register_port - Register a USB Type-C Port
1984 * @parent: Parent device
1985 * @cap: Description of the port
1987 * Registers a device for USB Type-C Port described in @cap.
1989 * Returns handle to the port on success or ERR_PTR on failure.
1991 struct typec_port *typec_register_port(struct device *parent,
1992 const struct typec_capability *cap)
1994 struct typec_port *port;
1998 port = kzalloc(sizeof(*port), GFP_KERNEL);
2000 return ERR_PTR(-ENOMEM);
2002 id = ida_simple_get(&typec_index_ida, 0, 0, GFP_KERNEL);
2008 switch (cap->type) {
2009 case TYPEC_PORT_SRC:
2010 port->pwr_role = TYPEC_SOURCE;
2011 port->vconn_role = TYPEC_SOURCE;
2013 case TYPEC_PORT_SNK:
2014 port->pwr_role = TYPEC_SINK;
2015 port->vconn_role = TYPEC_SINK;
2017 case TYPEC_PORT_DRP:
2018 if (cap->prefer_role != TYPEC_NO_PREFERRED_ROLE)
2019 port->pwr_role = cap->prefer_role;
2021 port->pwr_role = TYPEC_SINK;
2025 switch (cap->data) {
2026 case TYPEC_PORT_DFP:
2027 port->data_role = TYPEC_HOST;
2029 case TYPEC_PORT_UFP:
2030 port->data_role = TYPEC_DEVICE;
2032 case TYPEC_PORT_DRD:
2033 if (cap->prefer_role == TYPEC_SOURCE)
2034 port->data_role = TYPEC_HOST;
2036 port->data_role = TYPEC_DEVICE;
2040 ida_init(&port->mode_ids);
2041 mutex_init(&port->port_type_lock);
2042 mutex_init(&port->port_list_lock);
2043 INIT_LIST_HEAD(&port->port_list);
2046 port->ops = cap->ops;
2047 port->port_type = cap->type;
2048 port->prefer_role = cap->prefer_role;
2050 device_initialize(&port->dev);
2051 port->dev.class = &typec_class;
2052 port->dev.parent = parent;
2053 port->dev.fwnode = cap->fwnode;
2054 port->dev.type = &typec_port_dev_type;
2055 dev_set_name(&port->dev, "port%d", id);
2056 dev_set_drvdata(&port->dev, cap->driver_data);
2058 port->cap = kmemdup(cap, sizeof(*cap), GFP_KERNEL);
2060 put_device(&port->dev);
2061 return ERR_PTR(-ENOMEM);
2064 port->sw = typec_switch_get(&port->dev);
2065 if (IS_ERR(port->sw)) {
2066 ret = PTR_ERR(port->sw);
2067 put_device(&port->dev);
2068 return ERR_PTR(ret);
2071 port->mux = typec_mux_get(&port->dev, NULL);
2072 if (IS_ERR(port->mux)) {
2073 ret = PTR_ERR(port->mux);
2074 put_device(&port->dev);
2075 return ERR_PTR(ret);
2078 ret = device_add(&port->dev);
2080 dev_err(parent, "failed to register port (%d)\n", ret);
2081 put_device(&port->dev);
2082 return ERR_PTR(ret);
2085 ret = typec_link_ports(port);
2087 dev_warn(&port->dev, "failed to create symlinks (%d)\n", ret);
2091 EXPORT_SYMBOL_GPL(typec_register_port);
2094 * typec_unregister_port - Unregister a USB Type-C Port
2095 * @port: The port to be unregistered
2097 * Unregister device created with typec_register_port().
2099 void typec_unregister_port(struct typec_port *port)
2101 if (!IS_ERR_OR_NULL(port)) {
2102 typec_unlink_ports(port);
2103 device_unregister(&port->dev);
2106 EXPORT_SYMBOL_GPL(typec_unregister_port);
2108 static int __init typec_init(void)
2112 ret = bus_register(&typec_bus);
2116 ret = class_register(&typec_mux_class);
2118 goto err_unregister_bus;
2120 ret = class_register(&typec_class);
2122 goto err_unregister_mux_class;
2126 err_unregister_mux_class:
2127 class_unregister(&typec_mux_class);
2130 bus_unregister(&typec_bus);
2134 subsys_initcall(typec_init);
2136 static void __exit typec_exit(void)
2138 class_unregister(&typec_class);
2139 ida_destroy(&typec_index_ida);
2140 bus_unregister(&typec_bus);
2141 class_unregister(&typec_mux_class);
2143 module_exit(typec_exit);
2146 MODULE_LICENSE("GPL v2");
2147 MODULE_DESCRIPTION("USB Type-C Connector Class");