1 // SPDX-License-Identifier: GPL-2.0
3 * USB Type-C Connector Class
5 * Copyright (C) 2017, Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
9 #include <linux/module.h>
10 #include <linux/mutex.h>
11 #include <linux/property.h>
12 #include <linux/slab.h>
13 #include <linux/usb/pd_vdo.h>
14 #include <linux/usb/typec_mux.h>
15 #include <linux/usb/typec_retimer.h>
21 static DEFINE_IDA(typec_index_ida);
23 struct class typec_class = {
28 /* ------------------------------------------------------------------------- */
29 /* Common attributes */
31 static const char * const typec_accessory_modes[] = {
32 [TYPEC_ACCESSORY_NONE] = "none",
33 [TYPEC_ACCESSORY_AUDIO] = "analog_audio",
34 [TYPEC_ACCESSORY_DEBUG] = "debug",
37 /* Product types defined in USB PD Specification R3.0 V2.0 */
38 static const char * const product_type_ufp[8] = {
39 [IDH_PTYPE_NOT_UFP] = "not_ufp",
40 [IDH_PTYPE_HUB] = "hub",
41 [IDH_PTYPE_PERIPH] = "peripheral",
42 [IDH_PTYPE_PSD] = "psd",
43 [IDH_PTYPE_AMA] = "ama",
46 static const char * const product_type_dfp[8] = {
47 [IDH_PTYPE_NOT_DFP] = "not_dfp",
48 [IDH_PTYPE_DFP_HUB] = "hub",
49 [IDH_PTYPE_DFP_HOST] = "host",
50 [IDH_PTYPE_DFP_PB] = "power_brick",
53 static const char * const product_type_cable[8] = {
54 [IDH_PTYPE_NOT_CABLE] = "not_cable",
55 [IDH_PTYPE_PCABLE] = "passive",
56 [IDH_PTYPE_ACABLE] = "active",
57 [IDH_PTYPE_VPD] = "vpd",
60 static struct usb_pd_identity *get_pd_identity(struct device *dev)
62 if (is_typec_partner(dev)) {
63 struct typec_partner *partner = to_typec_partner(dev);
65 return partner->identity;
66 } else if (is_typec_cable(dev)) {
67 struct typec_cable *cable = to_typec_cable(dev);
69 return cable->identity;
74 static const char *get_pd_product_type(struct device *dev)
76 struct typec_port *port = to_typec_port(dev->parent);
77 struct usb_pd_identity *id = get_pd_identity(dev);
78 const char *ptype = NULL;
80 if (is_typec_partner(dev)) {
84 if (port->data_role == TYPEC_HOST)
85 ptype = product_type_ufp[PD_IDH_PTYPE(id->id_header)];
87 ptype = product_type_dfp[PD_IDH_DFP_PTYPE(id->id_header)];
88 } else if (is_typec_cable(dev)) {
90 ptype = product_type_cable[PD_IDH_PTYPE(id->id_header)];
92 ptype = to_typec_cable(dev)->active ?
93 product_type_cable[IDH_PTYPE_ACABLE] :
94 product_type_cable[IDH_PTYPE_PCABLE];
100 static ssize_t id_header_show(struct device *dev, struct device_attribute *attr,
103 struct usb_pd_identity *id = get_pd_identity(dev);
105 return sprintf(buf, "0x%08x\n", id->id_header);
107 static DEVICE_ATTR_RO(id_header);
109 static ssize_t cert_stat_show(struct device *dev, struct device_attribute *attr,
112 struct usb_pd_identity *id = get_pd_identity(dev);
114 return sprintf(buf, "0x%08x\n", id->cert_stat);
116 static DEVICE_ATTR_RO(cert_stat);
118 static ssize_t product_show(struct device *dev, struct device_attribute *attr,
121 struct usb_pd_identity *id = get_pd_identity(dev);
123 return sprintf(buf, "0x%08x\n", id->product);
125 static DEVICE_ATTR_RO(product);
127 static ssize_t product_type_vdo1_show(struct device *dev, struct device_attribute *attr,
130 struct usb_pd_identity *id = get_pd_identity(dev);
132 return sysfs_emit(buf, "0x%08x\n", id->vdo[0]);
134 static DEVICE_ATTR_RO(product_type_vdo1);
136 static ssize_t product_type_vdo2_show(struct device *dev, struct device_attribute *attr,
139 struct usb_pd_identity *id = get_pd_identity(dev);
141 return sysfs_emit(buf, "0x%08x\n", id->vdo[1]);
143 static DEVICE_ATTR_RO(product_type_vdo2);
145 static ssize_t product_type_vdo3_show(struct device *dev, struct device_attribute *attr,
148 struct usb_pd_identity *id = get_pd_identity(dev);
150 return sysfs_emit(buf, "0x%08x\n", id->vdo[2]);
152 static DEVICE_ATTR_RO(product_type_vdo3);
154 static struct attribute *usb_pd_id_attrs[] = {
155 &dev_attr_id_header.attr,
156 &dev_attr_cert_stat.attr,
157 &dev_attr_product.attr,
158 &dev_attr_product_type_vdo1.attr,
159 &dev_attr_product_type_vdo2.attr,
160 &dev_attr_product_type_vdo3.attr,
164 static const struct attribute_group usb_pd_id_group = {
166 .attrs = usb_pd_id_attrs,
169 static const struct attribute_group *usb_pd_id_groups[] = {
174 static void typec_product_type_notify(struct device *dev)
179 ptype = get_pd_product_type(dev);
183 sysfs_notify(&dev->kobj, NULL, "type");
185 envp[0] = kasprintf(GFP_KERNEL, "PRODUCT_TYPE=%s", ptype);
189 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
193 static void typec_report_identity(struct device *dev)
195 sysfs_notify(&dev->kobj, "identity", "id_header");
196 sysfs_notify(&dev->kobj, "identity", "cert_stat");
197 sysfs_notify(&dev->kobj, "identity", "product");
198 sysfs_notify(&dev->kobj, "identity", "product_type_vdo1");
199 sysfs_notify(&dev->kobj, "identity", "product_type_vdo2");
200 sysfs_notify(&dev->kobj, "identity", "product_type_vdo3");
201 typec_product_type_notify(dev);
205 type_show(struct device *dev, struct device_attribute *attr, char *buf)
209 ptype = get_pd_product_type(dev);
213 return sysfs_emit(buf, "%s\n", ptype);
215 static DEVICE_ATTR_RO(type);
217 static ssize_t usb_power_delivery_revision_show(struct device *dev,
218 struct device_attribute *attr,
220 static DEVICE_ATTR_RO(usb_power_delivery_revision);
222 /* ------------------------------------------------------------------------- */
223 /* Alternate Modes */
225 static int altmode_match(struct device *dev, void *data)
227 struct typec_altmode *adev = to_typec_altmode(dev);
228 struct typec_device_id *id = data;
230 if (!is_typec_altmode(dev))
233 return ((adev->svid == id->svid) && (adev->mode == id->mode));
236 static void typec_altmode_set_partner(struct altmode *altmode)
238 struct typec_altmode *adev = &altmode->adev;
239 struct typec_device_id id = { adev->svid, adev->mode, };
240 struct typec_port *port = typec_altmode2port(adev);
241 struct altmode *partner;
244 dev = device_find_child(&port->dev, &id, altmode_match);
248 /* Bind the port alt mode to the partner/plug alt mode. */
249 partner = to_altmode(to_typec_altmode(dev));
250 altmode->partner = partner;
252 /* Bind the partner/plug alt mode to the port alt mode. */
253 if (is_typec_plug(adev->dev.parent)) {
254 struct typec_plug *plug = to_typec_plug(adev->dev.parent);
256 partner->plug[plug->index] = altmode;
258 partner->partner = altmode;
262 static void typec_altmode_put_partner(struct altmode *altmode)
264 struct altmode *partner = altmode->partner;
265 struct typec_altmode *adev;
270 adev = &partner->adev;
272 if (is_typec_plug(adev->dev.parent)) {
273 struct typec_plug *plug = to_typec_plug(adev->dev.parent);
275 partner->plug[plug->index] = NULL;
277 partner->partner = NULL;
279 put_device(&adev->dev);
283 * typec_altmode_update_active - Report Enter/Exit mode
284 * @adev: Handle to the alternate mode
285 * @active: True when the mode has been entered
287 * If a partner or cable plug executes Enter/Exit Mode command successfully, the
288 * drivers use this routine to report the updated state of the mode.
290 void typec_altmode_update_active(struct typec_altmode *adev, bool active)
294 if (adev->active == active)
297 if (!is_typec_port(adev->dev.parent) && adev->dev.driver) {
299 module_put(adev->dev.driver->owner);
301 WARN_ON(!try_module_get(adev->dev.driver->owner));
304 adev->active = active;
305 snprintf(dir, sizeof(dir), "mode%d", adev->mode);
306 sysfs_notify(&adev->dev.kobj, dir, "active");
307 sysfs_notify(&adev->dev.kobj, NULL, "active");
308 kobject_uevent(&adev->dev.kobj, KOBJ_CHANGE);
310 EXPORT_SYMBOL_GPL(typec_altmode_update_active);
313 * typec_altmode2port - Alternate Mode to USB Type-C port
314 * @alt: The Alternate Mode
316 * Returns handle to the port that a cable plug or partner with @alt is
319 struct typec_port *typec_altmode2port(struct typec_altmode *alt)
321 if (is_typec_plug(alt->dev.parent))
322 return to_typec_port(alt->dev.parent->parent->parent);
323 if (is_typec_partner(alt->dev.parent))
324 return to_typec_port(alt->dev.parent->parent);
325 if (is_typec_port(alt->dev.parent))
326 return to_typec_port(alt->dev.parent);
330 EXPORT_SYMBOL_GPL(typec_altmode2port);
333 vdo_show(struct device *dev, struct device_attribute *attr, char *buf)
335 struct typec_altmode *alt = to_typec_altmode(dev);
337 return sprintf(buf, "0x%08x\n", alt->vdo);
339 static DEVICE_ATTR_RO(vdo);
342 description_show(struct device *dev, struct device_attribute *attr, char *buf)
344 struct typec_altmode *alt = to_typec_altmode(dev);
346 return sprintf(buf, "%s\n", alt->desc ? alt->desc : "");
348 static DEVICE_ATTR_RO(description);
351 active_show(struct device *dev, struct device_attribute *attr, char *buf)
353 struct typec_altmode *alt = to_typec_altmode(dev);
355 return sprintf(buf, "%s\n", alt->active ? "yes" : "no");
358 static ssize_t active_store(struct device *dev, struct device_attribute *attr,
359 const char *buf, size_t size)
361 struct typec_altmode *adev = to_typec_altmode(dev);
362 struct altmode *altmode = to_altmode(adev);
366 ret = kstrtobool(buf, &enter);
370 if (adev->active == enter)
373 if (is_typec_port(adev->dev.parent)) {
374 typec_altmode_update_active(adev, enter);
376 /* Make sure that the partner exits the mode before disabling */
377 if (altmode->partner && !enter && altmode->partner->adev.active)
378 typec_altmode_exit(&altmode->partner->adev);
379 } else if (altmode->partner) {
380 if (enter && !altmode->partner->adev.active) {
381 dev_warn(dev, "port has the mode disabled\n");
386 /* Note: If there is no driver, the mode will not be entered */
387 if (adev->ops && adev->ops->activate) {
388 ret = adev->ops->activate(adev, enter);
395 static DEVICE_ATTR_RW(active);
398 supported_roles_show(struct device *dev, struct device_attribute *attr,
401 struct altmode *alt = to_altmode(to_typec_altmode(dev));
404 switch (alt->roles) {
406 ret = sprintf(buf, "source\n");
409 ret = sprintf(buf, "sink\n");
413 ret = sprintf(buf, "source sink\n");
418 static DEVICE_ATTR_RO(supported_roles);
421 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
423 struct typec_altmode *adev = to_typec_altmode(dev);
425 return sprintf(buf, "%u\n", adev->mode);
427 static DEVICE_ATTR_RO(mode);
430 svid_show(struct device *dev, struct device_attribute *attr, char *buf)
432 struct typec_altmode *adev = to_typec_altmode(dev);
434 return sprintf(buf, "%04x\n", adev->svid);
436 static DEVICE_ATTR_RO(svid);
438 static struct attribute *typec_altmode_attrs[] = {
439 &dev_attr_active.attr,
446 static umode_t typec_altmode_attr_is_visible(struct kobject *kobj,
447 struct attribute *attr, int n)
449 struct typec_altmode *adev = to_typec_altmode(kobj_to_dev(kobj));
451 if (attr == &dev_attr_active.attr)
452 if (!adev->ops || !adev->ops->activate)
458 static const struct attribute_group typec_altmode_group = {
459 .is_visible = typec_altmode_attr_is_visible,
460 .attrs = typec_altmode_attrs,
463 static const struct attribute_group *typec_altmode_groups[] = {
464 &typec_altmode_group,
468 static int altmode_id_get(struct device *dev)
472 if (is_typec_partner(dev))
473 ids = &to_typec_partner(dev)->mode_ids;
474 else if (is_typec_plug(dev))
475 ids = &to_typec_plug(dev)->mode_ids;
477 ids = &to_typec_port(dev)->mode_ids;
479 return ida_simple_get(ids, 0, 0, GFP_KERNEL);
482 static void altmode_id_remove(struct device *dev, int id)
486 if (is_typec_partner(dev))
487 ids = &to_typec_partner(dev)->mode_ids;
488 else if (is_typec_plug(dev))
489 ids = &to_typec_plug(dev)->mode_ids;
491 ids = &to_typec_port(dev)->mode_ids;
493 ida_simple_remove(ids, id);
496 static void typec_altmode_release(struct device *dev)
498 struct altmode *alt = to_altmode(to_typec_altmode(dev));
500 typec_altmode_put_partner(alt);
502 altmode_id_remove(alt->adev.dev.parent, alt->id);
506 const struct device_type typec_altmode_dev_type = {
507 .name = "typec_alternate_mode",
508 .groups = typec_altmode_groups,
509 .release = typec_altmode_release,
512 static struct typec_altmode *
513 typec_register_altmode(struct device *parent,
514 const struct typec_altmode_desc *desc)
516 unsigned int id = altmode_id_get(parent);
517 bool is_port = is_typec_port(parent);
521 alt = kzalloc(sizeof(*alt), GFP_KERNEL);
523 altmode_id_remove(parent, id);
524 return ERR_PTR(-ENOMEM);
527 alt->adev.svid = desc->svid;
528 alt->adev.mode = desc->mode;
529 alt->adev.vdo = desc->vdo;
530 alt->roles = desc->roles;
533 alt->attrs[0] = &dev_attr_vdo.attr;
534 alt->attrs[1] = &dev_attr_description.attr;
535 alt->attrs[2] = &dev_attr_active.attr;
538 alt->attrs[3] = &dev_attr_supported_roles.attr;
539 alt->adev.active = true; /* Enabled by default */
542 sprintf(alt->group_name, "mode%d", desc->mode);
543 alt->group.name = alt->group_name;
544 alt->group.attrs = alt->attrs;
545 alt->groups[0] = &alt->group;
547 alt->adev.dev.parent = parent;
548 alt->adev.dev.groups = alt->groups;
549 alt->adev.dev.type = &typec_altmode_dev_type;
550 dev_set_name(&alt->adev.dev, "%s.%u", dev_name(parent), id);
552 /* Link partners and plugs with the ports */
554 typec_altmode_set_partner(alt);
556 /* The partners are bind to drivers */
557 if (is_typec_partner(parent))
558 alt->adev.dev.bus = &typec_bus;
560 /* Plug alt modes need a class to generate udev events. */
561 if (is_typec_plug(parent))
562 alt->adev.dev.class = &typec_class;
564 ret = device_register(&alt->adev.dev);
566 dev_err(parent, "failed to register alternate mode (%d)\n",
568 put_device(&alt->adev.dev);
576 * typec_unregister_altmode - Unregister Alternate Mode
577 * @adev: The alternate mode to be unregistered
579 * Unregister device created with typec_partner_register_altmode(),
580 * typec_plug_register_altmode() or typec_port_register_altmode().
582 void typec_unregister_altmode(struct typec_altmode *adev)
584 if (IS_ERR_OR_NULL(adev))
586 typec_mux_put(to_altmode(adev)->mux);
587 device_unregister(&adev->dev);
589 EXPORT_SYMBOL_GPL(typec_unregister_altmode);
591 /* ------------------------------------------------------------------------- */
592 /* Type-C Partners */
594 static ssize_t accessory_mode_show(struct device *dev,
595 struct device_attribute *attr,
598 struct typec_partner *p = to_typec_partner(dev);
600 return sprintf(buf, "%s\n", typec_accessory_modes[p->accessory]);
602 static DEVICE_ATTR_RO(accessory_mode);
604 static ssize_t supports_usb_power_delivery_show(struct device *dev,
605 struct device_attribute *attr,
608 struct typec_partner *p = to_typec_partner(dev);
610 return sprintf(buf, "%s\n", p->usb_pd ? "yes" : "no");
612 static DEVICE_ATTR_RO(supports_usb_power_delivery);
614 static ssize_t number_of_alternate_modes_show(struct device *dev, struct device_attribute *attr,
617 struct typec_partner *partner;
618 struct typec_plug *plug;
621 if (is_typec_partner(dev)) {
622 partner = to_typec_partner(dev);
623 num_altmodes = partner->num_altmodes;
624 } else if (is_typec_plug(dev)) {
625 plug = to_typec_plug(dev);
626 num_altmodes = plug->num_altmodes;
631 return sysfs_emit(buf, "%d\n", num_altmodes);
633 static DEVICE_ATTR_RO(number_of_alternate_modes);
635 static struct attribute *typec_partner_attrs[] = {
636 &dev_attr_accessory_mode.attr,
637 &dev_attr_supports_usb_power_delivery.attr,
638 &dev_attr_number_of_alternate_modes.attr,
640 &dev_attr_usb_power_delivery_revision.attr,
644 static umode_t typec_partner_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
646 struct typec_partner *partner = to_typec_partner(kobj_to_dev(kobj));
648 if (attr == &dev_attr_number_of_alternate_modes.attr) {
649 if (partner->num_altmodes < 0)
653 if (attr == &dev_attr_type.attr)
654 if (!get_pd_product_type(kobj_to_dev(kobj)))
660 static const struct attribute_group typec_partner_group = {
661 .is_visible = typec_partner_attr_is_visible,
662 .attrs = typec_partner_attrs
665 static const struct attribute_group *typec_partner_groups[] = {
666 &typec_partner_group,
670 static void typec_partner_release(struct device *dev)
672 struct typec_partner *partner = to_typec_partner(dev);
674 ida_destroy(&partner->mode_ids);
678 const struct device_type typec_partner_dev_type = {
679 .name = "typec_partner",
680 .groups = typec_partner_groups,
681 .release = typec_partner_release,
685 * typec_partner_set_identity - Report result from Discover Identity command
686 * @partner: The partner updated identity values
688 * This routine is used to report that the result of Discover Identity USB power
689 * delivery command has become available.
691 int typec_partner_set_identity(struct typec_partner *partner)
693 if (!partner->identity)
696 typec_report_identity(&partner->dev);
699 EXPORT_SYMBOL_GPL(typec_partner_set_identity);
702 * typec_partner_set_pd_revision - Set the PD revision supported by the partner
703 * @partner: The partner to be updated.
704 * @pd_revision: USB Power Delivery Specification Revision supported by partner
706 * This routine is used to report that the PD revision of the port partner has
709 void typec_partner_set_pd_revision(struct typec_partner *partner, u16 pd_revision)
711 if (partner->pd_revision == pd_revision)
714 partner->pd_revision = pd_revision;
715 sysfs_notify(&partner->dev.kobj, NULL, "usb_power_delivery_revision");
716 if (pd_revision != 0 && !partner->usb_pd) {
718 sysfs_notify(&partner->dev.kobj, NULL,
719 "supports_usb_power_delivery");
721 kobject_uevent(&partner->dev.kobj, KOBJ_CHANGE);
723 EXPORT_SYMBOL_GPL(typec_partner_set_pd_revision);
726 * typec_partner_set_usb_power_delivery - Declare USB Power Delivery Contract.
727 * @partner: The partner device.
728 * @pd: The USB PD instance.
730 * This routine can be used to declare USB Power Delivery Contract with @partner
731 * by linking @partner to @pd which contains the objects that were used during the
732 * negotiation of the contract.
734 * If @pd is NULL, the link is removed and the contract with @partner has ended.
736 int typec_partner_set_usb_power_delivery(struct typec_partner *partner,
737 struct usb_power_delivery *pd)
741 if (IS_ERR_OR_NULL(partner) || partner->pd == pd)
745 ret = usb_power_delivery_link_device(pd, &partner->dev);
749 usb_power_delivery_unlink_device(partner->pd, &partner->dev);
756 EXPORT_SYMBOL_GPL(typec_partner_set_usb_power_delivery);
759 * typec_partner_set_num_altmodes - Set the number of available partner altmodes
760 * @partner: The partner to be updated.
761 * @num_altmodes: The number of altmodes we want to specify as available.
763 * This routine is used to report the number of alternate modes supported by the
764 * partner. This value is *not* enforced in alternate mode registration routines.
766 * @partner.num_altmodes is set to -1 on partner registration, denoting that
767 * a valid value has not been set for it yet.
769 * Returns 0 on success or negative error number on failure.
771 int typec_partner_set_num_altmodes(struct typec_partner *partner, int num_altmodes)
775 if (num_altmodes < 0)
778 partner->num_altmodes = num_altmodes;
779 ret = sysfs_update_group(&partner->dev.kobj, &typec_partner_group);
783 sysfs_notify(&partner->dev.kobj, NULL, "number_of_alternate_modes");
784 kobject_uevent(&partner->dev.kobj, KOBJ_CHANGE);
788 EXPORT_SYMBOL_GPL(typec_partner_set_num_altmodes);
791 * typec_partner_register_altmode - Register USB Type-C Partner Alternate Mode
792 * @partner: USB Type-C Partner that supports the alternate mode
793 * @desc: Description of the alternate mode
795 * This routine is used to register each alternate mode individually that
796 * @partner has listed in response to Discover SVIDs command. The modes for a
797 * SVID listed in response to Discover Modes command need to be listed in an
800 * Returns handle to the alternate mode on success or ERR_PTR on failure.
802 struct typec_altmode *
803 typec_partner_register_altmode(struct typec_partner *partner,
804 const struct typec_altmode_desc *desc)
806 return typec_register_altmode(&partner->dev, desc);
808 EXPORT_SYMBOL_GPL(typec_partner_register_altmode);
811 * typec_partner_set_svdm_version - Set negotiated Structured VDM (SVDM) Version
812 * @partner: USB Type-C Partner that supports SVDM
813 * @svdm_version: Negotiated SVDM Version
815 * This routine is used to save the negotiated SVDM Version.
817 void typec_partner_set_svdm_version(struct typec_partner *partner,
818 enum usb_pd_svdm_ver svdm_version)
820 partner->svdm_version = svdm_version;
822 EXPORT_SYMBOL_GPL(typec_partner_set_svdm_version);
825 * typec_register_partner - Register a USB Type-C Partner
826 * @port: The USB Type-C Port the partner is connected to
827 * @desc: Description of the partner
829 * Registers a device for USB Type-C Partner described in @desc.
831 * Returns handle to the partner on success or ERR_PTR on failure.
833 struct typec_partner *typec_register_partner(struct typec_port *port,
834 struct typec_partner_desc *desc)
836 struct typec_partner *partner;
839 partner = kzalloc(sizeof(*partner), GFP_KERNEL);
841 return ERR_PTR(-ENOMEM);
843 ida_init(&partner->mode_ids);
844 partner->usb_pd = desc->usb_pd;
845 partner->accessory = desc->accessory;
846 partner->num_altmodes = -1;
847 partner->pd_revision = desc->pd_revision;
848 partner->svdm_version = port->cap->svdm_version;
850 if (desc->identity) {
852 * Creating directory for the identity only if the driver is
853 * able to provide data to it.
855 partner->dev.groups = usb_pd_id_groups;
856 partner->identity = desc->identity;
859 partner->dev.class = &typec_class;
860 partner->dev.parent = &port->dev;
861 partner->dev.type = &typec_partner_dev_type;
862 dev_set_name(&partner->dev, "%s-partner", dev_name(&port->dev));
864 ret = device_register(&partner->dev);
866 dev_err(&port->dev, "failed to register partner (%d)\n", ret);
867 put_device(&partner->dev);
873 EXPORT_SYMBOL_GPL(typec_register_partner);
876 * typec_unregister_partner - Unregister a USB Type-C Partner
877 * @partner: The partner to be unregistered
879 * Unregister device created with typec_register_partner().
881 void typec_unregister_partner(struct typec_partner *partner)
883 if (!IS_ERR_OR_NULL(partner))
884 device_unregister(&partner->dev);
886 EXPORT_SYMBOL_GPL(typec_unregister_partner);
888 /* ------------------------------------------------------------------------- */
889 /* Type-C Cable Plugs */
891 static void typec_plug_release(struct device *dev)
893 struct typec_plug *plug = to_typec_plug(dev);
895 ida_destroy(&plug->mode_ids);
899 static struct attribute *typec_plug_attrs[] = {
900 &dev_attr_number_of_alternate_modes.attr,
904 static umode_t typec_plug_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
906 struct typec_plug *plug = to_typec_plug(kobj_to_dev(kobj));
908 if (attr == &dev_attr_number_of_alternate_modes.attr) {
909 if (plug->num_altmodes < 0)
916 static const struct attribute_group typec_plug_group = {
917 .is_visible = typec_plug_attr_is_visible,
918 .attrs = typec_plug_attrs
921 static const struct attribute_group *typec_plug_groups[] = {
926 const struct device_type typec_plug_dev_type = {
927 .name = "typec_plug",
928 .groups = typec_plug_groups,
929 .release = typec_plug_release,
933 * typec_plug_set_num_altmodes - Set the number of available plug altmodes
934 * @plug: The plug to be updated.
935 * @num_altmodes: The number of altmodes we want to specify as available.
937 * This routine is used to report the number of alternate modes supported by the
938 * plug. This value is *not* enforced in alternate mode registration routines.
940 * @plug.num_altmodes is set to -1 on plug registration, denoting that
941 * a valid value has not been set for it yet.
943 * Returns 0 on success or negative error number on failure.
945 int typec_plug_set_num_altmodes(struct typec_plug *plug, int num_altmodes)
949 if (num_altmodes < 0)
952 plug->num_altmodes = num_altmodes;
953 ret = sysfs_update_group(&plug->dev.kobj, &typec_plug_group);
957 sysfs_notify(&plug->dev.kobj, NULL, "number_of_alternate_modes");
958 kobject_uevent(&plug->dev.kobj, KOBJ_CHANGE);
962 EXPORT_SYMBOL_GPL(typec_plug_set_num_altmodes);
965 * typec_plug_register_altmode - Register USB Type-C Cable Plug Alternate Mode
966 * @plug: USB Type-C Cable Plug that supports the alternate mode
967 * @desc: Description of the alternate mode
969 * This routine is used to register each alternate mode individually that @plug
970 * has listed in response to Discover SVIDs command. The modes for a SVID that
971 * the plug lists in response to Discover Modes command need to be listed in an
974 * Returns handle to the alternate mode on success or ERR_PTR on failure.
976 struct typec_altmode *
977 typec_plug_register_altmode(struct typec_plug *plug,
978 const struct typec_altmode_desc *desc)
980 return typec_register_altmode(&plug->dev, desc);
982 EXPORT_SYMBOL_GPL(typec_plug_register_altmode);
985 * typec_register_plug - Register a USB Type-C Cable Plug
986 * @cable: USB Type-C Cable with the plug
987 * @desc: Description of the cable plug
989 * Registers a device for USB Type-C Cable Plug described in @desc. A USB Type-C
990 * Cable Plug represents a plug with electronics in it that can response to USB
991 * Power Delivery SOP Prime or SOP Double Prime packages.
993 * Returns handle to the cable plug on success or ERR_PTR on failure.
995 struct typec_plug *typec_register_plug(struct typec_cable *cable,
996 struct typec_plug_desc *desc)
998 struct typec_plug *plug;
1002 plug = kzalloc(sizeof(*plug), GFP_KERNEL);
1004 return ERR_PTR(-ENOMEM);
1006 sprintf(name, "plug%d", desc->index);
1008 ida_init(&plug->mode_ids);
1009 plug->num_altmodes = -1;
1010 plug->index = desc->index;
1011 plug->dev.class = &typec_class;
1012 plug->dev.parent = &cable->dev;
1013 plug->dev.type = &typec_plug_dev_type;
1014 dev_set_name(&plug->dev, "%s-%s", dev_name(cable->dev.parent), name);
1016 ret = device_register(&plug->dev);
1018 dev_err(&cable->dev, "failed to register plug (%d)\n", ret);
1019 put_device(&plug->dev);
1020 return ERR_PTR(ret);
1025 EXPORT_SYMBOL_GPL(typec_register_plug);
1028 * typec_unregister_plug - Unregister a USB Type-C Cable Plug
1029 * @plug: The cable plug to be unregistered
1031 * Unregister device created with typec_register_plug().
1033 void typec_unregister_plug(struct typec_plug *plug)
1035 if (!IS_ERR_OR_NULL(plug))
1036 device_unregister(&plug->dev);
1038 EXPORT_SYMBOL_GPL(typec_unregister_plug);
1042 static const char * const typec_plug_types[] = {
1043 [USB_PLUG_NONE] = "unknown",
1044 [USB_PLUG_TYPE_A] = "type-a",
1045 [USB_PLUG_TYPE_B] = "type-b",
1046 [USB_PLUG_TYPE_C] = "type-c",
1047 [USB_PLUG_CAPTIVE] = "captive",
1050 static ssize_t plug_type_show(struct device *dev,
1051 struct device_attribute *attr, char *buf)
1053 struct typec_cable *cable = to_typec_cable(dev);
1055 return sprintf(buf, "%s\n", typec_plug_types[cable->type]);
1057 static DEVICE_ATTR_RO(plug_type);
1059 static struct attribute *typec_cable_attrs[] = {
1060 &dev_attr_type.attr,
1061 &dev_attr_plug_type.attr,
1062 &dev_attr_usb_power_delivery_revision.attr,
1065 ATTRIBUTE_GROUPS(typec_cable);
1067 static void typec_cable_release(struct device *dev)
1069 struct typec_cable *cable = to_typec_cable(dev);
1074 const struct device_type typec_cable_dev_type = {
1075 .name = "typec_cable",
1076 .groups = typec_cable_groups,
1077 .release = typec_cable_release,
1080 static int cable_match(struct device *dev, void *data)
1082 return is_typec_cable(dev);
1086 * typec_cable_get - Get a reference to the USB Type-C cable
1087 * @port: The USB Type-C Port the cable is connected to
1089 * The caller must decrement the reference count with typec_cable_put() after
1092 struct typec_cable *typec_cable_get(struct typec_port *port)
1096 dev = device_find_child(&port->dev, NULL, cable_match);
1100 return to_typec_cable(dev);
1102 EXPORT_SYMBOL_GPL(typec_cable_get);
1105 * typec_cable_put - Decrement the reference count on USB Type-C cable
1106 * @cable: The USB Type-C cable
1108 void typec_cable_put(struct typec_cable *cable)
1110 put_device(&cable->dev);
1112 EXPORT_SYMBOL_GPL(typec_cable_put);
1115 * typec_cable_is_active - Check is the USB Type-C cable active or passive
1116 * @cable: The USB Type-C Cable
1118 * Return 1 if the cable is active or 0 if it's passive.
1120 int typec_cable_is_active(struct typec_cable *cable)
1122 return cable->active;
1124 EXPORT_SYMBOL_GPL(typec_cable_is_active);
1127 * typec_cable_set_identity - Report result from Discover Identity command
1128 * @cable: The cable updated identity values
1130 * This routine is used to report that the result of Discover Identity USB power
1131 * delivery command has become available.
1133 int typec_cable_set_identity(struct typec_cable *cable)
1135 if (!cable->identity)
1138 typec_report_identity(&cable->dev);
1141 EXPORT_SYMBOL_GPL(typec_cable_set_identity);
1144 * typec_register_cable - Register a USB Type-C Cable
1145 * @port: The USB Type-C Port the cable is connected to
1146 * @desc: Description of the cable
1148 * Registers a device for USB Type-C Cable described in @desc. The cable will be
1149 * parent for the optional cable plug devises.
1151 * Returns handle to the cable on success or ERR_PTR on failure.
1153 struct typec_cable *typec_register_cable(struct typec_port *port,
1154 struct typec_cable_desc *desc)
1156 struct typec_cable *cable;
1159 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
1161 return ERR_PTR(-ENOMEM);
1163 cable->type = desc->type;
1164 cable->active = desc->active;
1165 cable->pd_revision = desc->pd_revision;
1167 if (desc->identity) {
1169 * Creating directory for the identity only if the driver is
1170 * able to provide data to it.
1172 cable->dev.groups = usb_pd_id_groups;
1173 cable->identity = desc->identity;
1176 cable->dev.class = &typec_class;
1177 cable->dev.parent = &port->dev;
1178 cable->dev.type = &typec_cable_dev_type;
1179 dev_set_name(&cable->dev, "%s-cable", dev_name(&port->dev));
1181 ret = device_register(&cable->dev);
1183 dev_err(&port->dev, "failed to register cable (%d)\n", ret);
1184 put_device(&cable->dev);
1185 return ERR_PTR(ret);
1190 EXPORT_SYMBOL_GPL(typec_register_cable);
1193 * typec_unregister_cable - Unregister a USB Type-C Cable
1194 * @cable: The cable to be unregistered
1196 * Unregister device created with typec_register_cable().
1198 void typec_unregister_cable(struct typec_cable *cable)
1200 if (!IS_ERR_OR_NULL(cable))
1201 device_unregister(&cable->dev);
1203 EXPORT_SYMBOL_GPL(typec_unregister_cable);
1205 /* ------------------------------------------------------------------------- */
1206 /* USB Type-C ports */
1209 * typec_port_set_usb_power_delivery - Assign USB PD for port.
1210 * @port: USB Type-C port.
1211 * @pd: USB PD instance.
1213 * This routine can be used to set the USB Power Delivery Capabilities for @port
1214 * that it will advertise to the partner.
1216 * If @pd is NULL, the assignment is removed.
1218 int typec_port_set_usb_power_delivery(struct typec_port *port, struct usb_power_delivery *pd)
1222 if (IS_ERR_OR_NULL(port) || port->pd == pd)
1226 ret = usb_power_delivery_link_device(pd, &port->dev);
1230 usb_power_delivery_unlink_device(port->pd, &port->dev);
1237 EXPORT_SYMBOL_GPL(typec_port_set_usb_power_delivery);
1239 static ssize_t select_usb_power_delivery_store(struct device *dev,
1240 struct device_attribute *attr,
1241 const char *buf, size_t size)
1243 struct typec_port *port = to_typec_port(dev);
1244 struct usb_power_delivery *pd;
1246 if (!port->ops || !port->ops->pd_set)
1249 pd = usb_power_delivery_find(buf);
1253 return port->ops->pd_set(port, pd);
1256 static ssize_t select_usb_power_delivery_show(struct device *dev,
1257 struct device_attribute *attr, char *buf)
1259 struct typec_port *port = to_typec_port(dev);
1260 struct usb_power_delivery **pds;
1261 struct usb_power_delivery *pd;
1264 if (!port->ops || !port->ops->pd_get)
1267 pds = port->ops->pd_get(port);
1271 for (pd = pds[0]; pd; pd++) {
1273 ret += sysfs_emit(buf + ret, "[%s] ", dev_name(&pd->dev));
1275 ret += sysfs_emit(buf + ret, "%s ", dev_name(&pd->dev));
1278 buf[ret - 1] = '\n';
1282 static DEVICE_ATTR_RW(select_usb_power_delivery);
1284 static struct attribute *port_attrs[] = {
1285 &dev_attr_select_usb_power_delivery.attr,
1289 static umode_t port_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
1291 struct typec_port *port = to_typec_port(kobj_to_dev(kobj));
1293 if (!port->pd || !port->ops || !port->ops->pd_get)
1295 if (!port->ops->pd_set)
1301 static const struct attribute_group pd_group = {
1302 .is_visible = port_attr_is_visible,
1303 .attrs = port_attrs,
1306 static const char * const typec_orientations[] = {
1307 [TYPEC_ORIENTATION_NONE] = "unknown",
1308 [TYPEC_ORIENTATION_NORMAL] = "normal",
1309 [TYPEC_ORIENTATION_REVERSE] = "reverse",
1312 static const char * const typec_roles[] = {
1313 [TYPEC_SINK] = "sink",
1314 [TYPEC_SOURCE] = "source",
1317 static const char * const typec_data_roles[] = {
1318 [TYPEC_DEVICE] = "device",
1319 [TYPEC_HOST] = "host",
1322 static const char * const typec_port_power_roles[] = {
1323 [TYPEC_PORT_SRC] = "source",
1324 [TYPEC_PORT_SNK] = "sink",
1325 [TYPEC_PORT_DRP] = "dual",
1328 static const char * const typec_port_data_roles[] = {
1329 [TYPEC_PORT_DFP] = "host",
1330 [TYPEC_PORT_UFP] = "device",
1331 [TYPEC_PORT_DRD] = "dual",
1334 static const char * const typec_port_types_drp[] = {
1335 [TYPEC_PORT_SRC] = "dual [source] sink",
1336 [TYPEC_PORT_SNK] = "dual source [sink]",
1337 [TYPEC_PORT_DRP] = "[dual] source sink",
1341 preferred_role_store(struct device *dev, struct device_attribute *attr,
1342 const char *buf, size_t size)
1344 struct typec_port *port = to_typec_port(dev);
1348 if (port->cap->type != TYPEC_PORT_DRP) {
1349 dev_dbg(dev, "Preferred role only supported with DRP ports\n");
1353 if (!port->ops || !port->ops->try_role) {
1354 dev_dbg(dev, "Setting preferred role not supported\n");
1358 role = sysfs_match_string(typec_roles, buf);
1360 if (sysfs_streq(buf, "none"))
1361 role = TYPEC_NO_PREFERRED_ROLE;
1366 ret = port->ops->try_role(port, role);
1370 port->prefer_role = role;
1375 preferred_role_show(struct device *dev, struct device_attribute *attr,
1378 struct typec_port *port = to_typec_port(dev);
1380 if (port->cap->type != TYPEC_PORT_DRP)
1383 if (port->prefer_role < 0)
1386 return sprintf(buf, "%s\n", typec_roles[port->prefer_role]);
1388 static DEVICE_ATTR_RW(preferred_role);
1390 static ssize_t data_role_store(struct device *dev,
1391 struct device_attribute *attr,
1392 const char *buf, size_t size)
1394 struct typec_port *port = to_typec_port(dev);
1397 if (!port->ops || !port->ops->dr_set) {
1398 dev_dbg(dev, "data role swapping not supported\n");
1402 ret = sysfs_match_string(typec_data_roles, buf);
1406 mutex_lock(&port->port_type_lock);
1407 if (port->cap->data != TYPEC_PORT_DRD) {
1409 goto unlock_and_ret;
1412 ret = port->ops->dr_set(port, ret);
1414 goto unlock_and_ret;
1418 mutex_unlock(&port->port_type_lock);
1422 static ssize_t data_role_show(struct device *dev,
1423 struct device_attribute *attr, char *buf)
1425 struct typec_port *port = to_typec_port(dev);
1427 if (port->cap->data == TYPEC_PORT_DRD)
1428 return sprintf(buf, "%s\n", port->data_role == TYPEC_HOST ?
1429 "[host] device" : "host [device]");
1431 return sprintf(buf, "[%s]\n", typec_data_roles[port->data_role]);
1433 static DEVICE_ATTR_RW(data_role);
1435 static ssize_t power_role_store(struct device *dev,
1436 struct device_attribute *attr,
1437 const char *buf, size_t size)
1439 struct typec_port *port = to_typec_port(dev);
1442 if (!port->ops || !port->ops->pr_set) {
1443 dev_dbg(dev, "power role swapping not supported\n");
1447 if (port->pwr_opmode != TYPEC_PWR_MODE_PD) {
1448 dev_dbg(dev, "partner unable to swap power role\n");
1452 ret = sysfs_match_string(typec_roles, buf);
1456 mutex_lock(&port->port_type_lock);
1457 if (port->port_type != TYPEC_PORT_DRP) {
1458 dev_dbg(dev, "port type fixed at \"%s\"",
1459 typec_port_power_roles[port->port_type]);
1461 goto unlock_and_ret;
1464 ret = port->ops->pr_set(port, ret);
1466 goto unlock_and_ret;
1470 mutex_unlock(&port->port_type_lock);
1474 static ssize_t power_role_show(struct device *dev,
1475 struct device_attribute *attr, char *buf)
1477 struct typec_port *port = to_typec_port(dev);
1479 if (port->cap->type == TYPEC_PORT_DRP)
1480 return sprintf(buf, "%s\n", port->pwr_role == TYPEC_SOURCE ?
1481 "[source] sink" : "source [sink]");
1483 return sprintf(buf, "[%s]\n", typec_roles[port->pwr_role]);
1485 static DEVICE_ATTR_RW(power_role);
1488 port_type_store(struct device *dev, struct device_attribute *attr,
1489 const char *buf, size_t size)
1491 struct typec_port *port = to_typec_port(dev);
1493 enum typec_port_type type;
1495 if (port->cap->type != TYPEC_PORT_DRP ||
1496 !port->ops || !port->ops->port_type_set) {
1497 dev_dbg(dev, "changing port type not supported\n");
1501 ret = sysfs_match_string(typec_port_power_roles, buf);
1506 mutex_lock(&port->port_type_lock);
1508 if (port->port_type == type) {
1510 goto unlock_and_ret;
1513 ret = port->ops->port_type_set(port, type);
1515 goto unlock_and_ret;
1517 port->port_type = type;
1521 mutex_unlock(&port->port_type_lock);
1526 port_type_show(struct device *dev, struct device_attribute *attr,
1529 struct typec_port *port = to_typec_port(dev);
1531 if (port->cap->type == TYPEC_PORT_DRP)
1532 return sprintf(buf, "%s\n",
1533 typec_port_types_drp[port->port_type]);
1535 return sprintf(buf, "[%s]\n", typec_port_power_roles[port->cap->type]);
1537 static DEVICE_ATTR_RW(port_type);
1539 static const char * const typec_pwr_opmodes[] = {
1540 [TYPEC_PWR_MODE_USB] = "default",
1541 [TYPEC_PWR_MODE_1_5A] = "1.5A",
1542 [TYPEC_PWR_MODE_3_0A] = "3.0A",
1543 [TYPEC_PWR_MODE_PD] = "usb_power_delivery",
1546 static ssize_t power_operation_mode_show(struct device *dev,
1547 struct device_attribute *attr,
1550 struct typec_port *port = to_typec_port(dev);
1552 return sprintf(buf, "%s\n", typec_pwr_opmodes[port->pwr_opmode]);
1554 static DEVICE_ATTR_RO(power_operation_mode);
1556 static ssize_t vconn_source_store(struct device *dev,
1557 struct device_attribute *attr,
1558 const char *buf, size_t size)
1560 struct typec_port *port = to_typec_port(dev);
1564 if (!port->cap->pd_revision) {
1565 dev_dbg(dev, "VCONN swap depends on USB Power Delivery\n");
1569 if (!port->ops || !port->ops->vconn_set) {
1570 dev_dbg(dev, "VCONN swapping not supported\n");
1574 ret = kstrtobool(buf, &source);
1578 ret = port->ops->vconn_set(port, (enum typec_role)source);
1585 static ssize_t vconn_source_show(struct device *dev,
1586 struct device_attribute *attr, char *buf)
1588 struct typec_port *port = to_typec_port(dev);
1590 return sprintf(buf, "%s\n",
1591 port->vconn_role == TYPEC_SOURCE ? "yes" : "no");
1593 static DEVICE_ATTR_RW(vconn_source);
1595 static ssize_t supported_accessory_modes_show(struct device *dev,
1596 struct device_attribute *attr,
1599 struct typec_port *port = to_typec_port(dev);
1603 for (i = 0; i < ARRAY_SIZE(port->cap->accessory); i++) {
1604 if (port->cap->accessory[i])
1605 ret += sprintf(buf + ret, "%s ",
1606 typec_accessory_modes[port->cap->accessory[i]]);
1610 return sprintf(buf, "none\n");
1612 buf[ret - 1] = '\n';
1616 static DEVICE_ATTR_RO(supported_accessory_modes);
1618 static ssize_t usb_typec_revision_show(struct device *dev,
1619 struct device_attribute *attr,
1622 struct typec_port *port = to_typec_port(dev);
1623 u16 rev = port->cap->revision;
1625 return sprintf(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
1627 static DEVICE_ATTR_RO(usb_typec_revision);
1629 static ssize_t usb_power_delivery_revision_show(struct device *dev,
1630 struct device_attribute *attr,
1635 if (is_typec_partner(dev)) {
1636 struct typec_partner *partner = to_typec_partner(dev);
1638 rev = partner->pd_revision;
1639 } else if (is_typec_cable(dev)) {
1640 struct typec_cable *cable = to_typec_cable(dev);
1642 rev = cable->pd_revision;
1643 } else if (is_typec_port(dev)) {
1644 struct typec_port *p = to_typec_port(dev);
1646 rev = p->cap->pd_revision;
1648 return sysfs_emit(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
1651 static ssize_t orientation_show(struct device *dev,
1652 struct device_attribute *attr,
1655 struct typec_port *port = to_typec_port(dev);
1657 return sprintf(buf, "%s\n", typec_orientations[port->orientation]);
1659 static DEVICE_ATTR_RO(orientation);
1661 static struct attribute *typec_attrs[] = {
1662 &dev_attr_data_role.attr,
1663 &dev_attr_power_operation_mode.attr,
1664 &dev_attr_power_role.attr,
1665 &dev_attr_preferred_role.attr,
1666 &dev_attr_supported_accessory_modes.attr,
1667 &dev_attr_usb_power_delivery_revision.attr,
1668 &dev_attr_usb_typec_revision.attr,
1669 &dev_attr_vconn_source.attr,
1670 &dev_attr_port_type.attr,
1671 &dev_attr_orientation.attr,
1675 static umode_t typec_attr_is_visible(struct kobject *kobj,
1676 struct attribute *attr, int n)
1678 struct typec_port *port = to_typec_port(kobj_to_dev(kobj));
1680 if (attr == &dev_attr_data_role.attr) {
1681 if (port->cap->data != TYPEC_PORT_DRD ||
1682 !port->ops || !port->ops->dr_set)
1684 } else if (attr == &dev_attr_power_role.attr) {
1685 if (port->cap->type != TYPEC_PORT_DRP ||
1686 !port->ops || !port->ops->pr_set)
1688 } else if (attr == &dev_attr_vconn_source.attr) {
1689 if (!port->cap->pd_revision ||
1690 !port->ops || !port->ops->vconn_set)
1692 } else if (attr == &dev_attr_preferred_role.attr) {
1693 if (port->cap->type != TYPEC_PORT_DRP ||
1694 !port->ops || !port->ops->try_role)
1696 } else if (attr == &dev_attr_port_type.attr) {
1697 if (!port->ops || !port->ops->port_type_set)
1699 if (port->cap->type != TYPEC_PORT_DRP)
1701 } else if (attr == &dev_attr_orientation.attr) {
1702 if (port->cap->orientation_aware)
1710 static const struct attribute_group typec_group = {
1711 .is_visible = typec_attr_is_visible,
1712 .attrs = typec_attrs,
1715 static const struct attribute_group *typec_groups[] = {
1721 static int typec_uevent(struct device *dev, struct kobj_uevent_env *env)
1725 ret = add_uevent_var(env, "TYPEC_PORT=%s", dev_name(dev));
1727 dev_err(dev, "failed to add uevent TYPEC_PORT\n");
1732 static void typec_release(struct device *dev)
1734 struct typec_port *port = to_typec_port(dev);
1736 ida_simple_remove(&typec_index_ida, port->id);
1737 ida_destroy(&port->mode_ids);
1738 typec_switch_put(port->sw);
1739 typec_mux_put(port->mux);
1740 typec_retimer_put(port->retimer);
1745 const struct device_type typec_port_dev_type = {
1746 .name = "typec_port",
1747 .groups = typec_groups,
1748 .uevent = typec_uevent,
1749 .release = typec_release,
1752 /* --------------------------------------- */
1753 /* Driver callbacks to report role updates */
1755 static int partner_match(struct device *dev, void *data)
1757 return is_typec_partner(dev);
1761 * typec_set_data_role - Report data role change
1762 * @port: The USB Type-C Port where the role was changed
1763 * @role: The new data role
1765 * This routine is used by the port drivers to report data role changes.
1767 void typec_set_data_role(struct typec_port *port, enum typec_data_role role)
1769 struct device *partner_dev;
1771 if (port->data_role == role)
1774 port->data_role = role;
1775 sysfs_notify(&port->dev.kobj, NULL, "data_role");
1776 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1778 partner_dev = device_find_child(&port->dev, NULL, partner_match);
1782 if (to_typec_partner(partner_dev)->identity)
1783 typec_product_type_notify(partner_dev);
1785 put_device(partner_dev);
1787 EXPORT_SYMBOL_GPL(typec_set_data_role);
1790 * typec_set_pwr_role - Report power role change
1791 * @port: The USB Type-C Port where the role was changed
1792 * @role: The new data role
1794 * This routine is used by the port drivers to report power role changes.
1796 void typec_set_pwr_role(struct typec_port *port, enum typec_role role)
1798 if (port->pwr_role == role)
1801 port->pwr_role = role;
1802 sysfs_notify(&port->dev.kobj, NULL, "power_role");
1803 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1805 EXPORT_SYMBOL_GPL(typec_set_pwr_role);
1808 * typec_set_vconn_role - Report VCONN source change
1809 * @port: The USB Type-C Port which VCONN role changed
1810 * @role: Source when @port is sourcing VCONN, or Sink when it's not
1812 * This routine is used by the port drivers to report if the VCONN source is
1815 void typec_set_vconn_role(struct typec_port *port, enum typec_role role)
1817 if (port->vconn_role == role)
1820 port->vconn_role = role;
1821 sysfs_notify(&port->dev.kobj, NULL, "vconn_source");
1822 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1824 EXPORT_SYMBOL_GPL(typec_set_vconn_role);
1827 * typec_set_pwr_opmode - Report changed power operation mode
1828 * @port: The USB Type-C Port where the mode was changed
1829 * @opmode: New power operation mode
1831 * This routine is used by the port drivers to report changed power operation
1832 * mode in @port. The modes are USB (default), 1.5A, 3.0A as defined in USB
1833 * Type-C specification, and "USB Power Delivery" when the power levels are
1834 * negotiated with methods defined in USB Power Delivery specification.
1836 void typec_set_pwr_opmode(struct typec_port *port,
1837 enum typec_pwr_opmode opmode)
1839 struct device *partner_dev;
1841 if (port->pwr_opmode == opmode)
1844 port->pwr_opmode = opmode;
1845 sysfs_notify(&port->dev.kobj, NULL, "power_operation_mode");
1846 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1848 partner_dev = device_find_child(&port->dev, NULL, partner_match);
1850 struct typec_partner *partner = to_typec_partner(partner_dev);
1852 if (opmode == TYPEC_PWR_MODE_PD && !partner->usb_pd) {
1853 partner->usb_pd = 1;
1854 sysfs_notify(&partner_dev->kobj, NULL,
1855 "supports_usb_power_delivery");
1856 kobject_uevent(&partner_dev->kobj, KOBJ_CHANGE);
1858 put_device(partner_dev);
1861 EXPORT_SYMBOL_GPL(typec_set_pwr_opmode);
1864 * typec_find_pwr_opmode - Get the typec power operation mode capability
1865 * @name: power operation mode string
1867 * This routine is used to find the typec_pwr_opmode by its string @name.
1869 * Returns typec_pwr_opmode if success, otherwise negative error code.
1871 int typec_find_pwr_opmode(const char *name)
1873 return match_string(typec_pwr_opmodes,
1874 ARRAY_SIZE(typec_pwr_opmodes), name);
1876 EXPORT_SYMBOL_GPL(typec_find_pwr_opmode);
1879 * typec_find_orientation - Convert orientation string to enum typec_orientation
1880 * @name: Orientation string
1882 * This routine is used to find the typec_orientation by its string name @name.
1884 * Returns the orientation value on success, otherwise negative error code.
1886 int typec_find_orientation(const char *name)
1888 return match_string(typec_orientations, ARRAY_SIZE(typec_orientations),
1891 EXPORT_SYMBOL_GPL(typec_find_orientation);
1894 * typec_find_port_power_role - Get the typec port power capability
1895 * @name: port power capability string
1897 * This routine is used to find the typec_port_type by its string name.
1899 * Returns typec_port_type if success, otherwise negative error code.
1901 int typec_find_port_power_role(const char *name)
1903 return match_string(typec_port_power_roles,
1904 ARRAY_SIZE(typec_port_power_roles), name);
1906 EXPORT_SYMBOL_GPL(typec_find_port_power_role);
1909 * typec_find_power_role - Find the typec one specific power role
1910 * @name: power role string
1912 * This routine is used to find the typec_role by its string name.
1914 * Returns typec_role if success, otherwise negative error code.
1916 int typec_find_power_role(const char *name)
1918 return match_string(typec_roles, ARRAY_SIZE(typec_roles), name);
1920 EXPORT_SYMBOL_GPL(typec_find_power_role);
1923 * typec_find_port_data_role - Get the typec port data capability
1924 * @name: port data capability string
1926 * This routine is used to find the typec_port_data by its string name.
1928 * Returns typec_port_data if success, otherwise negative error code.
1930 int typec_find_port_data_role(const char *name)
1932 return match_string(typec_port_data_roles,
1933 ARRAY_SIZE(typec_port_data_roles), name);
1935 EXPORT_SYMBOL_GPL(typec_find_port_data_role);
1937 /* ------------------------------------------ */
1938 /* API for Multiplexer/DeMultiplexer Switches */
1941 * typec_set_orientation - Set USB Type-C cable plug orientation
1942 * @port: USB Type-C Port
1943 * @orientation: USB Type-C cable plug orientation
1945 * Set cable plug orientation for @port.
1947 int typec_set_orientation(struct typec_port *port,
1948 enum typec_orientation orientation)
1952 ret = typec_switch_set(port->sw, orientation);
1956 port->orientation = orientation;
1957 sysfs_notify(&port->dev.kobj, NULL, "orientation");
1958 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1962 EXPORT_SYMBOL_GPL(typec_set_orientation);
1965 * typec_get_orientation - Get USB Type-C cable plug orientation
1966 * @port: USB Type-C Port
1968 * Get current cable plug orientation for @port.
1970 enum typec_orientation typec_get_orientation(struct typec_port *port)
1972 return port->orientation;
1974 EXPORT_SYMBOL_GPL(typec_get_orientation);
1977 * typec_set_mode - Set mode of operation for USB Type-C connector
1978 * @port: USB Type-C connector
1979 * @mode: Accessory Mode, USB Operation or Safe State
1981 * Configure @port for Accessory Mode @mode. This function will configure the
1982 * muxes needed for @mode.
1984 int typec_set_mode(struct typec_port *port, int mode)
1986 struct typec_mux_state state = { };
1990 return typec_mux_set(port->mux, &state);
1992 EXPORT_SYMBOL_GPL(typec_set_mode);
1994 /* --------------------------------------- */
1997 * typec_get_negotiated_svdm_version - Get negotiated SVDM Version
1998 * @port: USB Type-C Port.
2000 * Get the negotiated SVDM Version. The Version is set to the port default
2001 * value stored in typec_capability on partner registration, and updated after
2002 * a successful Discover Identity if the negotiated value is less than the
2005 * Returns usb_pd_svdm_ver if the partner has been registered otherwise -ENODEV.
2007 int typec_get_negotiated_svdm_version(struct typec_port *port)
2009 enum usb_pd_svdm_ver svdm_version;
2010 struct device *partner_dev;
2012 partner_dev = device_find_child(&port->dev, NULL, partner_match);
2016 svdm_version = to_typec_partner(partner_dev)->svdm_version;
2017 put_device(partner_dev);
2019 return svdm_version;
2021 EXPORT_SYMBOL_GPL(typec_get_negotiated_svdm_version);
2024 * typec_get_drvdata - Return private driver data pointer
2025 * @port: USB Type-C port
2027 void *typec_get_drvdata(struct typec_port *port)
2029 return dev_get_drvdata(&port->dev);
2031 EXPORT_SYMBOL_GPL(typec_get_drvdata);
2033 int typec_get_fw_cap(struct typec_capability *cap,
2034 struct fwnode_handle *fwnode)
2036 const char *cap_str;
2039 cap->fwnode = fwnode;
2041 ret = fwnode_property_read_string(fwnode, "power-role", &cap_str);
2045 ret = typec_find_port_power_role(cap_str);
2050 /* USB data support is optional */
2051 ret = fwnode_property_read_string(fwnode, "data-role", &cap_str);
2053 ret = typec_find_port_data_role(cap_str);
2059 /* Get the preferred power role for a DRP */
2060 if (cap->type == TYPEC_PORT_DRP) {
2061 cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
2063 ret = fwnode_property_read_string(fwnode, "try-power-role", &cap_str);
2065 ret = typec_find_power_role(cap_str);
2068 cap->prefer_role = ret;
2074 EXPORT_SYMBOL_GPL(typec_get_fw_cap);
2077 * typec_port_register_altmode - Register USB Type-C Port Alternate Mode
2078 * @port: USB Type-C Port that supports the alternate mode
2079 * @desc: Description of the alternate mode
2081 * This routine is used to register an alternate mode that @port is capable of
2084 * Returns handle to the alternate mode on success or ERR_PTR on failure.
2086 struct typec_altmode *
2087 typec_port_register_altmode(struct typec_port *port,
2088 const struct typec_altmode_desc *desc)
2090 struct typec_altmode *adev;
2091 struct typec_mux *mux;
2093 mux = typec_mux_get(&port->dev, desc);
2095 return ERR_CAST(mux);
2097 adev = typec_register_altmode(&port->dev, desc);
2101 to_altmode(adev)->mux = mux;
2105 EXPORT_SYMBOL_GPL(typec_port_register_altmode);
2107 void typec_port_register_altmodes(struct typec_port *port,
2108 const struct typec_altmode_ops *ops, void *drvdata,
2109 struct typec_altmode **altmodes, size_t n)
2111 struct fwnode_handle *altmodes_node, *child;
2112 struct typec_altmode_desc desc;
2113 struct typec_altmode *alt;
2118 altmodes_node = device_get_named_child_node(&port->dev, "altmodes");
2120 return; /* No altmodes specified */
2122 fwnode_for_each_child_node(altmodes_node, child) {
2123 ret = fwnode_property_read_u32(child, "svid", &svid);
2125 dev_err(&port->dev, "Error reading svid for altmode %s\n",
2126 fwnode_get_name(child));
2130 ret = fwnode_property_read_u32(child, "vdo", &vdo);
2132 dev_err(&port->dev, "Error reading vdo for altmode %s\n",
2133 fwnode_get_name(child));
2138 dev_err(&port->dev, "Error not enough space for altmode %s\n",
2139 fwnode_get_name(child));
2145 desc.mode = index + 1;
2146 alt = typec_port_register_altmode(port, &desc);
2148 dev_err(&port->dev, "Error registering altmode %s\n",
2149 fwnode_get_name(child));
2154 typec_altmode_set_drvdata(alt, drvdata);
2155 altmodes[index] = alt;
2159 EXPORT_SYMBOL_GPL(typec_port_register_altmodes);
2162 * typec_register_port - Register a USB Type-C Port
2163 * @parent: Parent device
2164 * @cap: Description of the port
2166 * Registers a device for USB Type-C Port described in @cap.
2168 * Returns handle to the port on success or ERR_PTR on failure.
2170 struct typec_port *typec_register_port(struct device *parent,
2171 const struct typec_capability *cap)
2173 struct typec_port *port;
2177 port = kzalloc(sizeof(*port), GFP_KERNEL);
2179 return ERR_PTR(-ENOMEM);
2181 id = ida_simple_get(&typec_index_ida, 0, 0, GFP_KERNEL);
2187 switch (cap->type) {
2188 case TYPEC_PORT_SRC:
2189 port->pwr_role = TYPEC_SOURCE;
2190 port->vconn_role = TYPEC_SOURCE;
2192 case TYPEC_PORT_SNK:
2193 port->pwr_role = TYPEC_SINK;
2194 port->vconn_role = TYPEC_SINK;
2196 case TYPEC_PORT_DRP:
2197 if (cap->prefer_role != TYPEC_NO_PREFERRED_ROLE)
2198 port->pwr_role = cap->prefer_role;
2200 port->pwr_role = TYPEC_SINK;
2204 switch (cap->data) {
2205 case TYPEC_PORT_DFP:
2206 port->data_role = TYPEC_HOST;
2208 case TYPEC_PORT_UFP:
2209 port->data_role = TYPEC_DEVICE;
2211 case TYPEC_PORT_DRD:
2212 if (cap->prefer_role == TYPEC_SOURCE)
2213 port->data_role = TYPEC_HOST;
2215 port->data_role = TYPEC_DEVICE;
2219 ida_init(&port->mode_ids);
2220 mutex_init(&port->port_type_lock);
2223 port->ops = cap->ops;
2224 port->port_type = cap->type;
2225 port->prefer_role = cap->prefer_role;
2227 device_initialize(&port->dev);
2228 port->dev.class = &typec_class;
2229 port->dev.parent = parent;
2230 port->dev.fwnode = cap->fwnode;
2231 port->dev.type = &typec_port_dev_type;
2232 dev_set_name(&port->dev, "port%d", id);
2233 dev_set_drvdata(&port->dev, cap->driver_data);
2235 port->cap = kmemdup(cap, sizeof(*cap), GFP_KERNEL);
2237 put_device(&port->dev);
2238 return ERR_PTR(-ENOMEM);
2241 port->sw = typec_switch_get(&port->dev);
2242 if (IS_ERR(port->sw)) {
2243 ret = PTR_ERR(port->sw);
2244 put_device(&port->dev);
2245 return ERR_PTR(ret);
2248 port->mux = typec_mux_get(&port->dev, NULL);
2249 if (IS_ERR(port->mux)) {
2250 ret = PTR_ERR(port->mux);
2251 put_device(&port->dev);
2252 return ERR_PTR(ret);
2255 port->retimer = typec_retimer_get(&port->dev);
2256 if (IS_ERR(port->retimer)) {
2257 ret = PTR_ERR(port->retimer);
2258 put_device(&port->dev);
2259 return ERR_PTR(ret);
2262 ret = device_add(&port->dev);
2264 dev_err(parent, "failed to register port (%d)\n", ret);
2265 put_device(&port->dev);
2266 return ERR_PTR(ret);
2269 ret = typec_port_set_usb_power_delivery(port, cap->pd);
2271 dev_err(&port->dev, "failed to link pd\n");
2272 device_unregister(&port->dev);
2273 return ERR_PTR(ret);
2276 ret = typec_link_ports(port);
2278 dev_warn(&port->dev, "failed to create symlinks (%d)\n", ret);
2282 EXPORT_SYMBOL_GPL(typec_register_port);
2285 * typec_unregister_port - Unregister a USB Type-C Port
2286 * @port: The port to be unregistered
2288 * Unregister device created with typec_register_port().
2290 void typec_unregister_port(struct typec_port *port)
2292 if (!IS_ERR_OR_NULL(port)) {
2293 typec_unlink_ports(port);
2294 typec_port_set_usb_power_delivery(port, NULL);
2295 device_unregister(&port->dev);
2298 EXPORT_SYMBOL_GPL(typec_unregister_port);
2300 static int __init typec_init(void)
2304 ret = bus_register(&typec_bus);
2308 ret = class_register(&typec_mux_class);
2310 goto err_unregister_bus;
2312 ret = class_register(&retimer_class);
2314 goto err_unregister_mux_class;
2316 ret = class_register(&typec_class);
2318 goto err_unregister_retimer_class;
2320 ret = usb_power_delivery_init();
2322 goto err_unregister_class;
2326 err_unregister_class:
2327 class_unregister(&typec_class);
2329 err_unregister_retimer_class:
2330 class_unregister(&retimer_class);
2332 err_unregister_mux_class:
2333 class_unregister(&typec_mux_class);
2336 bus_unregister(&typec_bus);
2340 subsys_initcall(typec_init);
2342 static void __exit typec_exit(void)
2344 usb_power_delivery_exit();
2345 class_unregister(&typec_class);
2346 ida_destroy(&typec_index_ida);
2347 bus_unregister(&typec_bus);
2348 class_unregister(&typec_mux_class);
2349 class_unregister(&retimer_class);
2351 module_exit(typec_exit);
2353 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
2354 MODULE_LICENSE("GPL v2");
2355 MODULE_DESCRIPTION("USB Type-C Connector Class");