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/device.h>
10 #include <linux/module.h>
11 #include <linux/mutex.h>
12 #include <linux/slab.h>
18 enum typec_plug_index index;
24 enum typec_plug_type type;
25 struct usb_pd_identity *identity;
26 unsigned int active:1;
29 struct typec_partner {
31 unsigned int usb_pd:1;
32 struct usb_pd_identity *identity;
33 enum typec_accessory accessory;
43 enum typec_data_role data_role;
44 enum typec_role pwr_role;
45 enum typec_role vconn_role;
46 enum typec_pwr_opmode pwr_opmode;
47 enum typec_port_type port_type;
48 struct mutex port_type_lock;
50 enum typec_orientation orientation;
51 struct typec_switch *sw;
52 struct typec_mux *mux;
54 const struct typec_capability *cap;
57 #define to_typec_port(_dev_) container_of(_dev_, struct typec_port, dev)
58 #define to_typec_plug(_dev_) container_of(_dev_, struct typec_plug, dev)
59 #define to_typec_cable(_dev_) container_of(_dev_, struct typec_cable, dev)
60 #define to_typec_partner(_dev_) container_of(_dev_, struct typec_partner, dev)
62 static const struct device_type typec_partner_dev_type;
63 static const struct device_type typec_cable_dev_type;
64 static const struct device_type typec_plug_dev_type;
66 #define is_typec_partner(_dev_) (_dev_->type == &typec_partner_dev_type)
67 #define is_typec_cable(_dev_) (_dev_->type == &typec_cable_dev_type)
68 #define is_typec_plug(_dev_) (_dev_->type == &typec_plug_dev_type)
70 static DEFINE_IDA(typec_index_ida);
71 static struct class *typec_class;
73 /* ------------------------------------------------------------------------- */
74 /* Common attributes */
76 static const char * const typec_accessory_modes[] = {
77 [TYPEC_ACCESSORY_NONE] = "none",
78 [TYPEC_ACCESSORY_AUDIO] = "analog_audio",
79 [TYPEC_ACCESSORY_DEBUG] = "debug",
82 static struct usb_pd_identity *get_pd_identity(struct device *dev)
84 if (is_typec_partner(dev)) {
85 struct typec_partner *partner = to_typec_partner(dev);
87 return partner->identity;
88 } else if (is_typec_cable(dev)) {
89 struct typec_cable *cable = to_typec_cable(dev);
91 return cable->identity;
96 static ssize_t id_header_show(struct device *dev, struct device_attribute *attr,
99 struct usb_pd_identity *id = get_pd_identity(dev);
101 return sprintf(buf, "0x%08x\n", id->id_header);
103 static DEVICE_ATTR_RO(id_header);
105 static ssize_t cert_stat_show(struct device *dev, struct device_attribute *attr,
108 struct usb_pd_identity *id = get_pd_identity(dev);
110 return sprintf(buf, "0x%08x\n", id->cert_stat);
112 static DEVICE_ATTR_RO(cert_stat);
114 static ssize_t product_show(struct device *dev, struct device_attribute *attr,
117 struct usb_pd_identity *id = get_pd_identity(dev);
119 return sprintf(buf, "0x%08x\n", id->product);
121 static DEVICE_ATTR_RO(product);
123 static struct attribute *usb_pd_id_attrs[] = {
124 &dev_attr_id_header.attr,
125 &dev_attr_cert_stat.attr,
126 &dev_attr_product.attr,
130 static const struct attribute_group usb_pd_id_group = {
132 .attrs = usb_pd_id_attrs,
135 static const struct attribute_group *usb_pd_id_groups[] = {
140 static void typec_report_identity(struct device *dev)
142 sysfs_notify(&dev->kobj, "identity", "id_header");
143 sysfs_notify(&dev->kobj, "identity", "cert_stat");
144 sysfs_notify(&dev->kobj, "identity", "product");
147 /* ------------------------------------------------------------------------- */
148 /* Alternate Modes */
150 static int altmode_match(struct device *dev, void *data)
152 struct typec_altmode *adev = to_typec_altmode(dev);
153 struct typec_device_id *id = data;
155 if (!is_typec_altmode(dev))
158 return ((adev->svid == id->svid) && (adev->mode == id->mode));
161 static void typec_altmode_set_partner(struct altmode *altmode)
163 struct typec_altmode *adev = &altmode->adev;
164 struct typec_device_id id = { adev->svid, adev->mode, };
165 struct typec_port *port = typec_altmode2port(adev);
166 struct altmode *partner;
169 dev = device_find_child(&port->dev, &id, altmode_match);
173 /* Bind the port alt mode to the partner/plug alt mode. */
174 partner = to_altmode(to_typec_altmode(dev));
175 altmode->partner = partner;
177 /* Bind the partner/plug alt mode to the port alt mode. */
178 if (is_typec_plug(adev->dev.parent)) {
179 struct typec_plug *plug = to_typec_plug(adev->dev.parent);
181 partner->plug[plug->index] = altmode;
183 partner->partner = altmode;
187 static void typec_altmode_put_partner(struct altmode *altmode)
189 struct altmode *partner = altmode->partner;
190 struct typec_altmode *adev;
195 adev = &partner->adev;
197 if (is_typec_plug(adev->dev.parent)) {
198 struct typec_plug *plug = to_typec_plug(adev->dev.parent);
200 partner->plug[plug->index] = NULL;
202 partner->partner = NULL;
204 put_device(&adev->dev);
207 static int __typec_port_match(struct device *dev, const void *name)
209 return !strcmp((const char *)name, dev_name(dev));
212 static void *typec_port_match(struct device_connection *con, int ep, void *data)
214 return class_find_device(typec_class, NULL, con->endpoint[ep],
218 struct typec_altmode *
219 typec_altmode_register_notifier(struct device *dev, u16 svid, u8 mode,
220 struct notifier_block *nb)
222 struct typec_device_id id = { svid, mode, };
223 struct device *altmode_dev;
224 struct device *port_dev;
225 struct altmode *altmode;
228 /* Find the port linked to the caller */
229 port_dev = device_connection_find_match(dev, NULL, NULL,
231 if (IS_ERR_OR_NULL(port_dev))
232 return port_dev ? ERR_CAST(port_dev) : ERR_PTR(-ENODEV);
234 /* Find the altmode with matching svid */
235 altmode_dev = device_find_child(port_dev, &id, altmode_match);
237 put_device(port_dev);
240 return ERR_PTR(-ENODEV);
242 altmode = to_altmode(to_typec_altmode(altmode_dev));
244 /* Register notifier */
245 ret = blocking_notifier_chain_register(&altmode->nh, nb);
247 put_device(altmode_dev);
251 return &altmode->adev;
253 EXPORT_SYMBOL_GPL(typec_altmode_register_notifier);
255 void typec_altmode_unregister_notifier(struct typec_altmode *adev,
256 struct notifier_block *nb)
258 struct altmode *altmode = to_altmode(adev);
260 blocking_notifier_chain_unregister(&altmode->nh, nb);
261 put_device(&adev->dev);
263 EXPORT_SYMBOL_GPL(typec_altmode_unregister_notifier);
266 * typec_altmode_update_active - Report Enter/Exit mode
267 * @adev: Handle to the alternate mode
268 * @active: True when the mode has been entered
270 * If a partner or cable plug executes Enter/Exit Mode command successfully, the
271 * drivers use this routine to report the updated state of the mode.
273 void typec_altmode_update_active(struct typec_altmode *adev, bool active)
277 if (adev->active == active)
280 if (!is_typec_port(adev->dev.parent)) {
282 module_put(adev->dev.driver->owner);
284 WARN_ON(!try_module_get(adev->dev.driver->owner));
287 adev->active = active;
288 snprintf(dir, sizeof(dir), "mode%d", adev->mode);
289 sysfs_notify(&adev->dev.kobj, dir, "active");
290 sysfs_notify(&adev->dev.kobj, NULL, "active");
291 kobject_uevent(&adev->dev.kobj, KOBJ_CHANGE);
293 EXPORT_SYMBOL_GPL(typec_altmode_update_active);
296 * typec_altmode2port - Alternate Mode to USB Type-C port
297 * @alt: The Alternate Mode
299 * Returns handle to the port that a cable plug or partner with @alt is
302 struct typec_port *typec_altmode2port(struct typec_altmode *alt)
304 if (is_typec_plug(alt->dev.parent))
305 return to_typec_port(alt->dev.parent->parent->parent);
306 if (is_typec_partner(alt->dev.parent))
307 return to_typec_port(alt->dev.parent->parent);
308 if (is_typec_port(alt->dev.parent))
309 return to_typec_port(alt->dev.parent);
313 EXPORT_SYMBOL_GPL(typec_altmode2port);
316 vdo_show(struct device *dev, struct device_attribute *attr, char *buf)
318 struct typec_altmode *alt = to_typec_altmode(dev);
320 return sprintf(buf, "0x%08x\n", alt->vdo);
322 static DEVICE_ATTR_RO(vdo);
325 description_show(struct device *dev, struct device_attribute *attr, char *buf)
327 struct typec_altmode *alt = to_typec_altmode(dev);
329 return sprintf(buf, "%s\n", alt->desc ? alt->desc : "");
331 static DEVICE_ATTR_RO(description);
334 active_show(struct device *dev, struct device_attribute *attr, char *buf)
336 struct typec_altmode *alt = to_typec_altmode(dev);
338 return sprintf(buf, "%s\n", alt->active ? "yes" : "no");
341 static ssize_t active_store(struct device *dev, struct device_attribute *attr,
342 const char *buf, size_t size)
344 struct typec_altmode *adev = to_typec_altmode(dev);
345 struct altmode *altmode = to_altmode(adev);
349 ret = kstrtobool(buf, &enter);
353 if (adev->active == enter)
356 if (is_typec_port(adev->dev.parent)) {
357 typec_altmode_update_active(adev, enter);
359 /* Make sure that the partner exits the mode before disabling */
360 if (altmode->partner && !enter && altmode->partner->adev.active)
361 typec_altmode_exit(&altmode->partner->adev);
362 } else if (altmode->partner) {
363 if (enter && !altmode->partner->adev.active) {
364 dev_warn(dev, "port has the mode disabled\n");
369 /* Note: If there is no driver, the mode will not be entered */
370 if (adev->ops && adev->ops->activate) {
371 ret = adev->ops->activate(adev, enter);
378 static DEVICE_ATTR_RW(active);
381 supported_roles_show(struct device *dev, struct device_attribute *attr,
384 struct altmode *alt = to_altmode(to_typec_altmode(dev));
387 switch (alt->roles) {
389 ret = sprintf(buf, "source\n");
392 ret = sprintf(buf, "sink\n");
396 ret = sprintf(buf, "source sink\n");
401 static DEVICE_ATTR_RO(supported_roles);
404 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
406 struct typec_altmode *adev = to_typec_altmode(dev);
408 return sprintf(buf, "%u\n", adev->mode);
410 static DEVICE_ATTR_RO(mode);
413 svid_show(struct device *dev, struct device_attribute *attr, char *buf)
415 struct typec_altmode *adev = to_typec_altmode(dev);
417 return sprintf(buf, "%04x\n", adev->svid);
419 static DEVICE_ATTR_RO(svid);
421 static struct attribute *typec_altmode_attrs[] = {
422 &dev_attr_active.attr,
428 ATTRIBUTE_GROUPS(typec_altmode);
430 static int altmode_id_get(struct device *dev)
434 if (is_typec_partner(dev))
435 ids = &to_typec_partner(dev)->mode_ids;
436 else if (is_typec_plug(dev))
437 ids = &to_typec_plug(dev)->mode_ids;
439 ids = &to_typec_port(dev)->mode_ids;
441 return ida_simple_get(ids, 0, 0, GFP_KERNEL);
444 static void altmode_id_remove(struct device *dev, int id)
448 if (is_typec_partner(dev))
449 ids = &to_typec_partner(dev)->mode_ids;
450 else if (is_typec_plug(dev))
451 ids = &to_typec_plug(dev)->mode_ids;
453 ids = &to_typec_port(dev)->mode_ids;
455 ida_simple_remove(ids, id);
458 static void typec_altmode_release(struct device *dev)
460 struct altmode *alt = to_altmode(to_typec_altmode(dev));
462 typec_altmode_put_partner(alt);
464 altmode_id_remove(alt->adev.dev.parent, alt->id);
468 const struct device_type typec_altmode_dev_type = {
469 .name = "typec_alternate_mode",
470 .groups = typec_altmode_groups,
471 .release = typec_altmode_release,
474 static struct typec_altmode *
475 typec_register_altmode(struct device *parent,
476 const struct typec_altmode_desc *desc)
478 unsigned int id = altmode_id_get(parent);
479 bool is_port = is_typec_port(parent);
483 alt = kzalloc(sizeof(*alt), GFP_KERNEL);
485 altmode_id_remove(parent, id);
486 return ERR_PTR(-ENOMEM);
489 alt->adev.svid = desc->svid;
490 alt->adev.mode = desc->mode;
491 alt->adev.vdo = desc->vdo;
492 alt->roles = desc->roles;
495 alt->attrs[0] = &dev_attr_vdo.attr;
496 alt->attrs[1] = &dev_attr_description.attr;
497 alt->attrs[2] = &dev_attr_active.attr;
500 alt->attrs[3] = &dev_attr_supported_roles.attr;
501 alt->adev.active = true; /* Enabled by default */
504 sprintf(alt->group_name, "mode%d", desc->mode);
505 alt->group.name = alt->group_name;
506 alt->group.attrs = alt->attrs;
507 alt->groups[0] = &alt->group;
509 alt->adev.dev.parent = parent;
510 alt->adev.dev.groups = alt->groups;
511 alt->adev.dev.type = &typec_altmode_dev_type;
512 dev_set_name(&alt->adev.dev, "%s.%u", dev_name(parent), id);
514 /* Link partners and plugs with the ports */
516 BLOCKING_INIT_NOTIFIER_HEAD(&alt->nh);
518 typec_altmode_set_partner(alt);
520 /* The partners are bind to drivers */
521 if (is_typec_partner(parent))
522 alt->adev.dev.bus = &typec_bus;
524 ret = device_register(&alt->adev.dev);
526 dev_err(parent, "failed to register alternate mode (%d)\n",
528 put_device(&alt->adev.dev);
536 * typec_unregister_altmode - Unregister Alternate Mode
537 * @adev: The alternate mode to be unregistered
539 * Unregister device created with typec_partner_register_altmode(),
540 * typec_plug_register_altmode() or typec_port_register_altmode().
542 void typec_unregister_altmode(struct typec_altmode *adev)
544 if (IS_ERR_OR_NULL(adev))
546 typec_mux_put(to_altmode(adev)->mux);
547 device_unregister(&adev->dev);
549 EXPORT_SYMBOL_GPL(typec_unregister_altmode);
551 /* ------------------------------------------------------------------------- */
552 /* Type-C Partners */
554 static ssize_t accessory_mode_show(struct device *dev,
555 struct device_attribute *attr,
558 struct typec_partner *p = to_typec_partner(dev);
560 return sprintf(buf, "%s\n", typec_accessory_modes[p->accessory]);
562 static DEVICE_ATTR_RO(accessory_mode);
564 static ssize_t supports_usb_power_delivery_show(struct device *dev,
565 struct device_attribute *attr,
568 struct typec_partner *p = to_typec_partner(dev);
570 return sprintf(buf, "%s\n", p->usb_pd ? "yes" : "no");
572 static DEVICE_ATTR_RO(supports_usb_power_delivery);
574 static struct attribute *typec_partner_attrs[] = {
575 &dev_attr_accessory_mode.attr,
576 &dev_attr_supports_usb_power_delivery.attr,
579 ATTRIBUTE_GROUPS(typec_partner);
581 static void typec_partner_release(struct device *dev)
583 struct typec_partner *partner = to_typec_partner(dev);
585 ida_destroy(&partner->mode_ids);
589 static const struct device_type typec_partner_dev_type = {
590 .name = "typec_partner",
591 .groups = typec_partner_groups,
592 .release = typec_partner_release,
596 * typec_partner_set_identity - Report result from Discover Identity command
597 * @partner: The partner updated identity values
599 * This routine is used to report that the result of Discover Identity USB power
600 * delivery command has become available.
602 int typec_partner_set_identity(struct typec_partner *partner)
604 if (!partner->identity)
607 typec_report_identity(&partner->dev);
610 EXPORT_SYMBOL_GPL(typec_partner_set_identity);
613 * typec_partner_register_altmode - Register USB Type-C Partner Alternate Mode
614 * @partner: USB Type-C Partner that supports the alternate mode
615 * @desc: Description of the alternate mode
617 * This routine is used to register each alternate mode individually that
618 * @partner has listed in response to Discover SVIDs command. The modes for a
619 * SVID listed in response to Discover Modes command need to be listed in an
622 * Returns handle to the alternate mode on success or NULL on failure.
624 struct typec_altmode *
625 typec_partner_register_altmode(struct typec_partner *partner,
626 const struct typec_altmode_desc *desc)
628 return typec_register_altmode(&partner->dev, desc);
630 EXPORT_SYMBOL_GPL(typec_partner_register_altmode);
633 * typec_register_partner - Register a USB Type-C Partner
634 * @port: The USB Type-C Port the partner is connected to
635 * @desc: Description of the partner
637 * Registers a device for USB Type-C Partner described in @desc.
639 * Returns handle to the partner on success or ERR_PTR on failure.
641 struct typec_partner *typec_register_partner(struct typec_port *port,
642 struct typec_partner_desc *desc)
644 struct typec_partner *partner;
647 partner = kzalloc(sizeof(*partner), GFP_KERNEL);
649 return ERR_PTR(-ENOMEM);
651 ida_init(&partner->mode_ids);
652 partner->usb_pd = desc->usb_pd;
653 partner->accessory = desc->accessory;
655 if (desc->identity) {
657 * Creating directory for the identity only if the driver is
658 * able to provide data to it.
660 partner->dev.groups = usb_pd_id_groups;
661 partner->identity = desc->identity;
664 partner->dev.class = typec_class;
665 partner->dev.parent = &port->dev;
666 partner->dev.type = &typec_partner_dev_type;
667 dev_set_name(&partner->dev, "%s-partner", dev_name(&port->dev));
669 ret = device_register(&partner->dev);
671 dev_err(&port->dev, "failed to register partner (%d)\n", ret);
672 put_device(&partner->dev);
678 EXPORT_SYMBOL_GPL(typec_register_partner);
681 * typec_unregister_partner - Unregister a USB Type-C Partner
682 * @partner: The partner to be unregistered
684 * Unregister device created with typec_register_partner().
686 void typec_unregister_partner(struct typec_partner *partner)
688 if (!IS_ERR_OR_NULL(partner))
689 device_unregister(&partner->dev);
691 EXPORT_SYMBOL_GPL(typec_unregister_partner);
693 /* ------------------------------------------------------------------------- */
694 /* Type-C Cable Plugs */
696 static void typec_plug_release(struct device *dev)
698 struct typec_plug *plug = to_typec_plug(dev);
700 ida_destroy(&plug->mode_ids);
704 static const struct device_type typec_plug_dev_type = {
705 .name = "typec_plug",
706 .release = typec_plug_release,
710 * typec_plug_register_altmode - Register USB Type-C Cable Plug Alternate Mode
711 * @plug: USB Type-C Cable Plug that supports the alternate mode
712 * @desc: Description of the alternate mode
714 * This routine is used to register each alternate mode individually that @plug
715 * has listed in response to Discover SVIDs command. The modes for a SVID that
716 * the plug lists in response to Discover Modes command need to be listed in an
719 * Returns handle to the alternate mode on success or ERR_PTR on failure.
721 struct typec_altmode *
722 typec_plug_register_altmode(struct typec_plug *plug,
723 const struct typec_altmode_desc *desc)
725 return typec_register_altmode(&plug->dev, desc);
727 EXPORT_SYMBOL_GPL(typec_plug_register_altmode);
730 * typec_register_plug - Register a USB Type-C Cable Plug
731 * @cable: USB Type-C Cable with the plug
732 * @desc: Description of the cable plug
734 * Registers a device for USB Type-C Cable Plug described in @desc. A USB Type-C
735 * Cable Plug represents a plug with electronics in it that can response to USB
736 * Power Delivery SOP Prime or SOP Double Prime packages.
738 * Returns handle to the cable plug on success or ERR_PTR on failure.
740 struct typec_plug *typec_register_plug(struct typec_cable *cable,
741 struct typec_plug_desc *desc)
743 struct typec_plug *plug;
747 plug = kzalloc(sizeof(*plug), GFP_KERNEL);
749 return ERR_PTR(-ENOMEM);
751 sprintf(name, "plug%d", desc->index);
753 ida_init(&plug->mode_ids);
754 plug->index = desc->index;
755 plug->dev.class = typec_class;
756 plug->dev.parent = &cable->dev;
757 plug->dev.type = &typec_plug_dev_type;
758 dev_set_name(&plug->dev, "%s-%s", dev_name(cable->dev.parent), name);
760 ret = device_register(&plug->dev);
762 dev_err(&cable->dev, "failed to register plug (%d)\n", ret);
763 put_device(&plug->dev);
769 EXPORT_SYMBOL_GPL(typec_register_plug);
772 * typec_unregister_plug - Unregister a USB Type-C Cable Plug
773 * @plug: The cable plug to be unregistered
775 * Unregister device created with typec_register_plug().
777 void typec_unregister_plug(struct typec_plug *plug)
779 if (!IS_ERR_OR_NULL(plug))
780 device_unregister(&plug->dev);
782 EXPORT_SYMBOL_GPL(typec_unregister_plug);
787 type_show(struct device *dev, struct device_attribute *attr, char *buf)
789 struct typec_cable *cable = to_typec_cable(dev);
791 return sprintf(buf, "%s\n", cable->active ? "active" : "passive");
793 static DEVICE_ATTR_RO(type);
795 static const char * const typec_plug_types[] = {
796 [USB_PLUG_NONE] = "unknown",
797 [USB_PLUG_TYPE_A] = "type-a",
798 [USB_PLUG_TYPE_B] = "type-b",
799 [USB_PLUG_TYPE_C] = "type-c",
800 [USB_PLUG_CAPTIVE] = "captive",
803 static ssize_t plug_type_show(struct device *dev,
804 struct device_attribute *attr, char *buf)
806 struct typec_cable *cable = to_typec_cable(dev);
808 return sprintf(buf, "%s\n", typec_plug_types[cable->type]);
810 static DEVICE_ATTR_RO(plug_type);
812 static struct attribute *typec_cable_attrs[] = {
814 &dev_attr_plug_type.attr,
817 ATTRIBUTE_GROUPS(typec_cable);
819 static void typec_cable_release(struct device *dev)
821 struct typec_cable *cable = to_typec_cable(dev);
826 static const struct device_type typec_cable_dev_type = {
827 .name = "typec_cable",
828 .groups = typec_cable_groups,
829 .release = typec_cable_release,
833 * typec_cable_set_identity - Report result from Discover Identity command
834 * @cable: The cable updated identity values
836 * This routine is used to report that the result of Discover Identity USB power
837 * delivery command has become available.
839 int typec_cable_set_identity(struct typec_cable *cable)
841 if (!cable->identity)
844 typec_report_identity(&cable->dev);
847 EXPORT_SYMBOL_GPL(typec_cable_set_identity);
850 * typec_register_cable - Register a USB Type-C Cable
851 * @port: The USB Type-C Port the cable is connected to
852 * @desc: Description of the cable
854 * Registers a device for USB Type-C Cable described in @desc. The cable will be
855 * parent for the optional cable plug devises.
857 * Returns handle to the cable on success or ERR_PTR on failure.
859 struct typec_cable *typec_register_cable(struct typec_port *port,
860 struct typec_cable_desc *desc)
862 struct typec_cable *cable;
865 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
867 return ERR_PTR(-ENOMEM);
869 cable->type = desc->type;
870 cable->active = desc->active;
872 if (desc->identity) {
874 * Creating directory for the identity only if the driver is
875 * able to provide data to it.
877 cable->dev.groups = usb_pd_id_groups;
878 cable->identity = desc->identity;
881 cable->dev.class = typec_class;
882 cable->dev.parent = &port->dev;
883 cable->dev.type = &typec_cable_dev_type;
884 dev_set_name(&cable->dev, "%s-cable", dev_name(&port->dev));
886 ret = device_register(&cable->dev);
888 dev_err(&port->dev, "failed to register cable (%d)\n", ret);
889 put_device(&cable->dev);
895 EXPORT_SYMBOL_GPL(typec_register_cable);
898 * typec_unregister_cable - Unregister a USB Type-C Cable
899 * @cable: The cable to be unregistered
901 * Unregister device created with typec_register_cable().
903 void typec_unregister_cable(struct typec_cable *cable)
905 if (!IS_ERR_OR_NULL(cable))
906 device_unregister(&cable->dev);
908 EXPORT_SYMBOL_GPL(typec_unregister_cable);
910 /* ------------------------------------------------------------------------- */
911 /* USB Type-C ports */
913 static const char * const typec_roles[] = {
914 [TYPEC_SINK] = "sink",
915 [TYPEC_SOURCE] = "source",
918 static const char * const typec_data_roles[] = {
919 [TYPEC_DEVICE] = "device",
920 [TYPEC_HOST] = "host",
923 static const char * const typec_port_power_roles[] = {
924 [TYPEC_PORT_SRC] = "source",
925 [TYPEC_PORT_SNK] = "sink",
926 [TYPEC_PORT_DRP] = "dual",
929 static const char * const typec_port_data_roles[] = {
930 [TYPEC_PORT_DFP] = "host",
931 [TYPEC_PORT_UFP] = "device",
932 [TYPEC_PORT_DRD] = "dual",
935 static const char * const typec_port_types_drp[] = {
936 [TYPEC_PORT_SRC] = "dual [source] sink",
937 [TYPEC_PORT_SNK] = "dual source [sink]",
938 [TYPEC_PORT_DRP] = "[dual] source sink",
942 preferred_role_store(struct device *dev, struct device_attribute *attr,
943 const char *buf, size_t size)
945 struct typec_port *port = to_typec_port(dev);
949 if (port->cap->type != TYPEC_PORT_DRP) {
950 dev_dbg(dev, "Preferred role only supported with DRP ports\n");
954 if (!port->cap->try_role) {
955 dev_dbg(dev, "Setting preferred role not supported\n");
959 role = sysfs_match_string(typec_roles, buf);
961 if (sysfs_streq(buf, "none"))
962 role = TYPEC_NO_PREFERRED_ROLE;
967 ret = port->cap->try_role(port->cap, role);
971 port->prefer_role = role;
976 preferred_role_show(struct device *dev, struct device_attribute *attr,
979 struct typec_port *port = to_typec_port(dev);
981 if (port->cap->type != TYPEC_PORT_DRP)
984 if (port->prefer_role < 0)
987 return sprintf(buf, "%s\n", typec_roles[port->prefer_role]);
989 static DEVICE_ATTR_RW(preferred_role);
991 static ssize_t data_role_store(struct device *dev,
992 struct device_attribute *attr,
993 const char *buf, size_t size)
995 struct typec_port *port = to_typec_port(dev);
998 if (!port->cap->dr_set) {
999 dev_dbg(dev, "data role swapping not supported\n");
1003 ret = sysfs_match_string(typec_data_roles, buf);
1007 mutex_lock(&port->port_type_lock);
1008 if (port->cap->data != TYPEC_PORT_DRD) {
1010 goto unlock_and_ret;
1013 ret = port->cap->dr_set(port->cap, ret);
1015 goto unlock_and_ret;
1019 mutex_unlock(&port->port_type_lock);
1023 static ssize_t data_role_show(struct device *dev,
1024 struct device_attribute *attr, char *buf)
1026 struct typec_port *port = to_typec_port(dev);
1028 if (port->cap->data == TYPEC_PORT_DRD)
1029 return sprintf(buf, "%s\n", port->data_role == TYPEC_HOST ?
1030 "[host] device" : "host [device]");
1032 return sprintf(buf, "[%s]\n", typec_data_roles[port->data_role]);
1034 static DEVICE_ATTR_RW(data_role);
1036 static ssize_t power_role_store(struct device *dev,
1037 struct device_attribute *attr,
1038 const char *buf, size_t size)
1040 struct typec_port *port = to_typec_port(dev);
1043 if (!port->cap->pd_revision) {
1044 dev_dbg(dev, "USB Power Delivery not supported\n");
1048 if (!port->cap->pr_set) {
1049 dev_dbg(dev, "power role swapping not supported\n");
1053 if (port->pwr_opmode != TYPEC_PWR_MODE_PD) {
1054 dev_dbg(dev, "partner unable to swap power role\n");
1058 ret = sysfs_match_string(typec_roles, buf);
1062 mutex_lock(&port->port_type_lock);
1063 if (port->port_type != TYPEC_PORT_DRP) {
1064 dev_dbg(dev, "port type fixed at \"%s\"",
1065 typec_port_power_roles[port->port_type]);
1067 goto unlock_and_ret;
1070 ret = port->cap->pr_set(port->cap, ret);
1072 goto unlock_and_ret;
1076 mutex_unlock(&port->port_type_lock);
1080 static ssize_t power_role_show(struct device *dev,
1081 struct device_attribute *attr, char *buf)
1083 struct typec_port *port = to_typec_port(dev);
1085 if (port->cap->type == TYPEC_PORT_DRP)
1086 return sprintf(buf, "%s\n", port->pwr_role == TYPEC_SOURCE ?
1087 "[source] sink" : "source [sink]");
1089 return sprintf(buf, "[%s]\n", typec_roles[port->pwr_role]);
1091 static DEVICE_ATTR_RW(power_role);
1094 port_type_store(struct device *dev, struct device_attribute *attr,
1095 const char *buf, size_t size)
1097 struct typec_port *port = to_typec_port(dev);
1099 enum typec_port_type type;
1101 if (!port->cap->port_type_set || port->cap->type != TYPEC_PORT_DRP) {
1102 dev_dbg(dev, "changing port type not supported\n");
1106 ret = sysfs_match_string(typec_port_power_roles, buf);
1111 mutex_lock(&port->port_type_lock);
1113 if (port->port_type == type) {
1115 goto unlock_and_ret;
1118 ret = port->cap->port_type_set(port->cap, type);
1120 goto unlock_and_ret;
1122 port->port_type = type;
1126 mutex_unlock(&port->port_type_lock);
1131 port_type_show(struct device *dev, struct device_attribute *attr,
1134 struct typec_port *port = to_typec_port(dev);
1136 if (port->cap->type == TYPEC_PORT_DRP)
1137 return sprintf(buf, "%s\n",
1138 typec_port_types_drp[port->port_type]);
1140 return sprintf(buf, "[%s]\n", typec_port_power_roles[port->cap->type]);
1142 static DEVICE_ATTR_RW(port_type);
1144 static const char * const typec_pwr_opmodes[] = {
1145 [TYPEC_PWR_MODE_USB] = "default",
1146 [TYPEC_PWR_MODE_1_5A] = "1.5A",
1147 [TYPEC_PWR_MODE_3_0A] = "3.0A",
1148 [TYPEC_PWR_MODE_PD] = "usb_power_delivery",
1151 static ssize_t power_operation_mode_show(struct device *dev,
1152 struct device_attribute *attr,
1155 struct typec_port *port = to_typec_port(dev);
1157 return sprintf(buf, "%s\n", typec_pwr_opmodes[port->pwr_opmode]);
1159 static DEVICE_ATTR_RO(power_operation_mode);
1161 static ssize_t vconn_source_store(struct device *dev,
1162 struct device_attribute *attr,
1163 const char *buf, size_t size)
1165 struct typec_port *port = to_typec_port(dev);
1169 if (!port->cap->pd_revision) {
1170 dev_dbg(dev, "VCONN swap depends on USB Power Delivery\n");
1174 if (!port->cap->vconn_set) {
1175 dev_dbg(dev, "VCONN swapping not supported\n");
1179 ret = kstrtobool(buf, &source);
1183 ret = port->cap->vconn_set(port->cap, (enum typec_role)source);
1190 static ssize_t vconn_source_show(struct device *dev,
1191 struct device_attribute *attr, char *buf)
1193 struct typec_port *port = to_typec_port(dev);
1195 return sprintf(buf, "%s\n",
1196 port->vconn_role == TYPEC_SOURCE ? "yes" : "no");
1198 static DEVICE_ATTR_RW(vconn_source);
1200 static ssize_t supported_accessory_modes_show(struct device *dev,
1201 struct device_attribute *attr,
1204 struct typec_port *port = to_typec_port(dev);
1208 for (i = 0; i < ARRAY_SIZE(port->cap->accessory); i++) {
1209 if (port->cap->accessory[i])
1210 ret += sprintf(buf + ret, "%s ",
1211 typec_accessory_modes[port->cap->accessory[i]]);
1215 return sprintf(buf, "none\n");
1217 buf[ret - 1] = '\n';
1221 static DEVICE_ATTR_RO(supported_accessory_modes);
1223 static ssize_t usb_typec_revision_show(struct device *dev,
1224 struct device_attribute *attr,
1227 struct typec_port *port = to_typec_port(dev);
1228 u16 rev = port->cap->revision;
1230 return sprintf(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
1232 static DEVICE_ATTR_RO(usb_typec_revision);
1234 static ssize_t usb_power_delivery_revision_show(struct device *dev,
1235 struct device_attribute *attr,
1238 struct typec_port *p = to_typec_port(dev);
1240 return sprintf(buf, "%d\n", (p->cap->pd_revision >> 8) & 0xff);
1242 static DEVICE_ATTR_RO(usb_power_delivery_revision);
1244 static struct attribute *typec_attrs[] = {
1245 &dev_attr_data_role.attr,
1246 &dev_attr_power_operation_mode.attr,
1247 &dev_attr_power_role.attr,
1248 &dev_attr_preferred_role.attr,
1249 &dev_attr_supported_accessory_modes.attr,
1250 &dev_attr_usb_power_delivery_revision.attr,
1251 &dev_attr_usb_typec_revision.attr,
1252 &dev_attr_vconn_source.attr,
1253 &dev_attr_port_type.attr,
1256 ATTRIBUTE_GROUPS(typec);
1258 static int typec_uevent(struct device *dev, struct kobj_uevent_env *env)
1262 ret = add_uevent_var(env, "TYPEC_PORT=%s", dev_name(dev));
1264 dev_err(dev, "failed to add uevent TYPEC_PORT\n");
1269 static void typec_release(struct device *dev)
1271 struct typec_port *port = to_typec_port(dev);
1273 ida_simple_remove(&typec_index_ida, port->id);
1274 ida_destroy(&port->mode_ids);
1275 typec_switch_put(port->sw);
1276 typec_mux_put(port->mux);
1280 const struct device_type typec_port_dev_type = {
1281 .name = "typec_port",
1282 .groups = typec_groups,
1283 .uevent = typec_uevent,
1284 .release = typec_release,
1287 /* --------------------------------------- */
1288 /* Driver callbacks to report role updates */
1291 * typec_set_data_role - Report data role change
1292 * @port: The USB Type-C Port where the role was changed
1293 * @role: The new data role
1295 * This routine is used by the port drivers to report data role changes.
1297 void typec_set_data_role(struct typec_port *port, enum typec_data_role role)
1299 if (port->data_role == role)
1302 port->data_role = role;
1303 sysfs_notify(&port->dev.kobj, NULL, "data_role");
1304 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1306 EXPORT_SYMBOL_GPL(typec_set_data_role);
1309 * typec_set_pwr_role - Report power role change
1310 * @port: The USB Type-C Port where the role was changed
1311 * @role: The new data role
1313 * This routine is used by the port drivers to report power role changes.
1315 void typec_set_pwr_role(struct typec_port *port, enum typec_role role)
1317 if (port->pwr_role == role)
1320 port->pwr_role = role;
1321 sysfs_notify(&port->dev.kobj, NULL, "power_role");
1322 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1324 EXPORT_SYMBOL_GPL(typec_set_pwr_role);
1327 * typec_set_pwr_role - Report VCONN source change
1328 * @port: The USB Type-C Port which VCONN role changed
1329 * @role: Source when @port is sourcing VCONN, or Sink when it's not
1331 * This routine is used by the port drivers to report if the VCONN source is
1334 void typec_set_vconn_role(struct typec_port *port, enum typec_role role)
1336 if (port->vconn_role == role)
1339 port->vconn_role = role;
1340 sysfs_notify(&port->dev.kobj, NULL, "vconn_source");
1341 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1343 EXPORT_SYMBOL_GPL(typec_set_vconn_role);
1345 static int partner_match(struct device *dev, void *data)
1347 return is_typec_partner(dev);
1351 * typec_set_pwr_opmode - Report changed power operation mode
1352 * @port: The USB Type-C Port where the mode was changed
1353 * @opmode: New power operation mode
1355 * This routine is used by the port drivers to report changed power operation
1356 * mode in @port. The modes are USB (default), 1.5A, 3.0A as defined in USB
1357 * Type-C specification, and "USB Power Delivery" when the power levels are
1358 * negotiated with methods defined in USB Power Delivery specification.
1360 void typec_set_pwr_opmode(struct typec_port *port,
1361 enum typec_pwr_opmode opmode)
1363 struct device *partner_dev;
1365 if (port->pwr_opmode == opmode)
1368 port->pwr_opmode = opmode;
1369 sysfs_notify(&port->dev.kobj, NULL, "power_operation_mode");
1370 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1372 partner_dev = device_find_child(&port->dev, NULL, partner_match);
1374 struct typec_partner *partner = to_typec_partner(partner_dev);
1376 if (opmode == TYPEC_PWR_MODE_PD && !partner->usb_pd) {
1377 partner->usb_pd = 1;
1378 sysfs_notify(&partner_dev->kobj, NULL,
1379 "supports_usb_power_delivery");
1381 put_device(partner_dev);
1384 EXPORT_SYMBOL_GPL(typec_set_pwr_opmode);
1387 * typec_find_port_power_role - Get the typec port power capability
1388 * @name: port power capability string
1390 * This routine is used to find the typec_port_type by its string name.
1392 * Returns typec_port_type if success, otherwise negative error code.
1394 int typec_find_port_power_role(const char *name)
1396 return match_string(typec_port_power_roles,
1397 ARRAY_SIZE(typec_port_power_roles), name);
1399 EXPORT_SYMBOL_GPL(typec_find_port_power_role);
1402 * typec_find_power_role - Find the typec one specific power role
1403 * @name: power role string
1405 * This routine is used to find the typec_role by its string name.
1407 * Returns typec_role if success, otherwise negative error code.
1409 int typec_find_power_role(const char *name)
1411 return match_string(typec_roles, ARRAY_SIZE(typec_roles), name);
1413 EXPORT_SYMBOL_GPL(typec_find_power_role);
1416 * typec_find_port_data_role - Get the typec port data capability
1417 * @name: port data capability string
1419 * This routine is used to find the typec_port_data by its string name.
1421 * Returns typec_port_data if success, otherwise negative error code.
1423 int typec_find_port_data_role(const char *name)
1425 return match_string(typec_port_data_roles,
1426 ARRAY_SIZE(typec_port_data_roles), name);
1428 EXPORT_SYMBOL_GPL(typec_find_port_data_role);
1430 /* ------------------------------------------ */
1431 /* API for Multiplexer/DeMultiplexer Switches */
1434 * typec_set_orientation - Set USB Type-C cable plug orientation
1435 * @port: USB Type-C Port
1436 * @orientation: USB Type-C cable plug orientation
1438 * Set cable plug orientation for @port.
1440 int typec_set_orientation(struct typec_port *port,
1441 enum typec_orientation orientation)
1446 ret = port->sw->set(port->sw, orientation);
1451 port->orientation = orientation;
1455 EXPORT_SYMBOL_GPL(typec_set_orientation);
1458 * typec_get_orientation - Get USB Type-C cable plug orientation
1459 * @port: USB Type-C Port
1461 * Get current cable plug orientation for @port.
1463 enum typec_orientation typec_get_orientation(struct typec_port *port)
1465 return port->orientation;
1467 EXPORT_SYMBOL_GPL(typec_get_orientation);
1470 * typec_set_mode - Set mode of operation for USB Type-C connector
1471 * @port: USB Type-C connector
1472 * @mode: Accessory Mode, USB Operation or Safe State
1474 * Configure @port for Accessory Mode @mode. This function will configure the
1475 * muxes needed for @mode.
1477 int typec_set_mode(struct typec_port *port, int mode)
1479 return port->mux ? port->mux->set(port->mux, mode) : 0;
1481 EXPORT_SYMBOL_GPL(typec_set_mode);
1483 /* --------------------------------------- */
1486 * typec_port_register_altmode - Register USB Type-C Port Alternate Mode
1487 * @port: USB Type-C Port that supports the alternate mode
1488 * @desc: Description of the alternate mode
1490 * This routine is used to register an alternate mode that @port is capable of
1493 * Returns handle to the alternate mode on success or ERR_PTR on failure.
1495 struct typec_altmode *
1496 typec_port_register_altmode(struct typec_port *port,
1497 const struct typec_altmode_desc *desc)
1499 struct typec_altmode *adev;
1500 struct typec_mux *mux;
1503 sprintf(id, "id%04xm%02x", desc->svid, desc->mode);
1505 mux = typec_mux_get(&port->dev, id);
1507 return ERR_CAST(mux);
1509 adev = typec_register_altmode(&port->dev, desc);
1513 to_altmode(adev)->mux = mux;
1517 EXPORT_SYMBOL_GPL(typec_port_register_altmode);
1520 * typec_register_port - Register a USB Type-C Port
1521 * @parent: Parent device
1522 * @cap: Description of the port
1524 * Registers a device for USB Type-C Port described in @cap.
1526 * Returns handle to the port on success or ERR_PTR on failure.
1528 struct typec_port *typec_register_port(struct device *parent,
1529 const struct typec_capability *cap)
1531 struct typec_port *port;
1535 port = kzalloc(sizeof(*port), GFP_KERNEL);
1537 return ERR_PTR(-ENOMEM);
1539 id = ida_simple_get(&typec_index_ida, 0, 0, GFP_KERNEL);
1545 switch (cap->type) {
1546 case TYPEC_PORT_SRC:
1547 port->pwr_role = TYPEC_SOURCE;
1548 port->vconn_role = TYPEC_SOURCE;
1550 case TYPEC_PORT_SNK:
1551 port->pwr_role = TYPEC_SINK;
1552 port->vconn_role = TYPEC_SINK;
1554 case TYPEC_PORT_DRP:
1555 if (cap->prefer_role != TYPEC_NO_PREFERRED_ROLE)
1556 port->pwr_role = cap->prefer_role;
1558 port->pwr_role = TYPEC_SINK;
1562 switch (cap->data) {
1563 case TYPEC_PORT_DFP:
1564 port->data_role = TYPEC_HOST;
1566 case TYPEC_PORT_UFP:
1567 port->data_role = TYPEC_DEVICE;
1569 case TYPEC_PORT_DRD:
1570 if (cap->prefer_role == TYPEC_SOURCE)
1571 port->data_role = TYPEC_HOST;
1573 port->data_role = TYPEC_DEVICE;
1577 ida_init(&port->mode_ids);
1578 mutex_init(&port->port_type_lock);
1582 port->port_type = cap->type;
1583 port->prefer_role = cap->prefer_role;
1585 device_initialize(&port->dev);
1586 port->dev.class = typec_class;
1587 port->dev.parent = parent;
1588 port->dev.fwnode = cap->fwnode;
1589 port->dev.type = &typec_port_dev_type;
1590 dev_set_name(&port->dev, "port%d", id);
1592 port->sw = typec_switch_get(&port->dev);
1593 if (IS_ERR(port->sw)) {
1594 ret = PTR_ERR(port->sw);
1595 put_device(&port->dev);
1596 return ERR_PTR(ret);
1599 port->mux = typec_mux_get(&port->dev, "typec-mux");
1600 if (IS_ERR(port->mux)) {
1601 ret = PTR_ERR(port->mux);
1602 put_device(&port->dev);
1603 return ERR_PTR(ret);
1606 ret = device_add(&port->dev);
1608 dev_err(parent, "failed to register port (%d)\n", ret);
1609 put_device(&port->dev);
1610 return ERR_PTR(ret);
1615 EXPORT_SYMBOL_GPL(typec_register_port);
1618 * typec_unregister_port - Unregister a USB Type-C Port
1619 * @port: The port to be unregistered
1621 * Unregister device created with typec_register_port().
1623 void typec_unregister_port(struct typec_port *port)
1625 if (!IS_ERR_OR_NULL(port))
1626 device_unregister(&port->dev);
1628 EXPORT_SYMBOL_GPL(typec_unregister_port);
1630 static int __init typec_init(void)
1634 ret = bus_register(&typec_bus);
1638 typec_class = class_create(THIS_MODULE, "typec");
1639 if (IS_ERR(typec_class)) {
1640 bus_unregister(&typec_bus);
1641 return PTR_ERR(typec_class);
1646 subsys_initcall(typec_init);
1648 static void __exit typec_exit(void)
1650 class_destroy(typec_class);
1651 ida_destroy(&typec_index_ida);
1652 bus_unregister(&typec_bus);
1654 module_exit(typec_exit);
1656 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1657 MODULE_LICENSE("GPL v2");
1658 MODULE_DESCRIPTION("USB Type-C Connector Class");