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>
16 #include <linux/usb.h>
22 static DEFINE_IDA(typec_index_ida);
24 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;
266 struct typec_altmode *partner_adev;
271 adev = &altmode->adev;
272 partner_adev = &partner->adev;
274 if (is_typec_plug(adev->dev.parent)) {
275 struct typec_plug *plug = to_typec_plug(adev->dev.parent);
277 partner->plug[plug->index] = NULL;
279 partner->partner = NULL;
281 put_device(&partner_adev->dev);
285 * typec_altmode_update_active - Report Enter/Exit mode
286 * @adev: Handle to the alternate mode
287 * @active: True when the mode has been entered
289 * If a partner or cable plug executes Enter/Exit Mode command successfully, the
290 * drivers use this routine to report the updated state of the mode.
292 void typec_altmode_update_active(struct typec_altmode *adev, bool active)
296 if (adev->active == active)
299 if (!is_typec_port(adev->dev.parent) && adev->dev.driver) {
301 module_put(adev->dev.driver->owner);
303 WARN_ON(!try_module_get(adev->dev.driver->owner));
306 adev->active = active;
307 snprintf(dir, sizeof(dir), "mode%d", adev->mode);
308 sysfs_notify(&adev->dev.kobj, dir, "active");
309 sysfs_notify(&adev->dev.kobj, NULL, "active");
310 kobject_uevent(&adev->dev.kobj, KOBJ_CHANGE);
312 EXPORT_SYMBOL_GPL(typec_altmode_update_active);
315 * typec_altmode2port - Alternate Mode to USB Type-C port
316 * @alt: The Alternate Mode
318 * Returns handle to the port that a cable plug or partner with @alt is
321 struct typec_port *typec_altmode2port(struct typec_altmode *alt)
323 if (is_typec_plug(alt->dev.parent))
324 return to_typec_port(alt->dev.parent->parent->parent);
325 if (is_typec_partner(alt->dev.parent))
326 return to_typec_port(alt->dev.parent->parent);
327 if (is_typec_port(alt->dev.parent))
328 return to_typec_port(alt->dev.parent);
332 EXPORT_SYMBOL_GPL(typec_altmode2port);
335 vdo_show(struct device *dev, struct device_attribute *attr, char *buf)
337 struct typec_altmode *alt = to_typec_altmode(dev);
339 return sprintf(buf, "0x%08x\n", alt->vdo);
341 static DEVICE_ATTR_RO(vdo);
344 description_show(struct device *dev, struct device_attribute *attr, char *buf)
346 struct typec_altmode *alt = to_typec_altmode(dev);
348 return sprintf(buf, "%s\n", alt->desc ? alt->desc : "");
350 static DEVICE_ATTR_RO(description);
353 active_show(struct device *dev, struct device_attribute *attr, char *buf)
355 struct typec_altmode *alt = to_typec_altmode(dev);
357 return sprintf(buf, "%s\n", alt->active ? "yes" : "no");
360 static ssize_t active_store(struct device *dev, struct device_attribute *attr,
361 const char *buf, size_t size)
363 struct typec_altmode *adev = to_typec_altmode(dev);
364 struct altmode *altmode = to_altmode(adev);
368 ret = kstrtobool(buf, &enter);
372 if (adev->active == enter)
375 if (is_typec_port(adev->dev.parent)) {
376 typec_altmode_update_active(adev, enter);
378 /* Make sure that the partner exits the mode before disabling */
379 if (altmode->partner && !enter && altmode->partner->adev.active)
380 typec_altmode_exit(&altmode->partner->adev);
381 } else if (altmode->partner) {
382 if (enter && !altmode->partner->adev.active) {
383 dev_warn(dev, "port has the mode disabled\n");
388 /* Note: If there is no driver, the mode will not be entered */
389 if (adev->ops && adev->ops->activate) {
390 ret = adev->ops->activate(adev, enter);
397 static DEVICE_ATTR_RW(active);
400 supported_roles_show(struct device *dev, struct device_attribute *attr,
403 struct altmode *alt = to_altmode(to_typec_altmode(dev));
406 switch (alt->roles) {
408 ret = sprintf(buf, "source\n");
411 ret = sprintf(buf, "sink\n");
415 ret = sprintf(buf, "source sink\n");
420 static DEVICE_ATTR_RO(supported_roles);
423 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
425 struct typec_altmode *adev = to_typec_altmode(dev);
427 return sprintf(buf, "%u\n", adev->mode);
429 static DEVICE_ATTR_RO(mode);
432 svid_show(struct device *dev, struct device_attribute *attr, char *buf)
434 struct typec_altmode *adev = to_typec_altmode(dev);
436 return sprintf(buf, "%04x\n", adev->svid);
438 static DEVICE_ATTR_RO(svid);
440 static struct attribute *typec_altmode_attrs[] = {
441 &dev_attr_active.attr,
448 static umode_t typec_altmode_attr_is_visible(struct kobject *kobj,
449 struct attribute *attr, int n)
451 struct typec_altmode *adev = to_typec_altmode(kobj_to_dev(kobj));
453 if (attr == &dev_attr_active.attr)
454 if (!adev->ops || !adev->ops->activate)
460 static const struct attribute_group typec_altmode_group = {
461 .is_visible = typec_altmode_attr_is_visible,
462 .attrs = typec_altmode_attrs,
465 static const struct attribute_group *typec_altmode_groups[] = {
466 &typec_altmode_group,
470 static int altmode_id_get(struct device *dev)
474 if (is_typec_partner(dev))
475 ids = &to_typec_partner(dev)->mode_ids;
476 else if (is_typec_plug(dev))
477 ids = &to_typec_plug(dev)->mode_ids;
479 ids = &to_typec_port(dev)->mode_ids;
481 return ida_simple_get(ids, 0, 0, GFP_KERNEL);
484 static void altmode_id_remove(struct device *dev, int id)
488 if (is_typec_partner(dev))
489 ids = &to_typec_partner(dev)->mode_ids;
490 else if (is_typec_plug(dev))
491 ids = &to_typec_plug(dev)->mode_ids;
493 ids = &to_typec_port(dev)->mode_ids;
495 ida_simple_remove(ids, id);
498 static void typec_altmode_release(struct device *dev)
500 struct altmode *alt = to_altmode(to_typec_altmode(dev));
502 if (!is_typec_port(dev->parent))
503 typec_altmode_put_partner(alt);
505 altmode_id_remove(alt->adev.dev.parent, alt->id);
509 const struct device_type typec_altmode_dev_type = {
510 .name = "typec_alternate_mode",
511 .groups = typec_altmode_groups,
512 .release = typec_altmode_release,
515 static struct typec_altmode *
516 typec_register_altmode(struct device *parent,
517 const struct typec_altmode_desc *desc)
519 unsigned int id = altmode_id_get(parent);
520 bool is_port = is_typec_port(parent);
524 alt = kzalloc(sizeof(*alt), GFP_KERNEL);
526 altmode_id_remove(parent, id);
527 return ERR_PTR(-ENOMEM);
530 alt->adev.svid = desc->svid;
531 alt->adev.mode = desc->mode;
532 alt->adev.vdo = desc->vdo;
533 alt->roles = desc->roles;
536 alt->attrs[0] = &dev_attr_vdo.attr;
537 alt->attrs[1] = &dev_attr_description.attr;
538 alt->attrs[2] = &dev_attr_active.attr;
541 alt->attrs[3] = &dev_attr_supported_roles.attr;
542 alt->adev.active = true; /* Enabled by default */
545 sprintf(alt->group_name, "mode%d", desc->mode);
546 alt->group.name = alt->group_name;
547 alt->group.attrs = alt->attrs;
548 alt->groups[0] = &alt->group;
550 alt->adev.dev.parent = parent;
551 alt->adev.dev.groups = alt->groups;
552 alt->adev.dev.type = &typec_altmode_dev_type;
553 dev_set_name(&alt->adev.dev, "%s.%u", dev_name(parent), id);
555 /* Link partners and plugs with the ports */
557 typec_altmode_set_partner(alt);
559 /* The partners are bind to drivers */
560 if (is_typec_partner(parent))
561 alt->adev.dev.bus = &typec_bus;
563 /* Plug alt modes need a class to generate udev events. */
564 if (is_typec_plug(parent))
565 alt->adev.dev.class = &typec_class;
567 ret = device_register(&alt->adev.dev);
569 dev_err(parent, "failed to register alternate mode (%d)\n",
571 put_device(&alt->adev.dev);
579 * typec_unregister_altmode - Unregister Alternate Mode
580 * @adev: The alternate mode to be unregistered
582 * Unregister device created with typec_partner_register_altmode(),
583 * typec_plug_register_altmode() or typec_port_register_altmode().
585 void typec_unregister_altmode(struct typec_altmode *adev)
587 if (IS_ERR_OR_NULL(adev))
589 typec_retimer_put(to_altmode(adev)->retimer);
590 typec_mux_put(to_altmode(adev)->mux);
591 device_unregister(&adev->dev);
593 EXPORT_SYMBOL_GPL(typec_unregister_altmode);
595 /* ------------------------------------------------------------------------- */
596 /* Type-C Partners */
598 static ssize_t accessory_mode_show(struct device *dev,
599 struct device_attribute *attr,
602 struct typec_partner *p = to_typec_partner(dev);
604 return sprintf(buf, "%s\n", typec_accessory_modes[p->accessory]);
606 static DEVICE_ATTR_RO(accessory_mode);
608 static ssize_t supports_usb_power_delivery_show(struct device *dev,
609 struct device_attribute *attr,
612 struct typec_partner *p = to_typec_partner(dev);
614 return sprintf(buf, "%s\n", p->usb_pd ? "yes" : "no");
616 static DEVICE_ATTR_RO(supports_usb_power_delivery);
618 static ssize_t number_of_alternate_modes_show(struct device *dev, struct device_attribute *attr,
621 struct typec_partner *partner;
622 struct typec_plug *plug;
625 if (is_typec_partner(dev)) {
626 partner = to_typec_partner(dev);
627 num_altmodes = partner->num_altmodes;
628 } else if (is_typec_plug(dev)) {
629 plug = to_typec_plug(dev);
630 num_altmodes = plug->num_altmodes;
635 return sysfs_emit(buf, "%d\n", num_altmodes);
637 static DEVICE_ATTR_RO(number_of_alternate_modes);
639 static struct attribute *typec_partner_attrs[] = {
640 &dev_attr_accessory_mode.attr,
641 &dev_attr_supports_usb_power_delivery.attr,
642 &dev_attr_number_of_alternate_modes.attr,
644 &dev_attr_usb_power_delivery_revision.attr,
648 static umode_t typec_partner_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
650 struct typec_partner *partner = to_typec_partner(kobj_to_dev(kobj));
652 if (attr == &dev_attr_number_of_alternate_modes.attr) {
653 if (partner->num_altmodes < 0)
657 if (attr == &dev_attr_type.attr)
658 if (!get_pd_product_type(kobj_to_dev(kobj)))
664 static const struct attribute_group typec_partner_group = {
665 .is_visible = typec_partner_attr_is_visible,
666 .attrs = typec_partner_attrs
669 static const struct attribute_group *typec_partner_groups[] = {
670 &typec_partner_group,
674 static void typec_partner_release(struct device *dev)
676 struct typec_partner *partner = to_typec_partner(dev);
678 ida_destroy(&partner->mode_ids);
682 const struct device_type typec_partner_dev_type = {
683 .name = "typec_partner",
684 .groups = typec_partner_groups,
685 .release = typec_partner_release,
688 static void typec_partner_link_device(struct typec_partner *partner, struct device *dev)
692 ret = sysfs_create_link(&dev->kobj, &partner->dev.kobj, "typec");
696 ret = sysfs_create_link(&partner->dev.kobj, &dev->kobj, dev_name(dev));
698 sysfs_remove_link(&dev->kobj, "typec");
703 partner->attach(partner, dev);
706 static void typec_partner_unlink_device(struct typec_partner *partner, struct device *dev)
708 sysfs_remove_link(&partner->dev.kobj, dev_name(dev));
709 sysfs_remove_link(&dev->kobj, "typec");
711 if (partner->deattach)
712 partner->deattach(partner, dev);
716 * typec_partner_set_identity - Report result from Discover Identity command
717 * @partner: The partner updated identity values
719 * This routine is used to report that the result of Discover Identity USB power
720 * delivery command has become available.
722 int typec_partner_set_identity(struct typec_partner *partner)
724 if (!partner->identity)
727 typec_report_identity(&partner->dev);
730 EXPORT_SYMBOL_GPL(typec_partner_set_identity);
733 * typec_partner_set_pd_revision - Set the PD revision supported by the partner
734 * @partner: The partner to be updated.
735 * @pd_revision: USB Power Delivery Specification Revision supported by partner
737 * This routine is used to report that the PD revision of the port partner has
740 void typec_partner_set_pd_revision(struct typec_partner *partner, u16 pd_revision)
742 if (partner->pd_revision == pd_revision)
745 partner->pd_revision = pd_revision;
746 sysfs_notify(&partner->dev.kobj, NULL, "usb_power_delivery_revision");
747 if (pd_revision != 0 && !partner->usb_pd) {
749 sysfs_notify(&partner->dev.kobj, NULL,
750 "supports_usb_power_delivery");
752 kobject_uevent(&partner->dev.kobj, KOBJ_CHANGE);
754 EXPORT_SYMBOL_GPL(typec_partner_set_pd_revision);
757 * typec_partner_set_usb_power_delivery - Declare USB Power Delivery Contract.
758 * @partner: The partner device.
759 * @pd: The USB PD instance.
761 * This routine can be used to declare USB Power Delivery Contract with @partner
762 * by linking @partner to @pd which contains the objects that were used during the
763 * negotiation of the contract.
765 * If @pd is NULL, the link is removed and the contract with @partner has ended.
767 int typec_partner_set_usb_power_delivery(struct typec_partner *partner,
768 struct usb_power_delivery *pd)
772 if (IS_ERR_OR_NULL(partner) || partner->pd == pd)
776 ret = usb_power_delivery_link_device(pd, &partner->dev);
780 usb_power_delivery_unlink_device(partner->pd, &partner->dev);
787 EXPORT_SYMBOL_GPL(typec_partner_set_usb_power_delivery);
790 * typec_partner_set_num_altmodes - Set the number of available partner altmodes
791 * @partner: The partner to be updated.
792 * @num_altmodes: The number of altmodes we want to specify as available.
794 * This routine is used to report the number of alternate modes supported by the
795 * partner. This value is *not* enforced in alternate mode registration routines.
797 * @partner.num_altmodes is set to -1 on partner registration, denoting that
798 * a valid value has not been set for it yet.
800 * Returns 0 on success or negative error number on failure.
802 int typec_partner_set_num_altmodes(struct typec_partner *partner, int num_altmodes)
806 if (num_altmodes < 0)
809 partner->num_altmodes = num_altmodes;
810 ret = sysfs_update_group(&partner->dev.kobj, &typec_partner_group);
814 sysfs_notify(&partner->dev.kobj, NULL, "number_of_alternate_modes");
815 kobject_uevent(&partner->dev.kobj, KOBJ_CHANGE);
819 EXPORT_SYMBOL_GPL(typec_partner_set_num_altmodes);
822 * typec_partner_register_altmode - Register USB Type-C Partner Alternate Mode
823 * @partner: USB Type-C Partner that supports the alternate mode
824 * @desc: Description of the alternate mode
826 * This routine is used to register each alternate mode individually that
827 * @partner has listed in response to Discover SVIDs command. The modes for a
828 * SVID listed in response to Discover Modes command need to be listed in an
831 * Returns handle to the alternate mode on success or ERR_PTR on failure.
833 struct typec_altmode *
834 typec_partner_register_altmode(struct typec_partner *partner,
835 const struct typec_altmode_desc *desc)
837 return typec_register_altmode(&partner->dev, desc);
839 EXPORT_SYMBOL_GPL(typec_partner_register_altmode);
842 * typec_partner_set_svdm_version - Set negotiated Structured VDM (SVDM) Version
843 * @partner: USB Type-C Partner that supports SVDM
844 * @svdm_version: Negotiated SVDM Version
846 * This routine is used to save the negotiated SVDM Version.
848 void typec_partner_set_svdm_version(struct typec_partner *partner,
849 enum usb_pd_svdm_ver svdm_version)
851 partner->svdm_version = svdm_version;
853 EXPORT_SYMBOL_GPL(typec_partner_set_svdm_version);
856 * typec_partner_usb_power_delivery_register - Register Type-C partner USB Power Delivery Support
857 * @partner: Type-C partner device.
858 * @desc: Description of the USB PD contract.
860 * This routine is a wrapper around usb_power_delivery_register(). It registers
861 * USB Power Delivery Capabilities for a Type-C partner device. Specifically,
862 * it sets the Type-C partner device as a parent for the resulting USB Power Delivery object.
864 * Returns handle to struct usb_power_delivery or ERR_PTR.
866 struct usb_power_delivery *
867 typec_partner_usb_power_delivery_register(struct typec_partner *partner,
868 struct usb_power_delivery_desc *desc)
870 return usb_power_delivery_register(&partner->dev, desc);
872 EXPORT_SYMBOL_GPL(typec_partner_usb_power_delivery_register);
875 * typec_register_partner - Register a USB Type-C Partner
876 * @port: The USB Type-C Port the partner is connected to
877 * @desc: Description of the partner
879 * Registers a device for USB Type-C Partner described in @desc.
881 * Returns handle to the partner on success or ERR_PTR on failure.
883 struct typec_partner *typec_register_partner(struct typec_port *port,
884 struct typec_partner_desc *desc)
886 struct typec_partner *partner;
889 partner = kzalloc(sizeof(*partner), GFP_KERNEL);
891 return ERR_PTR(-ENOMEM);
893 ida_init(&partner->mode_ids);
894 partner->usb_pd = desc->usb_pd;
895 partner->accessory = desc->accessory;
896 partner->num_altmodes = -1;
897 partner->pd_revision = desc->pd_revision;
898 partner->svdm_version = port->cap->svdm_version;
899 partner->attach = desc->attach;
900 partner->deattach = desc->deattach;
902 if (desc->identity) {
904 * Creating directory for the identity only if the driver is
905 * able to provide data to it.
907 partner->dev.groups = usb_pd_id_groups;
908 partner->identity = desc->identity;
911 partner->dev.class = &typec_class;
912 partner->dev.parent = &port->dev;
913 partner->dev.type = &typec_partner_dev_type;
914 dev_set_name(&partner->dev, "%s-partner", dev_name(&port->dev));
916 ret = device_register(&partner->dev);
918 dev_err(&port->dev, "failed to register partner (%d)\n", ret);
919 put_device(&partner->dev);
924 typec_partner_link_device(partner, port->usb2_dev);
926 typec_partner_link_device(partner, port->usb3_dev);
930 EXPORT_SYMBOL_GPL(typec_register_partner);
933 * typec_unregister_partner - Unregister a USB Type-C Partner
934 * @partner: The partner to be unregistered
936 * Unregister device created with typec_register_partner().
938 void typec_unregister_partner(struct typec_partner *partner)
940 struct typec_port *port;
942 if (IS_ERR_OR_NULL(partner))
945 port = to_typec_port(partner->dev.parent);
948 typec_partner_unlink_device(partner, port->usb2_dev);
950 typec_partner_unlink_device(partner, port->usb3_dev);
952 device_unregister(&partner->dev);
954 EXPORT_SYMBOL_GPL(typec_unregister_partner);
956 /* ------------------------------------------------------------------------- */
957 /* Type-C Cable Plugs */
959 static void typec_plug_release(struct device *dev)
961 struct typec_plug *plug = to_typec_plug(dev);
963 ida_destroy(&plug->mode_ids);
967 static struct attribute *typec_plug_attrs[] = {
968 &dev_attr_number_of_alternate_modes.attr,
972 static umode_t typec_plug_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
974 struct typec_plug *plug = to_typec_plug(kobj_to_dev(kobj));
976 if (attr == &dev_attr_number_of_alternate_modes.attr) {
977 if (plug->num_altmodes < 0)
984 static const struct attribute_group typec_plug_group = {
985 .is_visible = typec_plug_attr_is_visible,
986 .attrs = typec_plug_attrs
989 static const struct attribute_group *typec_plug_groups[] = {
994 const struct device_type typec_plug_dev_type = {
995 .name = "typec_plug",
996 .groups = typec_plug_groups,
997 .release = typec_plug_release,
1001 * typec_plug_set_num_altmodes - Set the number of available plug altmodes
1002 * @plug: The plug to be updated.
1003 * @num_altmodes: The number of altmodes we want to specify as available.
1005 * This routine is used to report the number of alternate modes supported by the
1006 * plug. This value is *not* enforced in alternate mode registration routines.
1008 * @plug.num_altmodes is set to -1 on plug registration, denoting that
1009 * a valid value has not been set for it yet.
1011 * Returns 0 on success or negative error number on failure.
1013 int typec_plug_set_num_altmodes(struct typec_plug *plug, int num_altmodes)
1017 if (num_altmodes < 0)
1020 plug->num_altmodes = num_altmodes;
1021 ret = sysfs_update_group(&plug->dev.kobj, &typec_plug_group);
1025 sysfs_notify(&plug->dev.kobj, NULL, "number_of_alternate_modes");
1026 kobject_uevent(&plug->dev.kobj, KOBJ_CHANGE);
1030 EXPORT_SYMBOL_GPL(typec_plug_set_num_altmodes);
1033 * typec_plug_register_altmode - Register USB Type-C Cable Plug Alternate Mode
1034 * @plug: USB Type-C Cable Plug that supports the alternate mode
1035 * @desc: Description of the alternate mode
1037 * This routine is used to register each alternate mode individually that @plug
1038 * has listed in response to Discover SVIDs command. The modes for a SVID that
1039 * the plug lists in response to Discover Modes command need to be listed in an
1042 * Returns handle to the alternate mode on success or ERR_PTR on failure.
1044 struct typec_altmode *
1045 typec_plug_register_altmode(struct typec_plug *plug,
1046 const struct typec_altmode_desc *desc)
1048 return typec_register_altmode(&plug->dev, desc);
1050 EXPORT_SYMBOL_GPL(typec_plug_register_altmode);
1053 * typec_register_plug - Register a USB Type-C Cable Plug
1054 * @cable: USB Type-C Cable with the plug
1055 * @desc: Description of the cable plug
1057 * Registers a device for USB Type-C Cable Plug described in @desc. A USB Type-C
1058 * Cable Plug represents a plug with electronics in it that can response to USB
1059 * Power Delivery SOP Prime or SOP Double Prime packages.
1061 * Returns handle to the cable plug on success or ERR_PTR on failure.
1063 struct typec_plug *typec_register_plug(struct typec_cable *cable,
1064 struct typec_plug_desc *desc)
1066 struct typec_plug *plug;
1070 plug = kzalloc(sizeof(*plug), GFP_KERNEL);
1072 return ERR_PTR(-ENOMEM);
1074 sprintf(name, "plug%d", desc->index);
1076 ida_init(&plug->mode_ids);
1077 plug->num_altmodes = -1;
1078 plug->index = desc->index;
1079 plug->dev.class = &typec_class;
1080 plug->dev.parent = &cable->dev;
1081 plug->dev.type = &typec_plug_dev_type;
1082 dev_set_name(&plug->dev, "%s-%s", dev_name(cable->dev.parent), name);
1084 ret = device_register(&plug->dev);
1086 dev_err(&cable->dev, "failed to register plug (%d)\n", ret);
1087 put_device(&plug->dev);
1088 return ERR_PTR(ret);
1093 EXPORT_SYMBOL_GPL(typec_register_plug);
1096 * typec_unregister_plug - Unregister a USB Type-C Cable Plug
1097 * @plug: The cable plug to be unregistered
1099 * Unregister device created with typec_register_plug().
1101 void typec_unregister_plug(struct typec_plug *plug)
1103 if (!IS_ERR_OR_NULL(plug))
1104 device_unregister(&plug->dev);
1106 EXPORT_SYMBOL_GPL(typec_unregister_plug);
1110 static const char * const typec_plug_types[] = {
1111 [USB_PLUG_NONE] = "unknown",
1112 [USB_PLUG_TYPE_A] = "type-a",
1113 [USB_PLUG_TYPE_B] = "type-b",
1114 [USB_PLUG_TYPE_C] = "type-c",
1115 [USB_PLUG_CAPTIVE] = "captive",
1118 static ssize_t plug_type_show(struct device *dev,
1119 struct device_attribute *attr, char *buf)
1121 struct typec_cable *cable = to_typec_cable(dev);
1123 return sprintf(buf, "%s\n", typec_plug_types[cable->type]);
1125 static DEVICE_ATTR_RO(plug_type);
1127 static struct attribute *typec_cable_attrs[] = {
1128 &dev_attr_type.attr,
1129 &dev_attr_plug_type.attr,
1130 &dev_attr_usb_power_delivery_revision.attr,
1133 ATTRIBUTE_GROUPS(typec_cable);
1135 static void typec_cable_release(struct device *dev)
1137 struct typec_cable *cable = to_typec_cable(dev);
1142 const struct device_type typec_cable_dev_type = {
1143 .name = "typec_cable",
1144 .groups = typec_cable_groups,
1145 .release = typec_cable_release,
1148 static int cable_match(struct device *dev, void *data)
1150 return is_typec_cable(dev);
1154 * typec_cable_get - Get a reference to the USB Type-C cable
1155 * @port: The USB Type-C Port the cable is connected to
1157 * The caller must decrement the reference count with typec_cable_put() after
1160 struct typec_cable *typec_cable_get(struct typec_port *port)
1164 dev = device_find_child(&port->dev, NULL, cable_match);
1168 return to_typec_cable(dev);
1170 EXPORT_SYMBOL_GPL(typec_cable_get);
1173 * typec_cable_put - Decrement the reference count on USB Type-C cable
1174 * @cable: The USB Type-C cable
1176 void typec_cable_put(struct typec_cable *cable)
1178 put_device(&cable->dev);
1180 EXPORT_SYMBOL_GPL(typec_cable_put);
1183 * typec_cable_is_active - Check is the USB Type-C cable active or passive
1184 * @cable: The USB Type-C Cable
1186 * Return 1 if the cable is active or 0 if it's passive.
1188 int typec_cable_is_active(struct typec_cable *cable)
1190 return cable->active;
1192 EXPORT_SYMBOL_GPL(typec_cable_is_active);
1195 * typec_cable_set_identity - Report result from Discover Identity command
1196 * @cable: The cable updated identity values
1198 * This routine is used to report that the result of Discover Identity USB power
1199 * delivery command has become available.
1201 int typec_cable_set_identity(struct typec_cable *cable)
1203 if (!cable->identity)
1206 typec_report_identity(&cable->dev);
1209 EXPORT_SYMBOL_GPL(typec_cable_set_identity);
1212 * typec_register_cable - Register a USB Type-C Cable
1213 * @port: The USB Type-C Port the cable is connected to
1214 * @desc: Description of the cable
1216 * Registers a device for USB Type-C Cable described in @desc. The cable will be
1217 * parent for the optional cable plug devises.
1219 * Returns handle to the cable on success or ERR_PTR on failure.
1221 struct typec_cable *typec_register_cable(struct typec_port *port,
1222 struct typec_cable_desc *desc)
1224 struct typec_cable *cable;
1227 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
1229 return ERR_PTR(-ENOMEM);
1231 cable->type = desc->type;
1232 cable->active = desc->active;
1233 cable->pd_revision = desc->pd_revision;
1235 if (desc->identity) {
1237 * Creating directory for the identity only if the driver is
1238 * able to provide data to it.
1240 cable->dev.groups = usb_pd_id_groups;
1241 cable->identity = desc->identity;
1244 cable->dev.class = &typec_class;
1245 cable->dev.parent = &port->dev;
1246 cable->dev.type = &typec_cable_dev_type;
1247 dev_set_name(&cable->dev, "%s-cable", dev_name(&port->dev));
1249 ret = device_register(&cable->dev);
1251 dev_err(&port->dev, "failed to register cable (%d)\n", ret);
1252 put_device(&cable->dev);
1253 return ERR_PTR(ret);
1258 EXPORT_SYMBOL_GPL(typec_register_cable);
1261 * typec_unregister_cable - Unregister a USB Type-C Cable
1262 * @cable: The cable to be unregistered
1264 * Unregister device created with typec_register_cable().
1266 void typec_unregister_cable(struct typec_cable *cable)
1268 if (!IS_ERR_OR_NULL(cable))
1269 device_unregister(&cable->dev);
1271 EXPORT_SYMBOL_GPL(typec_unregister_cable);
1273 /* ------------------------------------------------------------------------- */
1274 /* USB Type-C ports */
1277 * typec_port_set_usb_power_delivery - Assign USB PD for port.
1278 * @port: USB Type-C port.
1279 * @pd: USB PD instance.
1281 * This routine can be used to set the USB Power Delivery Capabilities for @port
1282 * that it will advertise to the partner.
1284 * If @pd is NULL, the assignment is removed.
1286 int typec_port_set_usb_power_delivery(struct typec_port *port, struct usb_power_delivery *pd)
1290 if (IS_ERR_OR_NULL(port) || port->pd == pd)
1294 ret = usb_power_delivery_link_device(pd, &port->dev);
1298 usb_power_delivery_unlink_device(port->pd, &port->dev);
1305 EXPORT_SYMBOL_GPL(typec_port_set_usb_power_delivery);
1307 static ssize_t select_usb_power_delivery_store(struct device *dev,
1308 struct device_attribute *attr,
1309 const char *buf, size_t size)
1311 struct typec_port *port = to_typec_port(dev);
1312 struct usb_power_delivery *pd;
1314 if (!port->ops || !port->ops->pd_set)
1317 pd = usb_power_delivery_find(buf);
1321 return port->ops->pd_set(port, pd);
1324 static ssize_t select_usb_power_delivery_show(struct device *dev,
1325 struct device_attribute *attr, char *buf)
1327 struct typec_port *port = to_typec_port(dev);
1328 struct usb_power_delivery **pds;
1331 if (!port->ops || !port->ops->pd_get)
1334 pds = port->ops->pd_get(port);
1338 for (i = 0; pds[i]; i++) {
1339 if (pds[i] == port->pd)
1340 ret += sysfs_emit_at(buf, ret, "[%s] ", dev_name(&pds[i]->dev));
1342 ret += sysfs_emit_at(buf, ret, "%s ", dev_name(&pds[i]->dev));
1345 buf[ret - 1] = '\n';
1349 static DEVICE_ATTR_RW(select_usb_power_delivery);
1351 static struct attribute *port_attrs[] = {
1352 &dev_attr_select_usb_power_delivery.attr,
1356 static umode_t port_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
1358 struct typec_port *port = to_typec_port(kobj_to_dev(kobj));
1360 if (!port->pd || !port->ops || !port->ops->pd_get)
1362 if (!port->ops->pd_set)
1368 static const struct attribute_group pd_group = {
1369 .is_visible = port_attr_is_visible,
1370 .attrs = port_attrs,
1373 static const char * const typec_orientations[] = {
1374 [TYPEC_ORIENTATION_NONE] = "unknown",
1375 [TYPEC_ORIENTATION_NORMAL] = "normal",
1376 [TYPEC_ORIENTATION_REVERSE] = "reverse",
1379 static const char * const typec_roles[] = {
1380 [TYPEC_SINK] = "sink",
1381 [TYPEC_SOURCE] = "source",
1384 static const char * const typec_data_roles[] = {
1385 [TYPEC_DEVICE] = "device",
1386 [TYPEC_HOST] = "host",
1389 static const char * const typec_port_power_roles[] = {
1390 [TYPEC_PORT_SRC] = "source",
1391 [TYPEC_PORT_SNK] = "sink",
1392 [TYPEC_PORT_DRP] = "dual",
1395 static const char * const typec_port_data_roles[] = {
1396 [TYPEC_PORT_DFP] = "host",
1397 [TYPEC_PORT_UFP] = "device",
1398 [TYPEC_PORT_DRD] = "dual",
1401 static const char * const typec_port_types_drp[] = {
1402 [TYPEC_PORT_SRC] = "dual [source] sink",
1403 [TYPEC_PORT_SNK] = "dual source [sink]",
1404 [TYPEC_PORT_DRP] = "[dual] source sink",
1408 preferred_role_store(struct device *dev, struct device_attribute *attr,
1409 const char *buf, size_t size)
1411 struct typec_port *port = to_typec_port(dev);
1415 if (port->cap->type != TYPEC_PORT_DRP) {
1416 dev_dbg(dev, "Preferred role only supported with DRP ports\n");
1420 if (!port->ops || !port->ops->try_role) {
1421 dev_dbg(dev, "Setting preferred role not supported\n");
1425 role = sysfs_match_string(typec_roles, buf);
1427 if (sysfs_streq(buf, "none"))
1428 role = TYPEC_NO_PREFERRED_ROLE;
1433 ret = port->ops->try_role(port, role);
1437 port->prefer_role = role;
1442 preferred_role_show(struct device *dev, struct device_attribute *attr,
1445 struct typec_port *port = to_typec_port(dev);
1447 if (port->cap->type != TYPEC_PORT_DRP)
1450 if (port->prefer_role < 0)
1453 return sprintf(buf, "%s\n", typec_roles[port->prefer_role]);
1455 static DEVICE_ATTR_RW(preferred_role);
1457 static ssize_t data_role_store(struct device *dev,
1458 struct device_attribute *attr,
1459 const char *buf, size_t size)
1461 struct typec_port *port = to_typec_port(dev);
1464 if (!port->ops || !port->ops->dr_set) {
1465 dev_dbg(dev, "data role swapping not supported\n");
1469 ret = sysfs_match_string(typec_data_roles, buf);
1473 mutex_lock(&port->port_type_lock);
1474 if (port->cap->data != TYPEC_PORT_DRD) {
1476 goto unlock_and_ret;
1479 ret = port->ops->dr_set(port, ret);
1481 goto unlock_and_ret;
1485 mutex_unlock(&port->port_type_lock);
1489 static ssize_t data_role_show(struct device *dev,
1490 struct device_attribute *attr, char *buf)
1492 struct typec_port *port = to_typec_port(dev);
1494 if (port->cap->data == TYPEC_PORT_DRD)
1495 return sprintf(buf, "%s\n", port->data_role == TYPEC_HOST ?
1496 "[host] device" : "host [device]");
1498 return sprintf(buf, "[%s]\n", typec_data_roles[port->data_role]);
1500 static DEVICE_ATTR_RW(data_role);
1502 static ssize_t power_role_store(struct device *dev,
1503 struct device_attribute *attr,
1504 const char *buf, size_t size)
1506 struct typec_port *port = to_typec_port(dev);
1509 if (!port->ops || !port->ops->pr_set) {
1510 dev_dbg(dev, "power role swapping not supported\n");
1514 if (port->pwr_opmode != TYPEC_PWR_MODE_PD) {
1515 dev_dbg(dev, "partner unable to swap power role\n");
1519 ret = sysfs_match_string(typec_roles, buf);
1523 mutex_lock(&port->port_type_lock);
1524 if (port->port_type != TYPEC_PORT_DRP) {
1525 dev_dbg(dev, "port type fixed at \"%s\"",
1526 typec_port_power_roles[port->port_type]);
1528 goto unlock_and_ret;
1531 ret = port->ops->pr_set(port, ret);
1533 goto unlock_and_ret;
1537 mutex_unlock(&port->port_type_lock);
1541 static ssize_t power_role_show(struct device *dev,
1542 struct device_attribute *attr, char *buf)
1544 struct typec_port *port = to_typec_port(dev);
1546 if (port->cap->type == TYPEC_PORT_DRP)
1547 return sprintf(buf, "%s\n", port->pwr_role == TYPEC_SOURCE ?
1548 "[source] sink" : "source [sink]");
1550 return sprintf(buf, "[%s]\n", typec_roles[port->pwr_role]);
1552 static DEVICE_ATTR_RW(power_role);
1555 port_type_store(struct device *dev, struct device_attribute *attr,
1556 const char *buf, size_t size)
1558 struct typec_port *port = to_typec_port(dev);
1560 enum typec_port_type type;
1562 if (port->cap->type != TYPEC_PORT_DRP ||
1563 !port->ops || !port->ops->port_type_set) {
1564 dev_dbg(dev, "changing port type not supported\n");
1568 ret = sysfs_match_string(typec_port_power_roles, buf);
1573 mutex_lock(&port->port_type_lock);
1575 if (port->port_type == type) {
1577 goto unlock_and_ret;
1580 ret = port->ops->port_type_set(port, type);
1582 goto unlock_and_ret;
1584 port->port_type = type;
1588 mutex_unlock(&port->port_type_lock);
1593 port_type_show(struct device *dev, struct device_attribute *attr,
1596 struct typec_port *port = to_typec_port(dev);
1598 if (port->cap->type == TYPEC_PORT_DRP)
1599 return sprintf(buf, "%s\n",
1600 typec_port_types_drp[port->port_type]);
1602 return sprintf(buf, "[%s]\n", typec_port_power_roles[port->cap->type]);
1604 static DEVICE_ATTR_RW(port_type);
1606 static const char * const typec_pwr_opmodes[] = {
1607 [TYPEC_PWR_MODE_USB] = "default",
1608 [TYPEC_PWR_MODE_1_5A] = "1.5A",
1609 [TYPEC_PWR_MODE_3_0A] = "3.0A",
1610 [TYPEC_PWR_MODE_PD] = "usb_power_delivery",
1613 static ssize_t power_operation_mode_show(struct device *dev,
1614 struct device_attribute *attr,
1617 struct typec_port *port = to_typec_port(dev);
1619 return sprintf(buf, "%s\n", typec_pwr_opmodes[port->pwr_opmode]);
1621 static DEVICE_ATTR_RO(power_operation_mode);
1623 static ssize_t vconn_source_store(struct device *dev,
1624 struct device_attribute *attr,
1625 const char *buf, size_t size)
1627 struct typec_port *port = to_typec_port(dev);
1631 if (!port->cap->pd_revision) {
1632 dev_dbg(dev, "VCONN swap depends on USB Power Delivery\n");
1636 if (!port->ops || !port->ops->vconn_set) {
1637 dev_dbg(dev, "VCONN swapping not supported\n");
1641 ret = kstrtobool(buf, &source);
1645 ret = port->ops->vconn_set(port, (enum typec_role)source);
1652 static ssize_t vconn_source_show(struct device *dev,
1653 struct device_attribute *attr, char *buf)
1655 struct typec_port *port = to_typec_port(dev);
1657 return sprintf(buf, "%s\n",
1658 port->vconn_role == TYPEC_SOURCE ? "yes" : "no");
1660 static DEVICE_ATTR_RW(vconn_source);
1662 static ssize_t supported_accessory_modes_show(struct device *dev,
1663 struct device_attribute *attr,
1666 struct typec_port *port = to_typec_port(dev);
1670 for (i = 0; i < ARRAY_SIZE(port->cap->accessory); i++) {
1671 if (port->cap->accessory[i])
1672 ret += sprintf(buf + ret, "%s ",
1673 typec_accessory_modes[port->cap->accessory[i]]);
1677 return sprintf(buf, "none\n");
1679 buf[ret - 1] = '\n';
1683 static DEVICE_ATTR_RO(supported_accessory_modes);
1685 static ssize_t usb_typec_revision_show(struct device *dev,
1686 struct device_attribute *attr,
1689 struct typec_port *port = to_typec_port(dev);
1690 u16 rev = port->cap->revision;
1692 return sprintf(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
1694 static DEVICE_ATTR_RO(usb_typec_revision);
1696 static ssize_t usb_power_delivery_revision_show(struct device *dev,
1697 struct device_attribute *attr,
1702 if (is_typec_partner(dev)) {
1703 struct typec_partner *partner = to_typec_partner(dev);
1705 rev = partner->pd_revision;
1706 } else if (is_typec_cable(dev)) {
1707 struct typec_cable *cable = to_typec_cable(dev);
1709 rev = cable->pd_revision;
1710 } else if (is_typec_port(dev)) {
1711 struct typec_port *p = to_typec_port(dev);
1713 rev = p->cap->pd_revision;
1715 return sysfs_emit(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
1718 static ssize_t orientation_show(struct device *dev,
1719 struct device_attribute *attr,
1722 struct typec_port *port = to_typec_port(dev);
1724 return sprintf(buf, "%s\n", typec_orientations[port->orientation]);
1726 static DEVICE_ATTR_RO(orientation);
1728 static struct attribute *typec_attrs[] = {
1729 &dev_attr_data_role.attr,
1730 &dev_attr_power_operation_mode.attr,
1731 &dev_attr_power_role.attr,
1732 &dev_attr_preferred_role.attr,
1733 &dev_attr_supported_accessory_modes.attr,
1734 &dev_attr_usb_power_delivery_revision.attr,
1735 &dev_attr_usb_typec_revision.attr,
1736 &dev_attr_vconn_source.attr,
1737 &dev_attr_port_type.attr,
1738 &dev_attr_orientation.attr,
1742 static umode_t typec_attr_is_visible(struct kobject *kobj,
1743 struct attribute *attr, int n)
1745 struct typec_port *port = to_typec_port(kobj_to_dev(kobj));
1747 if (attr == &dev_attr_data_role.attr) {
1748 if (port->cap->data != TYPEC_PORT_DRD ||
1749 !port->ops || !port->ops->dr_set)
1751 } else if (attr == &dev_attr_power_role.attr) {
1752 if (port->cap->type != TYPEC_PORT_DRP ||
1753 !port->ops || !port->ops->pr_set)
1755 } else if (attr == &dev_attr_vconn_source.attr) {
1756 if (!port->cap->pd_revision ||
1757 !port->ops || !port->ops->vconn_set)
1759 } else if (attr == &dev_attr_preferred_role.attr) {
1760 if (port->cap->type != TYPEC_PORT_DRP ||
1761 !port->ops || !port->ops->try_role)
1763 } else if (attr == &dev_attr_port_type.attr) {
1764 if (!port->ops || !port->ops->port_type_set)
1766 if (port->cap->type != TYPEC_PORT_DRP)
1768 } else if (attr == &dev_attr_orientation.attr) {
1769 if (port->cap->orientation_aware)
1777 static const struct attribute_group typec_group = {
1778 .is_visible = typec_attr_is_visible,
1779 .attrs = typec_attrs,
1782 static const struct attribute_group *typec_groups[] = {
1788 static int typec_uevent(const struct device *dev, struct kobj_uevent_env *env)
1792 ret = add_uevent_var(env, "TYPEC_PORT=%s", dev_name(dev));
1794 dev_err(dev, "failed to add uevent TYPEC_PORT\n");
1799 static void typec_release(struct device *dev)
1801 struct typec_port *port = to_typec_port(dev);
1803 ida_simple_remove(&typec_index_ida, port->id);
1804 ida_destroy(&port->mode_ids);
1805 typec_switch_put(port->sw);
1806 typec_mux_put(port->mux);
1807 typec_retimer_put(port->retimer);
1812 const struct device_type typec_port_dev_type = {
1813 .name = "typec_port",
1814 .groups = typec_groups,
1815 .uevent = typec_uevent,
1816 .release = typec_release,
1819 /* --------------------------------------- */
1820 /* Driver callbacks to report role updates */
1822 static int partner_match(struct device *dev, void *data)
1824 return is_typec_partner(dev);
1827 static struct typec_partner *typec_get_partner(struct typec_port *port)
1831 dev = device_find_child(&port->dev, NULL, partner_match);
1835 return to_typec_partner(dev);
1838 static void typec_partner_attach(struct typec_connector *con, struct device *dev)
1840 struct typec_port *port = container_of(con, struct typec_port, con);
1841 struct typec_partner *partner = typec_get_partner(port);
1842 struct usb_device *udev = to_usb_device(dev);
1844 if (udev->speed < USB_SPEED_SUPER)
1845 port->usb2_dev = dev;
1847 port->usb3_dev = dev;
1850 typec_partner_link_device(partner, dev);
1851 put_device(&partner->dev);
1855 static void typec_partner_deattach(struct typec_connector *con, struct device *dev)
1857 struct typec_port *port = container_of(con, struct typec_port, con);
1858 struct typec_partner *partner = typec_get_partner(port);
1861 typec_partner_unlink_device(partner, dev);
1862 put_device(&partner->dev);
1865 if (port->usb2_dev == dev)
1866 port->usb2_dev = NULL;
1867 else if (port->usb3_dev == dev)
1868 port->usb3_dev = NULL;
1872 * typec_set_data_role - Report data role change
1873 * @port: The USB Type-C Port where the role was changed
1874 * @role: The new data role
1876 * This routine is used by the port drivers to report data role changes.
1878 void typec_set_data_role(struct typec_port *port, enum typec_data_role role)
1880 struct typec_partner *partner;
1882 if (port->data_role == role)
1885 port->data_role = role;
1886 sysfs_notify(&port->dev.kobj, NULL, "data_role");
1887 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1889 partner = typec_get_partner(port);
1893 if (partner->identity)
1894 typec_product_type_notify(&partner->dev);
1896 put_device(&partner->dev);
1898 EXPORT_SYMBOL_GPL(typec_set_data_role);
1901 * typec_set_pwr_role - Report power role change
1902 * @port: The USB Type-C Port where the role was changed
1903 * @role: The new data role
1905 * This routine is used by the port drivers to report power role changes.
1907 void typec_set_pwr_role(struct typec_port *port, enum typec_role role)
1909 if (port->pwr_role == role)
1912 port->pwr_role = role;
1913 sysfs_notify(&port->dev.kobj, NULL, "power_role");
1914 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1916 EXPORT_SYMBOL_GPL(typec_set_pwr_role);
1919 * typec_set_vconn_role - Report VCONN source change
1920 * @port: The USB Type-C Port which VCONN role changed
1921 * @role: Source when @port is sourcing VCONN, or Sink when it's not
1923 * This routine is used by the port drivers to report if the VCONN source is
1926 void typec_set_vconn_role(struct typec_port *port, enum typec_role role)
1928 if (port->vconn_role == role)
1931 port->vconn_role = role;
1932 sysfs_notify(&port->dev.kobj, NULL, "vconn_source");
1933 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1935 EXPORT_SYMBOL_GPL(typec_set_vconn_role);
1938 * typec_set_pwr_opmode - Report changed power operation mode
1939 * @port: The USB Type-C Port where the mode was changed
1940 * @opmode: New power operation mode
1942 * This routine is used by the port drivers to report changed power operation
1943 * mode in @port. The modes are USB (default), 1.5A, 3.0A as defined in USB
1944 * Type-C specification, and "USB Power Delivery" when the power levels are
1945 * negotiated with methods defined in USB Power Delivery specification.
1947 void typec_set_pwr_opmode(struct typec_port *port,
1948 enum typec_pwr_opmode opmode)
1950 struct device *partner_dev;
1952 if (port->pwr_opmode == opmode)
1955 port->pwr_opmode = opmode;
1956 sysfs_notify(&port->dev.kobj, NULL, "power_operation_mode");
1957 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1959 partner_dev = device_find_child(&port->dev, NULL, partner_match);
1961 struct typec_partner *partner = to_typec_partner(partner_dev);
1963 if (opmode == TYPEC_PWR_MODE_PD && !partner->usb_pd) {
1964 partner->usb_pd = 1;
1965 sysfs_notify(&partner_dev->kobj, NULL,
1966 "supports_usb_power_delivery");
1967 kobject_uevent(&partner_dev->kobj, KOBJ_CHANGE);
1969 put_device(partner_dev);
1972 EXPORT_SYMBOL_GPL(typec_set_pwr_opmode);
1975 * typec_find_pwr_opmode - Get the typec power operation mode capability
1976 * @name: power operation mode string
1978 * This routine is used to find the typec_pwr_opmode by its string @name.
1980 * Returns typec_pwr_opmode if success, otherwise negative error code.
1982 int typec_find_pwr_opmode(const char *name)
1984 return match_string(typec_pwr_opmodes,
1985 ARRAY_SIZE(typec_pwr_opmodes), name);
1987 EXPORT_SYMBOL_GPL(typec_find_pwr_opmode);
1990 * typec_find_orientation - Convert orientation string to enum typec_orientation
1991 * @name: Orientation string
1993 * This routine is used to find the typec_orientation by its string name @name.
1995 * Returns the orientation value on success, otherwise negative error code.
1997 int typec_find_orientation(const char *name)
1999 return match_string(typec_orientations, ARRAY_SIZE(typec_orientations),
2002 EXPORT_SYMBOL_GPL(typec_find_orientation);
2005 * typec_find_port_power_role - Get the typec port power capability
2006 * @name: port power capability string
2008 * This routine is used to find the typec_port_type by its string name.
2010 * Returns typec_port_type if success, otherwise negative error code.
2012 int typec_find_port_power_role(const char *name)
2014 return match_string(typec_port_power_roles,
2015 ARRAY_SIZE(typec_port_power_roles), name);
2017 EXPORT_SYMBOL_GPL(typec_find_port_power_role);
2020 * typec_find_power_role - Find the typec one specific power role
2021 * @name: power role string
2023 * This routine is used to find the typec_role by its string name.
2025 * Returns typec_role if success, otherwise negative error code.
2027 int typec_find_power_role(const char *name)
2029 return match_string(typec_roles, ARRAY_SIZE(typec_roles), name);
2031 EXPORT_SYMBOL_GPL(typec_find_power_role);
2034 * typec_find_port_data_role - Get the typec port data capability
2035 * @name: port data capability string
2037 * This routine is used to find the typec_port_data by its string name.
2039 * Returns typec_port_data if success, otherwise negative error code.
2041 int typec_find_port_data_role(const char *name)
2043 return match_string(typec_port_data_roles,
2044 ARRAY_SIZE(typec_port_data_roles), name);
2046 EXPORT_SYMBOL_GPL(typec_find_port_data_role);
2048 /* ------------------------------------------ */
2049 /* API for Multiplexer/DeMultiplexer Switches */
2052 * typec_set_orientation - Set USB Type-C cable plug orientation
2053 * @port: USB Type-C Port
2054 * @orientation: USB Type-C cable plug orientation
2056 * Set cable plug orientation for @port.
2058 int typec_set_orientation(struct typec_port *port,
2059 enum typec_orientation orientation)
2063 ret = typec_switch_set(port->sw, orientation);
2067 port->orientation = orientation;
2068 sysfs_notify(&port->dev.kobj, NULL, "orientation");
2069 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
2073 EXPORT_SYMBOL_GPL(typec_set_orientation);
2076 * typec_get_orientation - Get USB Type-C cable plug orientation
2077 * @port: USB Type-C Port
2079 * Get current cable plug orientation for @port.
2081 enum typec_orientation typec_get_orientation(struct typec_port *port)
2083 return port->orientation;
2085 EXPORT_SYMBOL_GPL(typec_get_orientation);
2088 * typec_set_mode - Set mode of operation for USB Type-C connector
2089 * @port: USB Type-C connector
2090 * @mode: Accessory Mode, USB Operation or Safe State
2092 * Configure @port for Accessory Mode @mode. This function will configure the
2093 * muxes needed for @mode.
2095 int typec_set_mode(struct typec_port *port, int mode)
2097 struct typec_mux_state state = { };
2101 return typec_mux_set(port->mux, &state);
2103 EXPORT_SYMBOL_GPL(typec_set_mode);
2105 /* --------------------------------------- */
2108 * typec_get_negotiated_svdm_version - Get negotiated SVDM Version
2109 * @port: USB Type-C Port.
2111 * Get the negotiated SVDM Version. The Version is set to the port default
2112 * value stored in typec_capability on partner registration, and updated after
2113 * a successful Discover Identity if the negotiated value is less than the
2116 * Returns usb_pd_svdm_ver if the partner has been registered otherwise -ENODEV.
2118 int typec_get_negotiated_svdm_version(struct typec_port *port)
2120 enum usb_pd_svdm_ver svdm_version;
2121 struct device *partner_dev;
2123 partner_dev = device_find_child(&port->dev, NULL, partner_match);
2127 svdm_version = to_typec_partner(partner_dev)->svdm_version;
2128 put_device(partner_dev);
2130 return svdm_version;
2132 EXPORT_SYMBOL_GPL(typec_get_negotiated_svdm_version);
2135 * typec_get_drvdata - Return private driver data pointer
2136 * @port: USB Type-C port
2138 void *typec_get_drvdata(struct typec_port *port)
2140 return dev_get_drvdata(&port->dev);
2142 EXPORT_SYMBOL_GPL(typec_get_drvdata);
2144 int typec_get_fw_cap(struct typec_capability *cap,
2145 struct fwnode_handle *fwnode)
2147 const char *cap_str;
2150 cap->fwnode = fwnode;
2152 ret = fwnode_property_read_string(fwnode, "power-role", &cap_str);
2156 ret = typec_find_port_power_role(cap_str);
2161 /* USB data support is optional */
2162 ret = fwnode_property_read_string(fwnode, "data-role", &cap_str);
2164 ret = typec_find_port_data_role(cap_str);
2170 /* Get the preferred power role for a DRP */
2171 if (cap->type == TYPEC_PORT_DRP) {
2172 cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
2174 ret = fwnode_property_read_string(fwnode, "try-power-role", &cap_str);
2176 ret = typec_find_power_role(cap_str);
2179 cap->prefer_role = ret;
2185 EXPORT_SYMBOL_GPL(typec_get_fw_cap);
2188 * typec_port_register_altmode - Register USB Type-C Port Alternate Mode
2189 * @port: USB Type-C Port that supports the alternate mode
2190 * @desc: Description of the alternate mode
2192 * This routine is used to register an alternate mode that @port is capable of
2195 * Returns handle to the alternate mode on success or ERR_PTR on failure.
2197 struct typec_altmode *
2198 typec_port_register_altmode(struct typec_port *port,
2199 const struct typec_altmode_desc *desc)
2201 struct typec_altmode *adev;
2202 struct typec_mux *mux;
2203 struct typec_retimer *retimer;
2205 mux = typec_mux_get(&port->dev);
2207 return ERR_CAST(mux);
2209 retimer = typec_retimer_get(&port->dev);
2210 if (IS_ERR(retimer)) {
2212 return ERR_CAST(retimer);
2215 adev = typec_register_altmode(&port->dev, desc);
2217 typec_retimer_put(retimer);
2220 to_altmode(adev)->mux = mux;
2221 to_altmode(adev)->retimer = retimer;
2226 EXPORT_SYMBOL_GPL(typec_port_register_altmode);
2228 void typec_port_register_altmodes(struct typec_port *port,
2229 const struct typec_altmode_ops *ops, void *drvdata,
2230 struct typec_altmode **altmodes, size_t n)
2232 struct fwnode_handle *altmodes_node, *child;
2233 struct typec_altmode_desc desc;
2234 struct typec_altmode *alt;
2239 altmodes_node = device_get_named_child_node(&port->dev, "altmodes");
2241 return; /* No altmodes specified */
2243 fwnode_for_each_child_node(altmodes_node, child) {
2244 ret = fwnode_property_read_u32(child, "svid", &svid);
2246 dev_err(&port->dev, "Error reading svid for altmode %s\n",
2247 fwnode_get_name(child));
2251 ret = fwnode_property_read_u32(child, "vdo", &vdo);
2253 dev_err(&port->dev, "Error reading vdo for altmode %s\n",
2254 fwnode_get_name(child));
2259 dev_err(&port->dev, "Error not enough space for altmode %s\n",
2260 fwnode_get_name(child));
2266 desc.mode = index + 1;
2267 alt = typec_port_register_altmode(port, &desc);
2269 dev_err(&port->dev, "Error registering altmode %s\n",
2270 fwnode_get_name(child));
2275 typec_altmode_set_drvdata(alt, drvdata);
2276 altmodes[index] = alt;
2280 EXPORT_SYMBOL_GPL(typec_port_register_altmodes);
2283 * typec_register_port - Register a USB Type-C Port
2284 * @parent: Parent device
2285 * @cap: Description of the port
2287 * Registers a device for USB Type-C Port described in @cap.
2289 * Returns handle to the port on success or ERR_PTR on failure.
2291 struct typec_port *typec_register_port(struct device *parent,
2292 const struct typec_capability *cap)
2294 struct typec_port *port;
2298 port = kzalloc(sizeof(*port), GFP_KERNEL);
2300 return ERR_PTR(-ENOMEM);
2302 id = ida_simple_get(&typec_index_ida, 0, 0, GFP_KERNEL);
2308 switch (cap->type) {
2309 case TYPEC_PORT_SRC:
2310 port->pwr_role = TYPEC_SOURCE;
2311 port->vconn_role = TYPEC_SOURCE;
2313 case TYPEC_PORT_SNK:
2314 port->pwr_role = TYPEC_SINK;
2315 port->vconn_role = TYPEC_SINK;
2317 case TYPEC_PORT_DRP:
2318 if (cap->prefer_role != TYPEC_NO_PREFERRED_ROLE)
2319 port->pwr_role = cap->prefer_role;
2321 port->pwr_role = TYPEC_SINK;
2325 switch (cap->data) {
2326 case TYPEC_PORT_DFP:
2327 port->data_role = TYPEC_HOST;
2329 case TYPEC_PORT_UFP:
2330 port->data_role = TYPEC_DEVICE;
2332 case TYPEC_PORT_DRD:
2333 if (cap->prefer_role == TYPEC_SOURCE)
2334 port->data_role = TYPEC_HOST;
2336 port->data_role = TYPEC_DEVICE;
2340 ida_init(&port->mode_ids);
2341 mutex_init(&port->port_type_lock);
2344 port->ops = cap->ops;
2345 port->port_type = cap->type;
2346 port->prefer_role = cap->prefer_role;
2347 port->con.attach = typec_partner_attach;
2348 port->con.deattach = typec_partner_deattach;
2350 device_initialize(&port->dev);
2351 port->dev.class = &typec_class;
2352 port->dev.parent = parent;
2353 port->dev.fwnode = cap->fwnode;
2354 port->dev.type = &typec_port_dev_type;
2355 dev_set_name(&port->dev, "port%d", id);
2356 dev_set_drvdata(&port->dev, cap->driver_data);
2358 port->cap = kmemdup(cap, sizeof(*cap), GFP_KERNEL);
2360 put_device(&port->dev);
2361 return ERR_PTR(-ENOMEM);
2364 port->sw = typec_switch_get(&port->dev);
2365 if (IS_ERR(port->sw)) {
2366 ret = PTR_ERR(port->sw);
2367 put_device(&port->dev);
2368 return ERR_PTR(ret);
2371 port->mux = typec_mux_get(&port->dev);
2372 if (IS_ERR(port->mux)) {
2373 ret = PTR_ERR(port->mux);
2374 put_device(&port->dev);
2375 return ERR_PTR(ret);
2378 port->retimer = typec_retimer_get(&port->dev);
2379 if (IS_ERR(port->retimer)) {
2380 ret = PTR_ERR(port->retimer);
2381 put_device(&port->dev);
2382 return ERR_PTR(ret);
2387 ret = device_add(&port->dev);
2389 dev_err(parent, "failed to register port (%d)\n", ret);
2390 put_device(&port->dev);
2391 return ERR_PTR(ret);
2394 ret = usb_power_delivery_link_device(port->pd, &port->dev);
2396 dev_err(&port->dev, "failed to link pd\n");
2397 device_unregister(&port->dev);
2398 return ERR_PTR(ret);
2401 ret = typec_link_ports(port);
2403 dev_warn(&port->dev, "failed to create symlinks (%d)\n", ret);
2407 EXPORT_SYMBOL_GPL(typec_register_port);
2410 * typec_unregister_port - Unregister a USB Type-C Port
2411 * @port: The port to be unregistered
2413 * Unregister device created with typec_register_port().
2415 void typec_unregister_port(struct typec_port *port)
2417 if (!IS_ERR_OR_NULL(port)) {
2418 typec_unlink_ports(port);
2419 typec_port_set_usb_power_delivery(port, NULL);
2420 device_unregister(&port->dev);
2423 EXPORT_SYMBOL_GPL(typec_unregister_port);
2425 static int __init typec_init(void)
2429 ret = bus_register(&typec_bus);
2433 ret = class_register(&typec_mux_class);
2435 goto err_unregister_bus;
2437 ret = class_register(&retimer_class);
2439 goto err_unregister_mux_class;
2441 ret = class_register(&typec_class);
2443 goto err_unregister_retimer_class;
2445 ret = usb_power_delivery_init();
2447 goto err_unregister_class;
2451 err_unregister_class:
2452 class_unregister(&typec_class);
2454 err_unregister_retimer_class:
2455 class_unregister(&retimer_class);
2457 err_unregister_mux_class:
2458 class_unregister(&typec_mux_class);
2461 bus_unregister(&typec_bus);
2465 subsys_initcall(typec_init);
2467 static void __exit typec_exit(void)
2469 usb_power_delivery_exit();
2470 class_unregister(&typec_class);
2471 ida_destroy(&typec_index_ida);
2472 bus_unregister(&typec_bus);
2473 class_unregister(&typec_mux_class);
2474 class_unregister(&retimer_class);
2476 module_exit(typec_exit);
2478 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
2479 MODULE_LICENSE("GPL v2");
2480 MODULE_DESCRIPTION("USB Type-C Connector Class");