1 // SPDX-License-Identifier: GPL-2.0
3 * USB Type-C Connector Class
5 * Copyright (C) 2017, Intel Corporation
9 #include <linux/device.h>
10 #include <linux/module.h>
11 #include <linux/mutex.h>
12 #include <linux/property.h>
13 #include <linux/slab.h>
14 #include <linux/usb/pd_vdo.h>
20 enum typec_plug_index index;
27 enum typec_plug_type type;
28 struct usb_pd_identity *identity;
29 unsigned int active:1;
30 u16 pd_revision; /* 0300H = "3.0" */
33 struct typec_partner {
35 unsigned int usb_pd:1;
36 struct usb_pd_identity *identity;
37 enum typec_accessory accessory;
40 u16 pd_revision; /* 0300H = "3.0" */
41 enum usb_pd_svdm_ver svdm_version;
50 enum typec_data_role data_role;
51 enum typec_role pwr_role;
52 enum typec_role vconn_role;
53 enum typec_pwr_opmode pwr_opmode;
54 enum typec_port_type port_type;
55 struct mutex port_type_lock;
57 enum typec_orientation orientation;
58 struct typec_switch *sw;
59 struct typec_mux *mux;
61 const struct typec_capability *cap;
62 const struct typec_operations *ops;
65 #define to_typec_port(_dev_) container_of(_dev_, struct typec_port, dev)
66 #define to_typec_plug(_dev_) container_of(_dev_, struct typec_plug, dev)
67 #define to_typec_cable(_dev_) container_of(_dev_, struct typec_cable, dev)
68 #define to_typec_partner(_dev_) container_of(_dev_, struct typec_partner, dev)
70 static const struct device_type typec_partner_dev_type;
71 static const struct device_type typec_cable_dev_type;
72 static const struct device_type typec_plug_dev_type;
74 #define is_typec_partner(_dev_) (_dev_->type == &typec_partner_dev_type)
75 #define is_typec_cable(_dev_) (_dev_->type == &typec_cable_dev_type)
76 #define is_typec_plug(_dev_) (_dev_->type == &typec_plug_dev_type)
78 static DEFINE_IDA(typec_index_ida);
79 static struct class *typec_class;
81 /* ------------------------------------------------------------------------- */
82 /* Common attributes */
84 static const char * const typec_accessory_modes[] = {
85 [TYPEC_ACCESSORY_NONE] = "none",
86 [TYPEC_ACCESSORY_AUDIO] = "analog_audio",
87 [TYPEC_ACCESSORY_DEBUG] = "debug",
90 /* Product types defined in USB PD Specification R3.0 V2.0 */
91 static const char * const product_type_ufp[8] = {
92 [IDH_PTYPE_NOT_UFP] = "not_ufp",
93 [IDH_PTYPE_HUB] = "hub",
94 [IDH_PTYPE_PERIPH] = "peripheral",
95 [IDH_PTYPE_PSD] = "psd",
96 [IDH_PTYPE_AMA] = "ama",
99 static const char * const product_type_dfp[8] = {
100 [IDH_PTYPE_NOT_DFP] = "not_dfp",
101 [IDH_PTYPE_DFP_HUB] = "hub",
102 [IDH_PTYPE_DFP_HOST] = "host",
103 [IDH_PTYPE_DFP_PB] = "power_brick",
106 static const char * const product_type_cable[8] = {
107 [IDH_PTYPE_NOT_CABLE] = "not_cable",
108 [IDH_PTYPE_PCABLE] = "passive",
109 [IDH_PTYPE_ACABLE] = "active",
110 [IDH_PTYPE_VPD] = "vpd",
113 static struct usb_pd_identity *get_pd_identity(struct device *dev)
115 if (is_typec_partner(dev)) {
116 struct typec_partner *partner = to_typec_partner(dev);
118 return partner->identity;
119 } else if (is_typec_cable(dev)) {
120 struct typec_cable *cable = to_typec_cable(dev);
122 return cable->identity;
127 static const char *get_pd_product_type(struct device *dev)
129 struct typec_port *port = to_typec_port(dev->parent);
130 struct usb_pd_identity *id = get_pd_identity(dev);
131 const char *ptype = NULL;
133 if (is_typec_partner(dev)) {
137 if (port->data_role == TYPEC_HOST)
138 ptype = product_type_ufp[PD_IDH_PTYPE(id->id_header)];
140 ptype = product_type_dfp[PD_IDH_DFP_PTYPE(id->id_header)];
141 } else if (is_typec_cable(dev)) {
143 ptype = product_type_cable[PD_IDH_PTYPE(id->id_header)];
145 ptype = to_typec_cable(dev)->active ?
146 product_type_cable[IDH_PTYPE_ACABLE] :
147 product_type_cable[IDH_PTYPE_PCABLE];
153 static ssize_t id_header_show(struct device *dev, struct device_attribute *attr,
156 struct usb_pd_identity *id = get_pd_identity(dev);
158 return sprintf(buf, "0x%08x\n", id->id_header);
160 static DEVICE_ATTR_RO(id_header);
162 static ssize_t cert_stat_show(struct device *dev, struct device_attribute *attr,
165 struct usb_pd_identity *id = get_pd_identity(dev);
167 return sprintf(buf, "0x%08x\n", id->cert_stat);
169 static DEVICE_ATTR_RO(cert_stat);
171 static ssize_t product_show(struct device *dev, struct device_attribute *attr,
174 struct usb_pd_identity *id = get_pd_identity(dev);
176 return sprintf(buf, "0x%08x\n", id->product);
178 static DEVICE_ATTR_RO(product);
180 static ssize_t product_type_vdo1_show(struct device *dev, struct device_attribute *attr,
183 struct usb_pd_identity *id = get_pd_identity(dev);
185 return sysfs_emit(buf, "0x%08x\n", id->vdo[0]);
187 static DEVICE_ATTR_RO(product_type_vdo1);
189 static ssize_t product_type_vdo2_show(struct device *dev, struct device_attribute *attr,
192 struct usb_pd_identity *id = get_pd_identity(dev);
194 return sysfs_emit(buf, "0x%08x\n", id->vdo[1]);
196 static DEVICE_ATTR_RO(product_type_vdo2);
198 static ssize_t product_type_vdo3_show(struct device *dev, struct device_attribute *attr,
201 struct usb_pd_identity *id = get_pd_identity(dev);
203 return sysfs_emit(buf, "0x%08x\n", id->vdo[2]);
205 static DEVICE_ATTR_RO(product_type_vdo3);
207 static struct attribute *usb_pd_id_attrs[] = {
208 &dev_attr_id_header.attr,
209 &dev_attr_cert_stat.attr,
210 &dev_attr_product.attr,
211 &dev_attr_product_type_vdo1.attr,
212 &dev_attr_product_type_vdo2.attr,
213 &dev_attr_product_type_vdo3.attr,
217 static const struct attribute_group usb_pd_id_group = {
219 .attrs = usb_pd_id_attrs,
222 static const struct attribute_group *usb_pd_id_groups[] = {
227 static void typec_product_type_notify(struct device *dev)
232 ptype = get_pd_product_type(dev);
236 sysfs_notify(&dev->kobj, NULL, "type");
238 envp[0] = kasprintf(GFP_KERNEL, "PRODUCT_TYPE=%s", ptype);
242 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
246 static void typec_report_identity(struct device *dev)
248 sysfs_notify(&dev->kobj, "identity", "id_header");
249 sysfs_notify(&dev->kobj, "identity", "cert_stat");
250 sysfs_notify(&dev->kobj, "identity", "product");
251 sysfs_notify(&dev->kobj, "identity", "product_type_vdo1");
252 sysfs_notify(&dev->kobj, "identity", "product_type_vdo2");
253 sysfs_notify(&dev->kobj, "identity", "product_type_vdo3");
254 typec_product_type_notify(dev);
258 type_show(struct device *dev, struct device_attribute *attr, char *buf)
262 ptype = get_pd_product_type(dev);
266 return sysfs_emit(buf, "%s\n", ptype);
268 static DEVICE_ATTR_RO(type);
270 static ssize_t usb_power_delivery_revision_show(struct device *dev,
271 struct device_attribute *attr,
273 static DEVICE_ATTR_RO(usb_power_delivery_revision);
275 /* ------------------------------------------------------------------------- */
276 /* Alternate Modes */
278 static int altmode_match(struct device *dev, void *data)
280 struct typec_altmode *adev = to_typec_altmode(dev);
281 struct typec_device_id *id = data;
283 if (!is_typec_altmode(dev))
286 return ((adev->svid == id->svid) && (adev->mode == id->mode));
289 static void typec_altmode_set_partner(struct altmode *altmode)
291 struct typec_altmode *adev = &altmode->adev;
292 struct typec_device_id id = { adev->svid, adev->mode, };
293 struct typec_port *port = typec_altmode2port(adev);
294 struct altmode *partner;
297 dev = device_find_child(&port->dev, &id, altmode_match);
301 /* Bind the port alt mode to the partner/plug alt mode. */
302 partner = to_altmode(to_typec_altmode(dev));
303 altmode->partner = partner;
305 /* Bind the partner/plug alt mode to the port alt mode. */
306 if (is_typec_plug(adev->dev.parent)) {
307 struct typec_plug *plug = to_typec_plug(adev->dev.parent);
309 partner->plug[plug->index] = altmode;
311 partner->partner = altmode;
315 static void typec_altmode_put_partner(struct altmode *altmode)
317 struct altmode *partner = altmode->partner;
318 struct typec_altmode *adev;
323 adev = &partner->adev;
325 if (is_typec_plug(adev->dev.parent)) {
326 struct typec_plug *plug = to_typec_plug(adev->dev.parent);
328 partner->plug[plug->index] = NULL;
330 partner->partner = NULL;
332 put_device(&adev->dev);
336 * typec_altmode_update_active - Report Enter/Exit mode
337 * @adev: Handle to the alternate mode
338 * @active: True when the mode has been entered
340 * If a partner or cable plug executes Enter/Exit Mode command successfully, the
341 * drivers use this routine to report the updated state of the mode.
343 void typec_altmode_update_active(struct typec_altmode *adev, bool active)
347 if (adev->active == active)
350 if (!is_typec_port(adev->dev.parent) && adev->dev.driver) {
352 module_put(adev->dev.driver->owner);
354 WARN_ON(!try_module_get(adev->dev.driver->owner));
357 adev->active = active;
358 snprintf(dir, sizeof(dir), "mode%d", adev->mode);
359 sysfs_notify(&adev->dev.kobj, dir, "active");
360 sysfs_notify(&adev->dev.kobj, NULL, "active");
361 kobject_uevent(&adev->dev.kobj, KOBJ_CHANGE);
363 EXPORT_SYMBOL_GPL(typec_altmode_update_active);
366 * typec_altmode2port - Alternate Mode to USB Type-C port
367 * @alt: The Alternate Mode
369 * Returns handle to the port that a cable plug or partner with @alt is
372 struct typec_port *typec_altmode2port(struct typec_altmode *alt)
374 if (is_typec_plug(alt->dev.parent))
375 return to_typec_port(alt->dev.parent->parent->parent);
376 if (is_typec_partner(alt->dev.parent))
377 return to_typec_port(alt->dev.parent->parent);
378 if (is_typec_port(alt->dev.parent))
379 return to_typec_port(alt->dev.parent);
383 EXPORT_SYMBOL_GPL(typec_altmode2port);
386 vdo_show(struct device *dev, struct device_attribute *attr, char *buf)
388 struct typec_altmode *alt = to_typec_altmode(dev);
390 return sprintf(buf, "0x%08x\n", alt->vdo);
392 static DEVICE_ATTR_RO(vdo);
395 description_show(struct device *dev, struct device_attribute *attr, char *buf)
397 struct typec_altmode *alt = to_typec_altmode(dev);
399 return sprintf(buf, "%s\n", alt->desc ? alt->desc : "");
401 static DEVICE_ATTR_RO(description);
404 active_show(struct device *dev, struct device_attribute *attr, char *buf)
406 struct typec_altmode *alt = to_typec_altmode(dev);
408 return sprintf(buf, "%s\n", alt->active ? "yes" : "no");
411 static ssize_t active_store(struct device *dev, struct device_attribute *attr,
412 const char *buf, size_t size)
414 struct typec_altmode *adev = to_typec_altmode(dev);
415 struct altmode *altmode = to_altmode(adev);
419 ret = kstrtobool(buf, &enter);
423 if (adev->active == enter)
426 if (is_typec_port(adev->dev.parent)) {
427 typec_altmode_update_active(adev, enter);
429 /* Make sure that the partner exits the mode before disabling */
430 if (altmode->partner && !enter && altmode->partner->adev.active)
431 typec_altmode_exit(&altmode->partner->adev);
432 } else if (altmode->partner) {
433 if (enter && !altmode->partner->adev.active) {
434 dev_warn(dev, "port has the mode disabled\n");
439 /* Note: If there is no driver, the mode will not be entered */
440 if (adev->ops && adev->ops->activate) {
441 ret = adev->ops->activate(adev, enter);
448 static DEVICE_ATTR_RW(active);
451 supported_roles_show(struct device *dev, struct device_attribute *attr,
454 struct altmode *alt = to_altmode(to_typec_altmode(dev));
457 switch (alt->roles) {
459 ret = sprintf(buf, "source\n");
462 ret = sprintf(buf, "sink\n");
466 ret = sprintf(buf, "source sink\n");
471 static DEVICE_ATTR_RO(supported_roles);
474 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
476 struct typec_altmode *adev = to_typec_altmode(dev);
478 return sprintf(buf, "%u\n", adev->mode);
480 static DEVICE_ATTR_RO(mode);
483 svid_show(struct device *dev, struct device_attribute *attr, char *buf)
485 struct typec_altmode *adev = to_typec_altmode(dev);
487 return sprintf(buf, "%04x\n", adev->svid);
489 static DEVICE_ATTR_RO(svid);
491 static struct attribute *typec_altmode_attrs[] = {
492 &dev_attr_active.attr,
499 static umode_t typec_altmode_attr_is_visible(struct kobject *kobj,
500 struct attribute *attr, int n)
502 struct typec_altmode *adev = to_typec_altmode(kobj_to_dev(kobj));
504 if (attr == &dev_attr_active.attr)
505 if (!adev->ops || !adev->ops->activate)
511 static const struct attribute_group typec_altmode_group = {
512 .is_visible = typec_altmode_attr_is_visible,
513 .attrs = typec_altmode_attrs,
516 static const struct attribute_group *typec_altmode_groups[] = {
517 &typec_altmode_group,
521 static int altmode_id_get(struct device *dev)
525 if (is_typec_partner(dev))
526 ids = &to_typec_partner(dev)->mode_ids;
527 else if (is_typec_plug(dev))
528 ids = &to_typec_plug(dev)->mode_ids;
530 ids = &to_typec_port(dev)->mode_ids;
532 return ida_simple_get(ids, 0, 0, GFP_KERNEL);
535 static void altmode_id_remove(struct device *dev, int id)
539 if (is_typec_partner(dev))
540 ids = &to_typec_partner(dev)->mode_ids;
541 else if (is_typec_plug(dev))
542 ids = &to_typec_plug(dev)->mode_ids;
544 ids = &to_typec_port(dev)->mode_ids;
546 ida_simple_remove(ids, id);
549 static void typec_altmode_release(struct device *dev)
551 struct altmode *alt = to_altmode(to_typec_altmode(dev));
553 typec_altmode_put_partner(alt);
555 altmode_id_remove(alt->adev.dev.parent, alt->id);
559 const struct device_type typec_altmode_dev_type = {
560 .name = "typec_alternate_mode",
561 .groups = typec_altmode_groups,
562 .release = typec_altmode_release,
565 static struct typec_altmode *
566 typec_register_altmode(struct device *parent,
567 const struct typec_altmode_desc *desc)
569 unsigned int id = altmode_id_get(parent);
570 bool is_port = is_typec_port(parent);
574 alt = kzalloc(sizeof(*alt), GFP_KERNEL);
576 return ERR_PTR(-ENOMEM);
578 alt->adev.svid = desc->svid;
579 alt->adev.mode = desc->mode;
580 alt->adev.vdo = desc->vdo;
581 alt->roles = desc->roles;
584 alt->attrs[0] = &dev_attr_vdo.attr;
585 alt->attrs[1] = &dev_attr_description.attr;
586 alt->attrs[2] = &dev_attr_active.attr;
589 alt->attrs[3] = &dev_attr_supported_roles.attr;
590 alt->adev.active = true; /* Enabled by default */
593 sprintf(alt->group_name, "mode%d", desc->mode);
594 alt->group.name = alt->group_name;
595 alt->group.attrs = alt->attrs;
596 alt->groups[0] = &alt->group;
598 alt->adev.dev.parent = parent;
599 alt->adev.dev.groups = alt->groups;
600 alt->adev.dev.type = &typec_altmode_dev_type;
601 dev_set_name(&alt->adev.dev, "%s.%u", dev_name(parent), id);
603 /* Link partners and plugs with the ports */
605 typec_altmode_set_partner(alt);
607 /* The partners are bind to drivers */
608 if (is_typec_partner(parent))
609 alt->adev.dev.bus = &typec_bus;
611 /* Plug alt modes need a class to generate udev events. */
612 if (is_typec_plug(parent))
613 alt->adev.dev.class = typec_class;
615 ret = device_register(&alt->adev.dev);
617 dev_err(parent, "failed to register alternate mode (%d)\n",
619 put_device(&alt->adev.dev);
627 * typec_unregister_altmode - Unregister Alternate Mode
628 * @adev: The alternate mode to be unregistered
630 * Unregister device created with typec_partner_register_altmode(),
631 * typec_plug_register_altmode() or typec_port_register_altmode().
633 void typec_unregister_altmode(struct typec_altmode *adev)
635 if (IS_ERR_OR_NULL(adev))
637 typec_mux_put(to_altmode(adev)->mux);
638 device_unregister(&adev->dev);
640 EXPORT_SYMBOL_GPL(typec_unregister_altmode);
642 /* ------------------------------------------------------------------------- */
643 /* Type-C Partners */
645 static ssize_t accessory_mode_show(struct device *dev,
646 struct device_attribute *attr,
649 struct typec_partner *p = to_typec_partner(dev);
651 return sprintf(buf, "%s\n", typec_accessory_modes[p->accessory]);
653 static DEVICE_ATTR_RO(accessory_mode);
655 static ssize_t supports_usb_power_delivery_show(struct device *dev,
656 struct device_attribute *attr,
659 struct typec_partner *p = to_typec_partner(dev);
661 return sprintf(buf, "%s\n", p->usb_pd ? "yes" : "no");
663 static DEVICE_ATTR_RO(supports_usb_power_delivery);
665 static ssize_t number_of_alternate_modes_show(struct device *dev, struct device_attribute *attr,
668 struct typec_partner *partner;
669 struct typec_plug *plug;
672 if (is_typec_partner(dev)) {
673 partner = to_typec_partner(dev);
674 num_altmodes = partner->num_altmodes;
675 } else if (is_typec_plug(dev)) {
676 plug = to_typec_plug(dev);
677 num_altmodes = plug->num_altmodes;
682 return sysfs_emit(buf, "%d\n", num_altmodes);
684 static DEVICE_ATTR_RO(number_of_alternate_modes);
686 static struct attribute *typec_partner_attrs[] = {
687 &dev_attr_accessory_mode.attr,
688 &dev_attr_supports_usb_power_delivery.attr,
689 &dev_attr_number_of_alternate_modes.attr,
691 &dev_attr_usb_power_delivery_revision.attr,
695 static umode_t typec_partner_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
697 struct typec_partner *partner = to_typec_partner(kobj_to_dev(kobj));
699 if (attr == &dev_attr_number_of_alternate_modes.attr) {
700 if (partner->num_altmodes < 0)
704 if (attr == &dev_attr_type.attr)
705 if (!get_pd_product_type(kobj_to_dev(kobj)))
711 static const struct attribute_group typec_partner_group = {
712 .is_visible = typec_partner_attr_is_visible,
713 .attrs = typec_partner_attrs
716 static const struct attribute_group *typec_partner_groups[] = {
717 &typec_partner_group,
721 static void typec_partner_release(struct device *dev)
723 struct typec_partner *partner = to_typec_partner(dev);
725 ida_destroy(&partner->mode_ids);
729 static const struct device_type typec_partner_dev_type = {
730 .name = "typec_partner",
731 .groups = typec_partner_groups,
732 .release = typec_partner_release,
736 * typec_partner_set_identity - Report result from Discover Identity command
737 * @partner: The partner updated identity values
739 * This routine is used to report that the result of Discover Identity USB power
740 * delivery command has become available.
742 int typec_partner_set_identity(struct typec_partner *partner)
744 if (!partner->identity)
747 typec_report_identity(&partner->dev);
750 EXPORT_SYMBOL_GPL(typec_partner_set_identity);
753 * typec_partner_set_pd_revision - Set the PD revision supported by the partner
754 * @partner: The partner to be updated.
755 * @pd_revision: USB Power Delivery Specification Revision supported by partner
757 * This routine is used to report that the PD revision of the port partner has
760 void typec_partner_set_pd_revision(struct typec_partner *partner, u16 pd_revision)
762 if (partner->pd_revision == pd_revision)
765 partner->pd_revision = pd_revision;
766 sysfs_notify(&partner->dev.kobj, NULL, "usb_power_delivery_revision");
767 if (pd_revision != 0 && !partner->usb_pd) {
769 sysfs_notify(&partner->dev.kobj, NULL,
770 "supports_usb_power_delivery");
772 kobject_uevent(&partner->dev.kobj, KOBJ_CHANGE);
774 EXPORT_SYMBOL_GPL(typec_partner_set_pd_revision);
777 * typec_partner_set_num_altmodes - Set the number of available partner altmodes
778 * @partner: The partner to be updated.
779 * @num_altmodes: The number of altmodes we want to specify as available.
781 * This routine is used to report the number of alternate modes supported by the
782 * partner. This value is *not* enforced in alternate mode registration routines.
784 * @partner.num_altmodes is set to -1 on partner registration, denoting that
785 * a valid value has not been set for it yet.
787 * Returns 0 on success or negative error number on failure.
789 int typec_partner_set_num_altmodes(struct typec_partner *partner, int num_altmodes)
793 if (num_altmodes < 0)
796 partner->num_altmodes = num_altmodes;
797 ret = sysfs_update_group(&partner->dev.kobj, &typec_partner_group);
801 sysfs_notify(&partner->dev.kobj, NULL, "number_of_alternate_modes");
802 kobject_uevent(&partner->dev.kobj, KOBJ_CHANGE);
806 EXPORT_SYMBOL_GPL(typec_partner_set_num_altmodes);
809 * typec_partner_register_altmode - Register USB Type-C Partner Alternate Mode
810 * @partner: USB Type-C Partner that supports the alternate mode
811 * @desc: Description of the alternate mode
813 * This routine is used to register each alternate mode individually that
814 * @partner has listed in response to Discover SVIDs command. The modes for a
815 * SVID listed in response to Discover Modes command need to be listed in an
818 * Returns handle to the alternate mode on success or ERR_PTR on failure.
820 struct typec_altmode *
821 typec_partner_register_altmode(struct typec_partner *partner,
822 const struct typec_altmode_desc *desc)
824 return typec_register_altmode(&partner->dev, desc);
826 EXPORT_SYMBOL_GPL(typec_partner_register_altmode);
829 * typec_partner_set_svdm_version - Set negotiated Structured VDM (SVDM) Version
830 * @partner: USB Type-C Partner that supports SVDM
831 * @svdm_version: Negotiated SVDM Version
833 * This routine is used to save the negotiated SVDM Version.
835 void typec_partner_set_svdm_version(struct typec_partner *partner,
836 enum usb_pd_svdm_ver svdm_version)
838 partner->svdm_version = svdm_version;
840 EXPORT_SYMBOL_GPL(typec_partner_set_svdm_version);
843 * typec_register_partner - Register a USB Type-C Partner
844 * @port: The USB Type-C Port the partner is connected to
845 * @desc: Description of the partner
847 * Registers a device for USB Type-C Partner described in @desc.
849 * Returns handle to the partner on success or ERR_PTR on failure.
851 struct typec_partner *typec_register_partner(struct typec_port *port,
852 struct typec_partner_desc *desc)
854 struct typec_partner *partner;
857 partner = kzalloc(sizeof(*partner), GFP_KERNEL);
859 return ERR_PTR(-ENOMEM);
861 ida_init(&partner->mode_ids);
862 partner->usb_pd = desc->usb_pd;
863 partner->accessory = desc->accessory;
864 partner->num_altmodes = -1;
865 partner->pd_revision = desc->pd_revision;
866 partner->svdm_version = port->cap->svdm_version;
868 if (desc->identity) {
870 * Creating directory for the identity only if the driver is
871 * able to provide data to it.
873 partner->dev.groups = usb_pd_id_groups;
874 partner->identity = desc->identity;
877 partner->dev.class = typec_class;
878 partner->dev.parent = &port->dev;
879 partner->dev.type = &typec_partner_dev_type;
880 dev_set_name(&partner->dev, "%s-partner", dev_name(&port->dev));
882 ret = device_register(&partner->dev);
884 dev_err(&port->dev, "failed to register partner (%d)\n", ret);
885 put_device(&partner->dev);
891 EXPORT_SYMBOL_GPL(typec_register_partner);
894 * typec_unregister_partner - Unregister a USB Type-C Partner
895 * @partner: The partner to be unregistered
897 * Unregister device created with typec_register_partner().
899 void typec_unregister_partner(struct typec_partner *partner)
901 if (!IS_ERR_OR_NULL(partner))
902 device_unregister(&partner->dev);
904 EXPORT_SYMBOL_GPL(typec_unregister_partner);
906 /* ------------------------------------------------------------------------- */
907 /* Type-C Cable Plugs */
909 static void typec_plug_release(struct device *dev)
911 struct typec_plug *plug = to_typec_plug(dev);
913 ida_destroy(&plug->mode_ids);
917 static struct attribute *typec_plug_attrs[] = {
918 &dev_attr_number_of_alternate_modes.attr,
922 static umode_t typec_plug_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
924 struct typec_plug *plug = to_typec_plug(kobj_to_dev(kobj));
926 if (attr == &dev_attr_number_of_alternate_modes.attr) {
927 if (plug->num_altmodes < 0)
934 static const struct attribute_group typec_plug_group = {
935 .is_visible = typec_plug_attr_is_visible,
936 .attrs = typec_plug_attrs
939 static const struct attribute_group *typec_plug_groups[] = {
944 static const struct device_type typec_plug_dev_type = {
945 .name = "typec_plug",
946 .groups = typec_plug_groups,
947 .release = typec_plug_release,
951 * typec_plug_set_num_altmodes - Set the number of available plug altmodes
952 * @plug: The plug to be updated.
953 * @num_altmodes: The number of altmodes we want to specify as available.
955 * This routine is used to report the number of alternate modes supported by the
956 * plug. This value is *not* enforced in alternate mode registration routines.
958 * @plug.num_altmodes is set to -1 on plug registration, denoting that
959 * a valid value has not been set for it yet.
961 * Returns 0 on success or negative error number on failure.
963 int typec_plug_set_num_altmodes(struct typec_plug *plug, int num_altmodes)
967 if (num_altmodes < 0)
970 plug->num_altmodes = num_altmodes;
971 ret = sysfs_update_group(&plug->dev.kobj, &typec_plug_group);
975 sysfs_notify(&plug->dev.kobj, NULL, "number_of_alternate_modes");
976 kobject_uevent(&plug->dev.kobj, KOBJ_CHANGE);
980 EXPORT_SYMBOL_GPL(typec_plug_set_num_altmodes);
983 * typec_plug_register_altmode - Register USB Type-C Cable Plug Alternate Mode
984 * @plug: USB Type-C Cable Plug that supports the alternate mode
985 * @desc: Description of the alternate mode
987 * This routine is used to register each alternate mode individually that @plug
988 * has listed in response to Discover SVIDs command. The modes for a SVID that
989 * the plug lists in response to Discover Modes command need to be listed in an
992 * Returns handle to the alternate mode on success or ERR_PTR on failure.
994 struct typec_altmode *
995 typec_plug_register_altmode(struct typec_plug *plug,
996 const struct typec_altmode_desc *desc)
998 return typec_register_altmode(&plug->dev, desc);
1000 EXPORT_SYMBOL_GPL(typec_plug_register_altmode);
1003 * typec_register_plug - Register a USB Type-C Cable Plug
1004 * @cable: USB Type-C Cable with the plug
1005 * @desc: Description of the cable plug
1007 * Registers a device for USB Type-C Cable Plug described in @desc. A USB Type-C
1008 * Cable Plug represents a plug with electronics in it that can response to USB
1009 * Power Delivery SOP Prime or SOP Double Prime packages.
1011 * Returns handle to the cable plug on success or ERR_PTR on failure.
1013 struct typec_plug *typec_register_plug(struct typec_cable *cable,
1014 struct typec_plug_desc *desc)
1016 struct typec_plug *plug;
1020 plug = kzalloc(sizeof(*plug), GFP_KERNEL);
1022 return ERR_PTR(-ENOMEM);
1024 sprintf(name, "plug%d", desc->index);
1026 ida_init(&plug->mode_ids);
1027 plug->num_altmodes = -1;
1028 plug->index = desc->index;
1029 plug->dev.class = typec_class;
1030 plug->dev.parent = &cable->dev;
1031 plug->dev.type = &typec_plug_dev_type;
1032 dev_set_name(&plug->dev, "%s-%s", dev_name(cable->dev.parent), name);
1034 ret = device_register(&plug->dev);
1036 dev_err(&cable->dev, "failed to register plug (%d)\n", ret);
1037 put_device(&plug->dev);
1038 return ERR_PTR(ret);
1043 EXPORT_SYMBOL_GPL(typec_register_plug);
1046 * typec_unregister_plug - Unregister a USB Type-C Cable Plug
1047 * @plug: The cable plug to be unregistered
1049 * Unregister device created with typec_register_plug().
1051 void typec_unregister_plug(struct typec_plug *plug)
1053 if (!IS_ERR_OR_NULL(plug))
1054 device_unregister(&plug->dev);
1056 EXPORT_SYMBOL_GPL(typec_unregister_plug);
1060 static const char * const typec_plug_types[] = {
1061 [USB_PLUG_NONE] = "unknown",
1062 [USB_PLUG_TYPE_A] = "type-a",
1063 [USB_PLUG_TYPE_B] = "type-b",
1064 [USB_PLUG_TYPE_C] = "type-c",
1065 [USB_PLUG_CAPTIVE] = "captive",
1068 static ssize_t plug_type_show(struct device *dev,
1069 struct device_attribute *attr, char *buf)
1071 struct typec_cable *cable = to_typec_cable(dev);
1073 return sprintf(buf, "%s\n", typec_plug_types[cable->type]);
1075 static DEVICE_ATTR_RO(plug_type);
1077 static struct attribute *typec_cable_attrs[] = {
1078 &dev_attr_type.attr,
1079 &dev_attr_plug_type.attr,
1080 &dev_attr_usb_power_delivery_revision.attr,
1083 ATTRIBUTE_GROUPS(typec_cable);
1085 static void typec_cable_release(struct device *dev)
1087 struct typec_cable *cable = to_typec_cable(dev);
1092 static const struct device_type typec_cable_dev_type = {
1093 .name = "typec_cable",
1094 .groups = typec_cable_groups,
1095 .release = typec_cable_release,
1098 static int cable_match(struct device *dev, void *data)
1100 return is_typec_cable(dev);
1104 * typec_cable_get - Get a reference to the USB Type-C cable
1105 * @port: The USB Type-C Port the cable is connected to
1107 * The caller must decrement the reference count with typec_cable_put() after
1110 struct typec_cable *typec_cable_get(struct typec_port *port)
1114 dev = device_find_child(&port->dev, NULL, cable_match);
1118 return to_typec_cable(dev);
1120 EXPORT_SYMBOL_GPL(typec_cable_get);
1123 * typec_cable_put - Decrement the reference count on USB Type-C cable
1124 * @cable: The USB Type-C cable
1126 void typec_cable_put(struct typec_cable *cable)
1128 put_device(&cable->dev);
1130 EXPORT_SYMBOL_GPL(typec_cable_put);
1133 * typec_cable_is_active - Check is the USB Type-C cable active or passive
1134 * @cable: The USB Type-C Cable
1136 * Return 1 if the cable is active or 0 if it's passive.
1138 int typec_cable_is_active(struct typec_cable *cable)
1140 return cable->active;
1142 EXPORT_SYMBOL_GPL(typec_cable_is_active);
1145 * typec_cable_set_identity - Report result from Discover Identity command
1146 * @cable: The cable updated identity values
1148 * This routine is used to report that the result of Discover Identity USB power
1149 * delivery command has become available.
1151 int typec_cable_set_identity(struct typec_cable *cable)
1153 if (!cable->identity)
1156 typec_report_identity(&cable->dev);
1159 EXPORT_SYMBOL_GPL(typec_cable_set_identity);
1162 * typec_register_cable - Register a USB Type-C Cable
1163 * @port: The USB Type-C Port the cable is connected to
1164 * @desc: Description of the cable
1166 * Registers a device for USB Type-C Cable described in @desc. The cable will be
1167 * parent for the optional cable plug devises.
1169 * Returns handle to the cable on success or ERR_PTR on failure.
1171 struct typec_cable *typec_register_cable(struct typec_port *port,
1172 struct typec_cable_desc *desc)
1174 struct typec_cable *cable;
1177 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
1179 return ERR_PTR(-ENOMEM);
1181 cable->type = desc->type;
1182 cable->active = desc->active;
1183 cable->pd_revision = desc->pd_revision;
1185 if (desc->identity) {
1187 * Creating directory for the identity only if the driver is
1188 * able to provide data to it.
1190 cable->dev.groups = usb_pd_id_groups;
1191 cable->identity = desc->identity;
1194 cable->dev.class = typec_class;
1195 cable->dev.parent = &port->dev;
1196 cable->dev.type = &typec_cable_dev_type;
1197 dev_set_name(&cable->dev, "%s-cable", dev_name(&port->dev));
1199 ret = device_register(&cable->dev);
1201 dev_err(&port->dev, "failed to register cable (%d)\n", ret);
1202 put_device(&cable->dev);
1203 return ERR_PTR(ret);
1208 EXPORT_SYMBOL_GPL(typec_register_cable);
1211 * typec_unregister_cable - Unregister a USB Type-C Cable
1212 * @cable: The cable to be unregistered
1214 * Unregister device created with typec_register_cable().
1216 void typec_unregister_cable(struct typec_cable *cable)
1218 if (!IS_ERR_OR_NULL(cable))
1219 device_unregister(&cable->dev);
1221 EXPORT_SYMBOL_GPL(typec_unregister_cable);
1223 /* ------------------------------------------------------------------------- */
1224 /* USB Type-C ports */
1226 static const char * const typec_orientations[] = {
1227 [TYPEC_ORIENTATION_NONE] = "unknown",
1228 [TYPEC_ORIENTATION_NORMAL] = "normal",
1229 [TYPEC_ORIENTATION_REVERSE] = "reverse",
1232 static const char * const typec_roles[] = {
1233 [TYPEC_SINK] = "sink",
1234 [TYPEC_SOURCE] = "source",
1237 static const char * const typec_data_roles[] = {
1238 [TYPEC_DEVICE] = "device",
1239 [TYPEC_HOST] = "host",
1242 static const char * const typec_port_power_roles[] = {
1243 [TYPEC_PORT_SRC] = "source",
1244 [TYPEC_PORT_SNK] = "sink",
1245 [TYPEC_PORT_DRP] = "dual",
1248 static const char * const typec_port_data_roles[] = {
1249 [TYPEC_PORT_DFP] = "host",
1250 [TYPEC_PORT_UFP] = "device",
1251 [TYPEC_PORT_DRD] = "dual",
1254 static const char * const typec_port_types_drp[] = {
1255 [TYPEC_PORT_SRC] = "dual [source] sink",
1256 [TYPEC_PORT_SNK] = "dual source [sink]",
1257 [TYPEC_PORT_DRP] = "[dual] source sink",
1261 preferred_role_store(struct device *dev, struct device_attribute *attr,
1262 const char *buf, size_t size)
1264 struct typec_port *port = to_typec_port(dev);
1268 if (port->cap->type != TYPEC_PORT_DRP) {
1269 dev_dbg(dev, "Preferred role only supported with DRP ports\n");
1273 if (!port->ops || !port->ops->try_role) {
1274 dev_dbg(dev, "Setting preferred role not supported\n");
1278 role = sysfs_match_string(typec_roles, buf);
1280 if (sysfs_streq(buf, "none"))
1281 role = TYPEC_NO_PREFERRED_ROLE;
1286 ret = port->ops->try_role(port, role);
1290 port->prefer_role = role;
1295 preferred_role_show(struct device *dev, struct device_attribute *attr,
1298 struct typec_port *port = to_typec_port(dev);
1300 if (port->cap->type != TYPEC_PORT_DRP)
1303 if (port->prefer_role < 0)
1306 return sprintf(buf, "%s\n", typec_roles[port->prefer_role]);
1308 static DEVICE_ATTR_RW(preferred_role);
1310 static ssize_t data_role_store(struct device *dev,
1311 struct device_attribute *attr,
1312 const char *buf, size_t size)
1314 struct typec_port *port = to_typec_port(dev);
1317 if (!port->ops || !port->ops->dr_set) {
1318 dev_dbg(dev, "data role swapping not supported\n");
1322 ret = sysfs_match_string(typec_data_roles, buf);
1326 mutex_lock(&port->port_type_lock);
1327 if (port->cap->data != TYPEC_PORT_DRD) {
1329 goto unlock_and_ret;
1332 ret = port->ops->dr_set(port, ret);
1334 goto unlock_and_ret;
1338 mutex_unlock(&port->port_type_lock);
1342 static ssize_t data_role_show(struct device *dev,
1343 struct device_attribute *attr, char *buf)
1345 struct typec_port *port = to_typec_port(dev);
1347 if (port->cap->data == TYPEC_PORT_DRD)
1348 return sprintf(buf, "%s\n", port->data_role == TYPEC_HOST ?
1349 "[host] device" : "host [device]");
1351 return sprintf(buf, "[%s]\n", typec_data_roles[port->data_role]);
1353 static DEVICE_ATTR_RW(data_role);
1355 static ssize_t power_role_store(struct device *dev,
1356 struct device_attribute *attr,
1357 const char *buf, size_t size)
1359 struct typec_port *port = to_typec_port(dev);
1362 if (!port->ops || !port->ops->pr_set) {
1363 dev_dbg(dev, "power role swapping not supported\n");
1367 if (port->pwr_opmode != TYPEC_PWR_MODE_PD) {
1368 dev_dbg(dev, "partner unable to swap power role\n");
1372 ret = sysfs_match_string(typec_roles, buf);
1376 mutex_lock(&port->port_type_lock);
1377 if (port->port_type != TYPEC_PORT_DRP) {
1378 dev_dbg(dev, "port type fixed at \"%s\"",
1379 typec_port_power_roles[port->port_type]);
1381 goto unlock_and_ret;
1384 ret = port->ops->pr_set(port, ret);
1386 goto unlock_and_ret;
1390 mutex_unlock(&port->port_type_lock);
1394 static ssize_t power_role_show(struct device *dev,
1395 struct device_attribute *attr, char *buf)
1397 struct typec_port *port = to_typec_port(dev);
1399 if (port->cap->type == TYPEC_PORT_DRP)
1400 return sprintf(buf, "%s\n", port->pwr_role == TYPEC_SOURCE ?
1401 "[source] sink" : "source [sink]");
1403 return sprintf(buf, "[%s]\n", typec_roles[port->pwr_role]);
1405 static DEVICE_ATTR_RW(power_role);
1408 port_type_store(struct device *dev, struct device_attribute *attr,
1409 const char *buf, size_t size)
1411 struct typec_port *port = to_typec_port(dev);
1413 enum typec_port_type type;
1415 if (port->cap->type != TYPEC_PORT_DRP ||
1416 !port->ops || !port->ops->port_type_set) {
1417 dev_dbg(dev, "changing port type not supported\n");
1421 ret = sysfs_match_string(typec_port_power_roles, buf);
1426 mutex_lock(&port->port_type_lock);
1428 if (port->port_type == type) {
1430 goto unlock_and_ret;
1433 ret = port->ops->port_type_set(port, type);
1435 goto unlock_and_ret;
1437 port->port_type = type;
1441 mutex_unlock(&port->port_type_lock);
1446 port_type_show(struct device *dev, struct device_attribute *attr,
1449 struct typec_port *port = to_typec_port(dev);
1451 if (port->cap->type == TYPEC_PORT_DRP)
1452 return sprintf(buf, "%s\n",
1453 typec_port_types_drp[port->port_type]);
1455 return sprintf(buf, "[%s]\n", typec_port_power_roles[port->cap->type]);
1457 static DEVICE_ATTR_RW(port_type);
1459 static const char * const typec_pwr_opmodes[] = {
1460 [TYPEC_PWR_MODE_USB] = "default",
1461 [TYPEC_PWR_MODE_1_5A] = "1.5A",
1462 [TYPEC_PWR_MODE_3_0A] = "3.0A",
1463 [TYPEC_PWR_MODE_PD] = "usb_power_delivery",
1466 static ssize_t power_operation_mode_show(struct device *dev,
1467 struct device_attribute *attr,
1470 struct typec_port *port = to_typec_port(dev);
1472 return sprintf(buf, "%s\n", typec_pwr_opmodes[port->pwr_opmode]);
1474 static DEVICE_ATTR_RO(power_operation_mode);
1476 static ssize_t vconn_source_store(struct device *dev,
1477 struct device_attribute *attr,
1478 const char *buf, size_t size)
1480 struct typec_port *port = to_typec_port(dev);
1484 if (!port->cap->pd_revision) {
1485 dev_dbg(dev, "VCONN swap depends on USB Power Delivery\n");
1489 if (!port->ops || !port->ops->vconn_set) {
1490 dev_dbg(dev, "VCONN swapping not supported\n");
1494 ret = kstrtobool(buf, &source);
1498 ret = port->ops->vconn_set(port, (enum typec_role)source);
1505 static ssize_t vconn_source_show(struct device *dev,
1506 struct device_attribute *attr, char *buf)
1508 struct typec_port *port = to_typec_port(dev);
1510 return sprintf(buf, "%s\n",
1511 port->vconn_role == TYPEC_SOURCE ? "yes" : "no");
1513 static DEVICE_ATTR_RW(vconn_source);
1515 static ssize_t supported_accessory_modes_show(struct device *dev,
1516 struct device_attribute *attr,
1519 struct typec_port *port = to_typec_port(dev);
1523 for (i = 0; i < ARRAY_SIZE(port->cap->accessory); i++) {
1524 if (port->cap->accessory[i])
1525 ret += sprintf(buf + ret, "%s ",
1526 typec_accessory_modes[port->cap->accessory[i]]);
1530 return sprintf(buf, "none\n");
1532 buf[ret - 1] = '\n';
1536 static DEVICE_ATTR_RO(supported_accessory_modes);
1538 static ssize_t usb_typec_revision_show(struct device *dev,
1539 struct device_attribute *attr,
1542 struct typec_port *port = to_typec_port(dev);
1543 u16 rev = port->cap->revision;
1545 return sprintf(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
1547 static DEVICE_ATTR_RO(usb_typec_revision);
1549 static ssize_t usb_power_delivery_revision_show(struct device *dev,
1550 struct device_attribute *attr,
1555 if (is_typec_partner(dev)) {
1556 struct typec_partner *partner = to_typec_partner(dev);
1558 rev = partner->pd_revision;
1559 } else if (is_typec_cable(dev)) {
1560 struct typec_cable *cable = to_typec_cable(dev);
1562 rev = cable->pd_revision;
1563 } else if (is_typec_port(dev)) {
1564 struct typec_port *p = to_typec_port(dev);
1566 rev = p->cap->pd_revision;
1568 return sysfs_emit(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
1571 static ssize_t orientation_show(struct device *dev,
1572 struct device_attribute *attr,
1575 struct typec_port *port = to_typec_port(dev);
1577 return sprintf(buf, "%s\n", typec_orientations[port->orientation]);
1579 static DEVICE_ATTR_RO(orientation);
1581 static struct attribute *typec_attrs[] = {
1582 &dev_attr_data_role.attr,
1583 &dev_attr_power_operation_mode.attr,
1584 &dev_attr_power_role.attr,
1585 &dev_attr_preferred_role.attr,
1586 &dev_attr_supported_accessory_modes.attr,
1587 &dev_attr_usb_power_delivery_revision.attr,
1588 &dev_attr_usb_typec_revision.attr,
1589 &dev_attr_vconn_source.attr,
1590 &dev_attr_port_type.attr,
1591 &dev_attr_orientation.attr,
1595 static umode_t typec_attr_is_visible(struct kobject *kobj,
1596 struct attribute *attr, int n)
1598 struct typec_port *port = to_typec_port(kobj_to_dev(kobj));
1600 if (attr == &dev_attr_data_role.attr) {
1601 if (port->cap->data != TYPEC_PORT_DRD ||
1602 !port->ops || !port->ops->dr_set)
1604 } else if (attr == &dev_attr_power_role.attr) {
1605 if (port->cap->type != TYPEC_PORT_DRP ||
1606 !port->ops || !port->ops->pr_set)
1608 } else if (attr == &dev_attr_vconn_source.attr) {
1609 if (!port->cap->pd_revision ||
1610 !port->ops || !port->ops->vconn_set)
1612 } else if (attr == &dev_attr_preferred_role.attr) {
1613 if (port->cap->type != TYPEC_PORT_DRP ||
1614 !port->ops || !port->ops->try_role)
1616 } else if (attr == &dev_attr_port_type.attr) {
1617 if (!port->ops || !port->ops->port_type_set)
1619 if (port->cap->type != TYPEC_PORT_DRP)
1621 } else if (attr == &dev_attr_orientation.attr) {
1622 if (port->cap->orientation_aware)
1630 static const struct attribute_group typec_group = {
1631 .is_visible = typec_attr_is_visible,
1632 .attrs = typec_attrs,
1635 static const struct attribute_group *typec_groups[] = {
1640 static int typec_uevent(struct device *dev, struct kobj_uevent_env *env)
1644 ret = add_uevent_var(env, "TYPEC_PORT=%s", dev_name(dev));
1646 dev_err(dev, "failed to add uevent TYPEC_PORT\n");
1651 static void typec_release(struct device *dev)
1653 struct typec_port *port = to_typec_port(dev);
1655 ida_simple_remove(&typec_index_ida, port->id);
1656 ida_destroy(&port->mode_ids);
1657 typec_switch_put(port->sw);
1658 typec_mux_put(port->mux);
1663 const struct device_type typec_port_dev_type = {
1664 .name = "typec_port",
1665 .groups = typec_groups,
1666 .uevent = typec_uevent,
1667 .release = typec_release,
1670 /* --------------------------------------- */
1671 /* Driver callbacks to report role updates */
1673 static int partner_match(struct device *dev, void *data)
1675 return is_typec_partner(dev);
1679 * typec_set_data_role - Report data role change
1680 * @port: The USB Type-C Port where the role was changed
1681 * @role: The new data role
1683 * This routine is used by the port drivers to report data role changes.
1685 void typec_set_data_role(struct typec_port *port, enum typec_data_role role)
1687 struct device *partner_dev;
1689 if (port->data_role == role)
1692 port->data_role = role;
1693 sysfs_notify(&port->dev.kobj, NULL, "data_role");
1694 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1696 partner_dev = device_find_child(&port->dev, NULL, partner_match);
1700 if (to_typec_partner(partner_dev)->identity)
1701 typec_product_type_notify(partner_dev);
1703 put_device(partner_dev);
1705 EXPORT_SYMBOL_GPL(typec_set_data_role);
1708 * typec_set_pwr_role - Report power role change
1709 * @port: The USB Type-C Port where the role was changed
1710 * @role: The new data role
1712 * This routine is used by the port drivers to report power role changes.
1714 void typec_set_pwr_role(struct typec_port *port, enum typec_role role)
1716 if (port->pwr_role == role)
1719 port->pwr_role = role;
1720 sysfs_notify(&port->dev.kobj, NULL, "power_role");
1721 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1723 EXPORT_SYMBOL_GPL(typec_set_pwr_role);
1726 * typec_set_vconn_role - Report VCONN source change
1727 * @port: The USB Type-C Port which VCONN role changed
1728 * @role: Source when @port is sourcing VCONN, or Sink when it's not
1730 * This routine is used by the port drivers to report if the VCONN source is
1733 void typec_set_vconn_role(struct typec_port *port, enum typec_role role)
1735 if (port->vconn_role == role)
1738 port->vconn_role = role;
1739 sysfs_notify(&port->dev.kobj, NULL, "vconn_source");
1740 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1742 EXPORT_SYMBOL_GPL(typec_set_vconn_role);
1745 * typec_set_pwr_opmode - Report changed power operation mode
1746 * @port: The USB Type-C Port where the mode was changed
1747 * @opmode: New power operation mode
1749 * This routine is used by the port drivers to report changed power operation
1750 * mode in @port. The modes are USB (default), 1.5A, 3.0A as defined in USB
1751 * Type-C specification, and "USB Power Delivery" when the power levels are
1752 * negotiated with methods defined in USB Power Delivery specification.
1754 void typec_set_pwr_opmode(struct typec_port *port,
1755 enum typec_pwr_opmode opmode)
1757 struct device *partner_dev;
1759 if (port->pwr_opmode == opmode)
1762 port->pwr_opmode = opmode;
1763 sysfs_notify(&port->dev.kobj, NULL, "power_operation_mode");
1764 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1766 partner_dev = device_find_child(&port->dev, NULL, partner_match);
1768 struct typec_partner *partner = to_typec_partner(partner_dev);
1770 if (opmode == TYPEC_PWR_MODE_PD && !partner->usb_pd) {
1771 partner->usb_pd = 1;
1772 sysfs_notify(&partner_dev->kobj, NULL,
1773 "supports_usb_power_delivery");
1775 put_device(partner_dev);
1778 EXPORT_SYMBOL_GPL(typec_set_pwr_opmode);
1781 * typec_find_pwr_opmode - Get the typec power operation mode capability
1782 * @name: power operation mode string
1784 * This routine is used to find the typec_pwr_opmode by its string @name.
1786 * Returns typec_pwr_opmode if success, otherwise negative error code.
1788 int typec_find_pwr_opmode(const char *name)
1790 return match_string(typec_pwr_opmodes,
1791 ARRAY_SIZE(typec_pwr_opmodes), name);
1793 EXPORT_SYMBOL_GPL(typec_find_pwr_opmode);
1796 * typec_find_orientation - Convert orientation string to enum typec_orientation
1797 * @name: Orientation string
1799 * This routine is used to find the typec_orientation by its string name @name.
1801 * Returns the orientation value on success, otherwise negative error code.
1803 int typec_find_orientation(const char *name)
1805 return match_string(typec_orientations, ARRAY_SIZE(typec_orientations),
1808 EXPORT_SYMBOL_GPL(typec_find_orientation);
1811 * typec_find_port_power_role - Get the typec port power capability
1812 * @name: port power capability string
1814 * This routine is used to find the typec_port_type by its string name.
1816 * Returns typec_port_type if success, otherwise negative error code.
1818 int typec_find_port_power_role(const char *name)
1820 return match_string(typec_port_power_roles,
1821 ARRAY_SIZE(typec_port_power_roles), name);
1823 EXPORT_SYMBOL_GPL(typec_find_port_power_role);
1826 * typec_find_power_role - Find the typec one specific power role
1827 * @name: power role string
1829 * This routine is used to find the typec_role by its string name.
1831 * Returns typec_role if success, otherwise negative error code.
1833 int typec_find_power_role(const char *name)
1835 return match_string(typec_roles, ARRAY_SIZE(typec_roles), name);
1837 EXPORT_SYMBOL_GPL(typec_find_power_role);
1840 * typec_find_port_data_role - Get the typec port data capability
1841 * @name: port data capability string
1843 * This routine is used to find the typec_port_data by its string name.
1845 * Returns typec_port_data if success, otherwise negative error code.
1847 int typec_find_port_data_role(const char *name)
1849 return match_string(typec_port_data_roles,
1850 ARRAY_SIZE(typec_port_data_roles), name);
1852 EXPORT_SYMBOL_GPL(typec_find_port_data_role);
1854 /* ------------------------------------------ */
1855 /* API for Multiplexer/DeMultiplexer Switches */
1858 * typec_set_orientation - Set USB Type-C cable plug orientation
1859 * @port: USB Type-C Port
1860 * @orientation: USB Type-C cable plug orientation
1862 * Set cable plug orientation for @port.
1864 int typec_set_orientation(struct typec_port *port,
1865 enum typec_orientation orientation)
1869 ret = typec_switch_set(port->sw, orientation);
1873 port->orientation = orientation;
1874 sysfs_notify(&port->dev.kobj, NULL, "orientation");
1875 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1879 EXPORT_SYMBOL_GPL(typec_set_orientation);
1882 * typec_get_orientation - Get USB Type-C cable plug orientation
1883 * @port: USB Type-C Port
1885 * Get current cable plug orientation for @port.
1887 enum typec_orientation typec_get_orientation(struct typec_port *port)
1889 return port->orientation;
1891 EXPORT_SYMBOL_GPL(typec_get_orientation);
1894 * typec_set_mode - Set mode of operation for USB Type-C connector
1895 * @port: USB Type-C connector
1896 * @mode: Accessory Mode, USB Operation or Safe State
1898 * Configure @port for Accessory Mode @mode. This function will configure the
1899 * muxes needed for @mode.
1901 int typec_set_mode(struct typec_port *port, int mode)
1903 struct typec_mux_state state = { };
1907 return typec_mux_set(port->mux, &state);
1909 EXPORT_SYMBOL_GPL(typec_set_mode);
1911 /* --------------------------------------- */
1914 * typec_get_negotiated_svdm_version - Get negotiated SVDM Version
1915 * @port: USB Type-C Port.
1917 * Get the negotiated SVDM Version. The Version is set to the port default
1918 * value stored in typec_capability on partner registration, and updated after
1919 * a successful Discover Identity if the negotiated value is less than the
1922 * Returns usb_pd_svdm_ver if the partner has been registered otherwise -ENODEV.
1924 int typec_get_negotiated_svdm_version(struct typec_port *port)
1926 enum usb_pd_svdm_ver svdm_version;
1927 struct device *partner_dev;
1929 partner_dev = device_find_child(&port->dev, NULL, partner_match);
1933 svdm_version = to_typec_partner(partner_dev)->svdm_version;
1934 put_device(partner_dev);
1936 return svdm_version;
1938 EXPORT_SYMBOL_GPL(typec_get_negotiated_svdm_version);
1941 * typec_get_drvdata - Return private driver data pointer
1942 * @port: USB Type-C port
1944 void *typec_get_drvdata(struct typec_port *port)
1946 return dev_get_drvdata(&port->dev);
1948 EXPORT_SYMBOL_GPL(typec_get_drvdata);
1951 * typec_port_register_altmode - Register USB Type-C Port Alternate Mode
1952 * @port: USB Type-C Port that supports the alternate mode
1953 * @desc: Description of the alternate mode
1955 * This routine is used to register an alternate mode that @port is capable of
1958 * Returns handle to the alternate mode on success or ERR_PTR on failure.
1960 struct typec_altmode *
1961 typec_port_register_altmode(struct typec_port *port,
1962 const struct typec_altmode_desc *desc)
1964 struct typec_altmode *adev;
1965 struct typec_mux *mux;
1967 mux = typec_mux_get(&port->dev, desc);
1969 return ERR_CAST(mux);
1971 adev = typec_register_altmode(&port->dev, desc);
1975 to_altmode(adev)->mux = mux;
1979 EXPORT_SYMBOL_GPL(typec_port_register_altmode);
1982 * typec_register_port - Register a USB Type-C Port
1983 * @parent: Parent device
1984 * @cap: Description of the port
1986 * Registers a device for USB Type-C Port described in @cap.
1988 * Returns handle to the port on success or ERR_PTR on failure.
1990 struct typec_port *typec_register_port(struct device *parent,
1991 const struct typec_capability *cap)
1993 struct typec_port *port;
1997 port = kzalloc(sizeof(*port), GFP_KERNEL);
1999 return ERR_PTR(-ENOMEM);
2001 id = ida_simple_get(&typec_index_ida, 0, 0, GFP_KERNEL);
2007 switch (cap->type) {
2008 case TYPEC_PORT_SRC:
2009 port->pwr_role = TYPEC_SOURCE;
2010 port->vconn_role = TYPEC_SOURCE;
2012 case TYPEC_PORT_SNK:
2013 port->pwr_role = TYPEC_SINK;
2014 port->vconn_role = TYPEC_SINK;
2016 case TYPEC_PORT_DRP:
2017 if (cap->prefer_role != TYPEC_NO_PREFERRED_ROLE)
2018 port->pwr_role = cap->prefer_role;
2020 port->pwr_role = TYPEC_SINK;
2024 switch (cap->data) {
2025 case TYPEC_PORT_DFP:
2026 port->data_role = TYPEC_HOST;
2028 case TYPEC_PORT_UFP:
2029 port->data_role = TYPEC_DEVICE;
2031 case TYPEC_PORT_DRD:
2032 if (cap->prefer_role == TYPEC_SOURCE)
2033 port->data_role = TYPEC_HOST;
2035 port->data_role = TYPEC_DEVICE;
2039 ida_init(&port->mode_ids);
2040 mutex_init(&port->port_type_lock);
2043 port->ops = cap->ops;
2044 port->port_type = cap->type;
2045 port->prefer_role = cap->prefer_role;
2047 device_initialize(&port->dev);
2048 port->dev.class = typec_class;
2049 port->dev.parent = parent;
2050 port->dev.fwnode = cap->fwnode;
2051 port->dev.type = &typec_port_dev_type;
2052 dev_set_name(&port->dev, "port%d", id);
2053 dev_set_drvdata(&port->dev, cap->driver_data);
2055 port->cap = kmemdup(cap, sizeof(*cap), GFP_KERNEL);
2057 put_device(&port->dev);
2058 return ERR_PTR(-ENOMEM);
2061 port->sw = typec_switch_get(&port->dev);
2062 if (IS_ERR(port->sw)) {
2063 ret = PTR_ERR(port->sw);
2064 put_device(&port->dev);
2065 return ERR_PTR(ret);
2068 port->mux = typec_mux_get(&port->dev, NULL);
2069 if (IS_ERR(port->mux)) {
2070 ret = PTR_ERR(port->mux);
2071 put_device(&port->dev);
2072 return ERR_PTR(ret);
2075 ret = device_add(&port->dev);
2077 dev_err(parent, "failed to register port (%d)\n", ret);
2078 put_device(&port->dev);
2079 return ERR_PTR(ret);
2084 EXPORT_SYMBOL_GPL(typec_register_port);
2087 * typec_unregister_port - Unregister a USB Type-C Port
2088 * @port: The port to be unregistered
2090 * Unregister device created with typec_register_port().
2092 void typec_unregister_port(struct typec_port *port)
2094 if (!IS_ERR_OR_NULL(port))
2095 device_unregister(&port->dev);
2097 EXPORT_SYMBOL_GPL(typec_unregister_port);
2099 static int __init typec_init(void)
2103 ret = bus_register(&typec_bus);
2107 ret = class_register(&typec_mux_class);
2109 goto err_unregister_bus;
2111 typec_class = class_create(THIS_MODULE, "typec");
2112 if (IS_ERR(typec_class)) {
2113 ret = PTR_ERR(typec_class);
2114 goto err_unregister_mux_class;
2119 err_unregister_mux_class:
2120 class_unregister(&typec_mux_class);
2123 bus_unregister(&typec_bus);
2127 subsys_initcall(typec_init);
2129 static void __exit typec_exit(void)
2131 class_destroy(typec_class);
2132 ida_destroy(&typec_index_ida);
2133 bus_unregister(&typec_bus);
2134 class_unregister(&typec_mux_class);
2136 module_exit(typec_exit);
2139 MODULE_LICENSE("GPL v2");
2140 MODULE_DESCRIPTION("USB Type-C Connector Class");