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/property.h>
13 #include <linux/slab.h>
19 enum typec_plug_index index;
25 enum typec_plug_type type;
26 struct usb_pd_identity *identity;
27 unsigned int active:1;
30 struct typec_partner {
32 unsigned int usb_pd:1;
33 struct usb_pd_identity *identity;
34 enum typec_accessory accessory;
44 enum typec_data_role data_role;
45 enum typec_role pwr_role;
46 enum typec_role vconn_role;
47 enum typec_pwr_opmode pwr_opmode;
48 enum typec_port_type port_type;
49 struct mutex port_type_lock;
51 enum typec_orientation orientation;
52 struct typec_switch *sw;
53 struct typec_mux *mux;
55 const struct typec_capability *cap;
56 const struct typec_operations *ops;
59 #define to_typec_port(_dev_) container_of(_dev_, struct typec_port, dev)
60 #define to_typec_plug(_dev_) container_of(_dev_, struct typec_plug, dev)
61 #define to_typec_cable(_dev_) container_of(_dev_, struct typec_cable, dev)
62 #define to_typec_partner(_dev_) container_of(_dev_, struct typec_partner, dev)
64 static const struct device_type typec_partner_dev_type;
65 static const struct device_type typec_cable_dev_type;
66 static const struct device_type typec_plug_dev_type;
68 #define is_typec_partner(_dev_) (_dev_->type == &typec_partner_dev_type)
69 #define is_typec_cable(_dev_) (_dev_->type == &typec_cable_dev_type)
70 #define is_typec_plug(_dev_) (_dev_->type == &typec_plug_dev_type)
72 static DEFINE_IDA(typec_index_ida);
73 static struct class *typec_class;
75 /* ------------------------------------------------------------------------- */
76 /* Common attributes */
78 static const char * const typec_accessory_modes[] = {
79 [TYPEC_ACCESSORY_NONE] = "none",
80 [TYPEC_ACCESSORY_AUDIO] = "analog_audio",
81 [TYPEC_ACCESSORY_DEBUG] = "debug",
84 static struct usb_pd_identity *get_pd_identity(struct device *dev)
86 if (is_typec_partner(dev)) {
87 struct typec_partner *partner = to_typec_partner(dev);
89 return partner->identity;
90 } else if (is_typec_cable(dev)) {
91 struct typec_cable *cable = to_typec_cable(dev);
93 return cable->identity;
98 static ssize_t id_header_show(struct device *dev, struct device_attribute *attr,
101 struct usb_pd_identity *id = get_pd_identity(dev);
103 return sprintf(buf, "0x%08x\n", id->id_header);
105 static DEVICE_ATTR_RO(id_header);
107 static ssize_t cert_stat_show(struct device *dev, struct device_attribute *attr,
110 struct usb_pd_identity *id = get_pd_identity(dev);
112 return sprintf(buf, "0x%08x\n", id->cert_stat);
114 static DEVICE_ATTR_RO(cert_stat);
116 static ssize_t product_show(struct device *dev, struct device_attribute *attr,
119 struct usb_pd_identity *id = get_pd_identity(dev);
121 return sprintf(buf, "0x%08x\n", id->product);
123 static DEVICE_ATTR_RO(product);
125 static struct attribute *usb_pd_id_attrs[] = {
126 &dev_attr_id_header.attr,
127 &dev_attr_cert_stat.attr,
128 &dev_attr_product.attr,
132 static const struct attribute_group usb_pd_id_group = {
134 .attrs = usb_pd_id_attrs,
137 static const struct attribute_group *usb_pd_id_groups[] = {
142 static void typec_report_identity(struct device *dev)
144 sysfs_notify(&dev->kobj, "identity", "id_header");
145 sysfs_notify(&dev->kobj, "identity", "cert_stat");
146 sysfs_notify(&dev->kobj, "identity", "product");
149 /* ------------------------------------------------------------------------- */
150 /* Alternate Modes */
152 static int altmode_match(struct device *dev, void *data)
154 struct typec_altmode *adev = to_typec_altmode(dev);
155 struct typec_device_id *id = data;
157 if (!is_typec_altmode(dev))
160 return ((adev->svid == id->svid) && (adev->mode == id->mode));
163 static void typec_altmode_set_partner(struct altmode *altmode)
165 struct typec_altmode *adev = &altmode->adev;
166 struct typec_device_id id = { adev->svid, adev->mode, };
167 struct typec_port *port = typec_altmode2port(adev);
168 struct altmode *partner;
171 dev = device_find_child(&port->dev, &id, altmode_match);
175 /* Bind the port alt mode to the partner/plug alt mode. */
176 partner = to_altmode(to_typec_altmode(dev));
177 altmode->partner = partner;
179 /* Bind the partner/plug alt mode to the port alt mode. */
180 if (is_typec_plug(adev->dev.parent)) {
181 struct typec_plug *plug = to_typec_plug(adev->dev.parent);
183 partner->plug[plug->index] = altmode;
185 partner->partner = altmode;
189 static void typec_altmode_put_partner(struct altmode *altmode)
191 struct altmode *partner = altmode->partner;
192 struct typec_altmode *adev;
197 adev = &partner->adev;
199 if (is_typec_plug(adev->dev.parent)) {
200 struct typec_plug *plug = to_typec_plug(adev->dev.parent);
202 partner->plug[plug->index] = NULL;
204 partner->partner = NULL;
206 put_device(&adev->dev);
209 static void *typec_port_match(struct device_connection *con, int ep, void *data)
214 * FIXME: Check does the fwnode supports the requested SVID. If it does
215 * we need to return ERR_PTR(-PROBE_DEFER) when there is no device.
218 return class_find_device_by_fwnode(typec_class, con->fwnode);
220 dev = class_find_device_by_name(typec_class, con->endpoint[ep]);
222 return dev ? dev : ERR_PTR(-EPROBE_DEFER);
225 struct typec_altmode *
226 typec_altmode_register_notifier(struct device *dev, u16 svid, u8 mode,
227 struct notifier_block *nb)
229 struct typec_device_id id = { svid, mode, };
230 struct device *altmode_dev;
231 struct device *port_dev;
232 struct altmode *altmode;
235 /* Find the port linked to the caller */
236 port_dev = device_connection_find_match(dev, NULL, NULL,
238 if (IS_ERR_OR_NULL(port_dev))
239 return port_dev ? ERR_CAST(port_dev) : ERR_PTR(-ENODEV);
241 /* Find the altmode with matching svid */
242 altmode_dev = device_find_child(port_dev, &id, altmode_match);
244 put_device(port_dev);
247 return ERR_PTR(-ENODEV);
249 altmode = to_altmode(to_typec_altmode(altmode_dev));
251 /* Register notifier */
252 ret = blocking_notifier_chain_register(&altmode->nh, nb);
254 put_device(altmode_dev);
258 return &altmode->adev;
260 EXPORT_SYMBOL_GPL(typec_altmode_register_notifier);
262 void typec_altmode_unregister_notifier(struct typec_altmode *adev,
263 struct notifier_block *nb)
265 struct altmode *altmode = to_altmode(adev);
267 blocking_notifier_chain_unregister(&altmode->nh, nb);
268 put_device(&adev->dev);
270 EXPORT_SYMBOL_GPL(typec_altmode_unregister_notifier);
273 * typec_altmode_update_active - Report Enter/Exit mode
274 * @adev: Handle to the alternate mode
275 * @active: True when the mode has been entered
277 * If a partner or cable plug executes Enter/Exit Mode command successfully, the
278 * drivers use this routine to report the updated state of the mode.
280 void typec_altmode_update_active(struct typec_altmode *adev, bool active)
284 if (adev->active == active)
287 if (!is_typec_port(adev->dev.parent) && adev->dev.driver) {
289 module_put(adev->dev.driver->owner);
291 WARN_ON(!try_module_get(adev->dev.driver->owner));
294 adev->active = active;
295 snprintf(dir, sizeof(dir), "mode%d", adev->mode);
296 sysfs_notify(&adev->dev.kobj, dir, "active");
297 sysfs_notify(&adev->dev.kobj, NULL, "active");
298 kobject_uevent(&adev->dev.kobj, KOBJ_CHANGE);
300 EXPORT_SYMBOL_GPL(typec_altmode_update_active);
303 * typec_altmode2port - Alternate Mode to USB Type-C port
304 * @alt: The Alternate Mode
306 * Returns handle to the port that a cable plug or partner with @alt is
309 struct typec_port *typec_altmode2port(struct typec_altmode *alt)
311 if (is_typec_plug(alt->dev.parent))
312 return to_typec_port(alt->dev.parent->parent->parent);
313 if (is_typec_partner(alt->dev.parent))
314 return to_typec_port(alt->dev.parent->parent);
315 if (is_typec_port(alt->dev.parent))
316 return to_typec_port(alt->dev.parent);
320 EXPORT_SYMBOL_GPL(typec_altmode2port);
323 vdo_show(struct device *dev, struct device_attribute *attr, char *buf)
325 struct typec_altmode *alt = to_typec_altmode(dev);
327 return sprintf(buf, "0x%08x\n", alt->vdo);
329 static DEVICE_ATTR_RO(vdo);
332 description_show(struct device *dev, struct device_attribute *attr, char *buf)
334 struct typec_altmode *alt = to_typec_altmode(dev);
336 return sprintf(buf, "%s\n", alt->desc ? alt->desc : "");
338 static DEVICE_ATTR_RO(description);
341 active_show(struct device *dev, struct device_attribute *attr, char *buf)
343 struct typec_altmode *alt = to_typec_altmode(dev);
345 return sprintf(buf, "%s\n", alt->active ? "yes" : "no");
348 static ssize_t active_store(struct device *dev, struct device_attribute *attr,
349 const char *buf, size_t size)
351 struct typec_altmode *adev = to_typec_altmode(dev);
352 struct altmode *altmode = to_altmode(adev);
356 ret = kstrtobool(buf, &enter);
360 if (adev->active == enter)
363 if (is_typec_port(adev->dev.parent)) {
364 typec_altmode_update_active(adev, enter);
366 /* Make sure that the partner exits the mode before disabling */
367 if (altmode->partner && !enter && altmode->partner->adev.active)
368 typec_altmode_exit(&altmode->partner->adev);
369 } else if (altmode->partner) {
370 if (enter && !altmode->partner->adev.active) {
371 dev_warn(dev, "port has the mode disabled\n");
376 /* Note: If there is no driver, the mode will not be entered */
377 if (adev->ops && adev->ops->activate) {
378 ret = adev->ops->activate(adev, enter);
385 static DEVICE_ATTR_RW(active);
388 supported_roles_show(struct device *dev, struct device_attribute *attr,
391 struct altmode *alt = to_altmode(to_typec_altmode(dev));
394 switch (alt->roles) {
396 ret = sprintf(buf, "source\n");
399 ret = sprintf(buf, "sink\n");
403 ret = sprintf(buf, "source sink\n");
408 static DEVICE_ATTR_RO(supported_roles);
411 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
413 struct typec_altmode *adev = to_typec_altmode(dev);
415 return sprintf(buf, "%u\n", adev->mode);
417 static DEVICE_ATTR_RO(mode);
420 svid_show(struct device *dev, struct device_attribute *attr, char *buf)
422 struct typec_altmode *adev = to_typec_altmode(dev);
424 return sprintf(buf, "%04x\n", adev->svid);
426 static DEVICE_ATTR_RO(svid);
428 static struct attribute *typec_altmode_attrs[] = {
429 &dev_attr_active.attr,
435 ATTRIBUTE_GROUPS(typec_altmode);
437 static int altmode_id_get(struct device *dev)
441 if (is_typec_partner(dev))
442 ids = &to_typec_partner(dev)->mode_ids;
443 else if (is_typec_plug(dev))
444 ids = &to_typec_plug(dev)->mode_ids;
446 ids = &to_typec_port(dev)->mode_ids;
448 return ida_simple_get(ids, 0, 0, GFP_KERNEL);
451 static void altmode_id_remove(struct device *dev, int id)
455 if (is_typec_partner(dev))
456 ids = &to_typec_partner(dev)->mode_ids;
457 else if (is_typec_plug(dev))
458 ids = &to_typec_plug(dev)->mode_ids;
460 ids = &to_typec_port(dev)->mode_ids;
462 ida_simple_remove(ids, id);
465 static void typec_altmode_release(struct device *dev)
467 struct altmode *alt = to_altmode(to_typec_altmode(dev));
469 typec_altmode_put_partner(alt);
471 altmode_id_remove(alt->adev.dev.parent, alt->id);
475 const struct device_type typec_altmode_dev_type = {
476 .name = "typec_alternate_mode",
477 .groups = typec_altmode_groups,
478 .release = typec_altmode_release,
481 static struct typec_altmode *
482 typec_register_altmode(struct device *parent,
483 const struct typec_altmode_desc *desc)
485 unsigned int id = altmode_id_get(parent);
486 bool is_port = is_typec_port(parent);
490 alt = kzalloc(sizeof(*alt), GFP_KERNEL);
492 return ERR_PTR(-ENOMEM);
494 alt->adev.svid = desc->svid;
495 alt->adev.mode = desc->mode;
496 alt->adev.vdo = desc->vdo;
497 alt->roles = desc->roles;
500 alt->attrs[0] = &dev_attr_vdo.attr;
501 alt->attrs[1] = &dev_attr_description.attr;
502 alt->attrs[2] = &dev_attr_active.attr;
505 alt->attrs[3] = &dev_attr_supported_roles.attr;
506 alt->adev.active = true; /* Enabled by default */
509 sprintf(alt->group_name, "mode%d", desc->mode);
510 alt->group.name = alt->group_name;
511 alt->group.attrs = alt->attrs;
512 alt->groups[0] = &alt->group;
514 alt->adev.dev.parent = parent;
515 alt->adev.dev.groups = alt->groups;
516 alt->adev.dev.type = &typec_altmode_dev_type;
517 dev_set_name(&alt->adev.dev, "%s.%u", dev_name(parent), id);
519 /* Link partners and plugs with the ports */
521 BLOCKING_INIT_NOTIFIER_HEAD(&alt->nh);
523 typec_altmode_set_partner(alt);
525 /* The partners are bind to drivers */
526 if (is_typec_partner(parent))
527 alt->adev.dev.bus = &typec_bus;
529 ret = device_register(&alt->adev.dev);
531 dev_err(parent, "failed to register alternate mode (%d)\n",
533 put_device(&alt->adev.dev);
541 * typec_unregister_altmode - Unregister Alternate Mode
542 * @adev: The alternate mode to be unregistered
544 * Unregister device created with typec_partner_register_altmode(),
545 * typec_plug_register_altmode() or typec_port_register_altmode().
547 void typec_unregister_altmode(struct typec_altmode *adev)
549 if (IS_ERR_OR_NULL(adev))
551 typec_mux_put(to_altmode(adev)->mux);
552 device_unregister(&adev->dev);
554 EXPORT_SYMBOL_GPL(typec_unregister_altmode);
556 /* ------------------------------------------------------------------------- */
557 /* Type-C Partners */
559 static ssize_t accessory_mode_show(struct device *dev,
560 struct device_attribute *attr,
563 struct typec_partner *p = to_typec_partner(dev);
565 return sprintf(buf, "%s\n", typec_accessory_modes[p->accessory]);
567 static DEVICE_ATTR_RO(accessory_mode);
569 static ssize_t supports_usb_power_delivery_show(struct device *dev,
570 struct device_attribute *attr,
573 struct typec_partner *p = to_typec_partner(dev);
575 return sprintf(buf, "%s\n", p->usb_pd ? "yes" : "no");
577 static DEVICE_ATTR_RO(supports_usb_power_delivery);
579 static struct attribute *typec_partner_attrs[] = {
580 &dev_attr_accessory_mode.attr,
581 &dev_attr_supports_usb_power_delivery.attr,
584 ATTRIBUTE_GROUPS(typec_partner);
586 static void typec_partner_release(struct device *dev)
588 struct typec_partner *partner = to_typec_partner(dev);
590 ida_destroy(&partner->mode_ids);
594 static const struct device_type typec_partner_dev_type = {
595 .name = "typec_partner",
596 .groups = typec_partner_groups,
597 .release = typec_partner_release,
601 * typec_partner_set_identity - Report result from Discover Identity command
602 * @partner: The partner updated identity values
604 * This routine is used to report that the result of Discover Identity USB power
605 * delivery command has become available.
607 int typec_partner_set_identity(struct typec_partner *partner)
609 if (!partner->identity)
612 typec_report_identity(&partner->dev);
615 EXPORT_SYMBOL_GPL(typec_partner_set_identity);
618 * typec_partner_register_altmode - Register USB Type-C Partner Alternate Mode
619 * @partner: USB Type-C Partner that supports the alternate mode
620 * @desc: Description of the alternate mode
622 * This routine is used to register each alternate mode individually that
623 * @partner has listed in response to Discover SVIDs command. The modes for a
624 * SVID listed in response to Discover Modes command need to be listed in an
627 * Returns handle to the alternate mode on success or NULL on failure.
629 struct typec_altmode *
630 typec_partner_register_altmode(struct typec_partner *partner,
631 const struct typec_altmode_desc *desc)
633 return typec_register_altmode(&partner->dev, desc);
635 EXPORT_SYMBOL_GPL(typec_partner_register_altmode);
638 * typec_register_partner - Register a USB Type-C Partner
639 * @port: The USB Type-C Port the partner is connected to
640 * @desc: Description of the partner
642 * Registers a device for USB Type-C Partner described in @desc.
644 * Returns handle to the partner on success or ERR_PTR on failure.
646 struct typec_partner *typec_register_partner(struct typec_port *port,
647 struct typec_partner_desc *desc)
649 struct typec_partner *partner;
652 partner = kzalloc(sizeof(*partner), GFP_KERNEL);
654 return ERR_PTR(-ENOMEM);
656 ida_init(&partner->mode_ids);
657 partner->usb_pd = desc->usb_pd;
658 partner->accessory = desc->accessory;
660 if (desc->identity) {
662 * Creating directory for the identity only if the driver is
663 * able to provide data to it.
665 partner->dev.groups = usb_pd_id_groups;
666 partner->identity = desc->identity;
669 partner->dev.class = typec_class;
670 partner->dev.parent = &port->dev;
671 partner->dev.type = &typec_partner_dev_type;
672 dev_set_name(&partner->dev, "%s-partner", dev_name(&port->dev));
674 ret = device_register(&partner->dev);
676 dev_err(&port->dev, "failed to register partner (%d)\n", ret);
677 put_device(&partner->dev);
683 EXPORT_SYMBOL_GPL(typec_register_partner);
686 * typec_unregister_partner - Unregister a USB Type-C Partner
687 * @partner: The partner to be unregistered
689 * Unregister device created with typec_register_partner().
691 void typec_unregister_partner(struct typec_partner *partner)
693 if (!IS_ERR_OR_NULL(partner))
694 device_unregister(&partner->dev);
696 EXPORT_SYMBOL_GPL(typec_unregister_partner);
698 /* ------------------------------------------------------------------------- */
699 /* Type-C Cable Plugs */
701 static void typec_plug_release(struct device *dev)
703 struct typec_plug *plug = to_typec_plug(dev);
705 ida_destroy(&plug->mode_ids);
709 static const struct device_type typec_plug_dev_type = {
710 .name = "typec_plug",
711 .release = typec_plug_release,
715 * typec_plug_register_altmode - Register USB Type-C Cable Plug Alternate Mode
716 * @plug: USB Type-C Cable Plug that supports the alternate mode
717 * @desc: Description of the alternate mode
719 * This routine is used to register each alternate mode individually that @plug
720 * has listed in response to Discover SVIDs command. The modes for a SVID that
721 * the plug lists in response to Discover Modes command need to be listed in an
724 * Returns handle to the alternate mode on success or ERR_PTR on failure.
726 struct typec_altmode *
727 typec_plug_register_altmode(struct typec_plug *plug,
728 const struct typec_altmode_desc *desc)
730 return typec_register_altmode(&plug->dev, desc);
732 EXPORT_SYMBOL_GPL(typec_plug_register_altmode);
735 * typec_register_plug - Register a USB Type-C Cable Plug
736 * @cable: USB Type-C Cable with the plug
737 * @desc: Description of the cable plug
739 * Registers a device for USB Type-C Cable Plug described in @desc. A USB Type-C
740 * Cable Plug represents a plug with electronics in it that can response to USB
741 * Power Delivery SOP Prime or SOP Double Prime packages.
743 * Returns handle to the cable plug on success or ERR_PTR on failure.
745 struct typec_plug *typec_register_plug(struct typec_cable *cable,
746 struct typec_plug_desc *desc)
748 struct typec_plug *plug;
752 plug = kzalloc(sizeof(*plug), GFP_KERNEL);
754 return ERR_PTR(-ENOMEM);
756 sprintf(name, "plug%d", desc->index);
758 ida_init(&plug->mode_ids);
759 plug->index = desc->index;
760 plug->dev.class = typec_class;
761 plug->dev.parent = &cable->dev;
762 plug->dev.type = &typec_plug_dev_type;
763 dev_set_name(&plug->dev, "%s-%s", dev_name(cable->dev.parent), name);
765 ret = device_register(&plug->dev);
767 dev_err(&cable->dev, "failed to register plug (%d)\n", ret);
768 put_device(&plug->dev);
774 EXPORT_SYMBOL_GPL(typec_register_plug);
777 * typec_unregister_plug - Unregister a USB Type-C Cable Plug
778 * @plug: The cable plug to be unregistered
780 * Unregister device created with typec_register_plug().
782 void typec_unregister_plug(struct typec_plug *plug)
784 if (!IS_ERR_OR_NULL(plug))
785 device_unregister(&plug->dev);
787 EXPORT_SYMBOL_GPL(typec_unregister_plug);
792 type_show(struct device *dev, struct device_attribute *attr, char *buf)
794 struct typec_cable *cable = to_typec_cable(dev);
796 return sprintf(buf, "%s\n", cable->active ? "active" : "passive");
798 static DEVICE_ATTR_RO(type);
800 static const char * const typec_plug_types[] = {
801 [USB_PLUG_NONE] = "unknown",
802 [USB_PLUG_TYPE_A] = "type-a",
803 [USB_PLUG_TYPE_B] = "type-b",
804 [USB_PLUG_TYPE_C] = "type-c",
805 [USB_PLUG_CAPTIVE] = "captive",
808 static ssize_t plug_type_show(struct device *dev,
809 struct device_attribute *attr, char *buf)
811 struct typec_cable *cable = to_typec_cable(dev);
813 return sprintf(buf, "%s\n", typec_plug_types[cable->type]);
815 static DEVICE_ATTR_RO(plug_type);
817 static struct attribute *typec_cable_attrs[] = {
819 &dev_attr_plug_type.attr,
822 ATTRIBUTE_GROUPS(typec_cable);
824 static void typec_cable_release(struct device *dev)
826 struct typec_cable *cable = to_typec_cable(dev);
831 static const struct device_type typec_cable_dev_type = {
832 .name = "typec_cable",
833 .groups = typec_cable_groups,
834 .release = typec_cable_release,
838 * typec_cable_set_identity - Report result from Discover Identity command
839 * @cable: The cable updated identity values
841 * This routine is used to report that the result of Discover Identity USB power
842 * delivery command has become available.
844 int typec_cable_set_identity(struct typec_cable *cable)
846 if (!cable->identity)
849 typec_report_identity(&cable->dev);
852 EXPORT_SYMBOL_GPL(typec_cable_set_identity);
855 * typec_register_cable - Register a USB Type-C Cable
856 * @port: The USB Type-C Port the cable is connected to
857 * @desc: Description of the cable
859 * Registers a device for USB Type-C Cable described in @desc. The cable will be
860 * parent for the optional cable plug devises.
862 * Returns handle to the cable on success or ERR_PTR on failure.
864 struct typec_cable *typec_register_cable(struct typec_port *port,
865 struct typec_cable_desc *desc)
867 struct typec_cable *cable;
870 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
872 return ERR_PTR(-ENOMEM);
874 cable->type = desc->type;
875 cable->active = desc->active;
877 if (desc->identity) {
879 * Creating directory for the identity only if the driver is
880 * able to provide data to it.
882 cable->dev.groups = usb_pd_id_groups;
883 cable->identity = desc->identity;
886 cable->dev.class = typec_class;
887 cable->dev.parent = &port->dev;
888 cable->dev.type = &typec_cable_dev_type;
889 dev_set_name(&cable->dev, "%s-cable", dev_name(&port->dev));
891 ret = device_register(&cable->dev);
893 dev_err(&port->dev, "failed to register cable (%d)\n", ret);
894 put_device(&cable->dev);
900 EXPORT_SYMBOL_GPL(typec_register_cable);
903 * typec_unregister_cable - Unregister a USB Type-C Cable
904 * @cable: The cable to be unregistered
906 * Unregister device created with typec_register_cable().
908 void typec_unregister_cable(struct typec_cable *cable)
910 if (!IS_ERR_OR_NULL(cable))
911 device_unregister(&cable->dev);
913 EXPORT_SYMBOL_GPL(typec_unregister_cable);
915 /* ------------------------------------------------------------------------- */
916 /* USB Type-C ports */
918 static const char * const typec_roles[] = {
919 [TYPEC_SINK] = "sink",
920 [TYPEC_SOURCE] = "source",
923 static const char * const typec_data_roles[] = {
924 [TYPEC_DEVICE] = "device",
925 [TYPEC_HOST] = "host",
928 static const char * const typec_port_power_roles[] = {
929 [TYPEC_PORT_SRC] = "source",
930 [TYPEC_PORT_SNK] = "sink",
931 [TYPEC_PORT_DRP] = "dual",
934 static const char * const typec_port_data_roles[] = {
935 [TYPEC_PORT_DFP] = "host",
936 [TYPEC_PORT_UFP] = "device",
937 [TYPEC_PORT_DRD] = "dual",
940 static const char * const typec_port_types_drp[] = {
941 [TYPEC_PORT_SRC] = "dual [source] sink",
942 [TYPEC_PORT_SNK] = "dual source [sink]",
943 [TYPEC_PORT_DRP] = "[dual] source sink",
947 preferred_role_store(struct device *dev, struct device_attribute *attr,
948 const char *buf, size_t size)
950 struct typec_port *port = to_typec_port(dev);
954 if (port->cap->type != TYPEC_PORT_DRP) {
955 dev_dbg(dev, "Preferred role only supported with DRP ports\n");
959 if (!port->ops || !port->ops->try_role) {
960 dev_dbg(dev, "Setting preferred role not supported\n");
964 role = sysfs_match_string(typec_roles, buf);
966 if (sysfs_streq(buf, "none"))
967 role = TYPEC_NO_PREFERRED_ROLE;
972 ret = port->ops->try_role(port, role);
976 port->prefer_role = role;
981 preferred_role_show(struct device *dev, struct device_attribute *attr,
984 struct typec_port *port = to_typec_port(dev);
986 if (port->cap->type != TYPEC_PORT_DRP)
989 if (port->prefer_role < 0)
992 return sprintf(buf, "%s\n", typec_roles[port->prefer_role]);
994 static DEVICE_ATTR_RW(preferred_role);
996 static ssize_t data_role_store(struct device *dev,
997 struct device_attribute *attr,
998 const char *buf, size_t size)
1000 struct typec_port *port = to_typec_port(dev);
1003 if (!port->ops || !port->ops->dr_set) {
1004 dev_dbg(dev, "data role swapping not supported\n");
1008 ret = sysfs_match_string(typec_data_roles, buf);
1012 mutex_lock(&port->port_type_lock);
1013 if (port->cap->data != TYPEC_PORT_DRD) {
1015 goto unlock_and_ret;
1018 ret = port->ops->dr_set(port, ret);
1020 goto unlock_and_ret;
1024 mutex_unlock(&port->port_type_lock);
1028 static ssize_t data_role_show(struct device *dev,
1029 struct device_attribute *attr, char *buf)
1031 struct typec_port *port = to_typec_port(dev);
1033 if (port->cap->data == TYPEC_PORT_DRD)
1034 return sprintf(buf, "%s\n", port->data_role == TYPEC_HOST ?
1035 "[host] device" : "host [device]");
1037 return sprintf(buf, "[%s]\n", typec_data_roles[port->data_role]);
1039 static DEVICE_ATTR_RW(data_role);
1041 static ssize_t power_role_store(struct device *dev,
1042 struct device_attribute *attr,
1043 const char *buf, size_t size)
1045 struct typec_port *port = to_typec_port(dev);
1048 if (!port->cap->pd_revision) {
1049 dev_dbg(dev, "USB Power Delivery not supported\n");
1053 if (!port->ops || !port->ops->pr_set) {
1054 dev_dbg(dev, "power role swapping not supported\n");
1058 if (port->pwr_opmode != TYPEC_PWR_MODE_PD) {
1059 dev_dbg(dev, "partner unable to swap power role\n");
1063 ret = sysfs_match_string(typec_roles, buf);
1067 mutex_lock(&port->port_type_lock);
1068 if (port->port_type != TYPEC_PORT_DRP) {
1069 dev_dbg(dev, "port type fixed at \"%s\"",
1070 typec_port_power_roles[port->port_type]);
1072 goto unlock_and_ret;
1075 ret = port->ops->pr_set(port, ret);
1077 goto unlock_and_ret;
1081 mutex_unlock(&port->port_type_lock);
1085 static ssize_t power_role_show(struct device *dev,
1086 struct device_attribute *attr, char *buf)
1088 struct typec_port *port = to_typec_port(dev);
1090 if (port->cap->type == TYPEC_PORT_DRP)
1091 return sprintf(buf, "%s\n", port->pwr_role == TYPEC_SOURCE ?
1092 "[source] sink" : "source [sink]");
1094 return sprintf(buf, "[%s]\n", typec_roles[port->pwr_role]);
1096 static DEVICE_ATTR_RW(power_role);
1099 port_type_store(struct device *dev, struct device_attribute *attr,
1100 const char *buf, size_t size)
1102 struct typec_port *port = to_typec_port(dev);
1104 enum typec_port_type type;
1106 if (port->cap->type != TYPEC_PORT_DRP ||
1107 !port->ops || !port->ops->port_type_set) {
1108 dev_dbg(dev, "changing port type not supported\n");
1112 ret = sysfs_match_string(typec_port_power_roles, buf);
1117 mutex_lock(&port->port_type_lock);
1119 if (port->port_type == type) {
1121 goto unlock_and_ret;
1124 ret = port->ops->port_type_set(port, type);
1126 goto unlock_and_ret;
1128 port->port_type = type;
1132 mutex_unlock(&port->port_type_lock);
1137 port_type_show(struct device *dev, struct device_attribute *attr,
1140 struct typec_port *port = to_typec_port(dev);
1142 if (port->cap->type == TYPEC_PORT_DRP)
1143 return sprintf(buf, "%s\n",
1144 typec_port_types_drp[port->port_type]);
1146 return sprintf(buf, "[%s]\n", typec_port_power_roles[port->cap->type]);
1148 static DEVICE_ATTR_RW(port_type);
1150 static const char * const typec_pwr_opmodes[] = {
1151 [TYPEC_PWR_MODE_USB] = "default",
1152 [TYPEC_PWR_MODE_1_5A] = "1.5A",
1153 [TYPEC_PWR_MODE_3_0A] = "3.0A",
1154 [TYPEC_PWR_MODE_PD] = "usb_power_delivery",
1157 static ssize_t power_operation_mode_show(struct device *dev,
1158 struct device_attribute *attr,
1161 struct typec_port *port = to_typec_port(dev);
1163 return sprintf(buf, "%s\n", typec_pwr_opmodes[port->pwr_opmode]);
1165 static DEVICE_ATTR_RO(power_operation_mode);
1167 static ssize_t vconn_source_store(struct device *dev,
1168 struct device_attribute *attr,
1169 const char *buf, size_t size)
1171 struct typec_port *port = to_typec_port(dev);
1175 if (!port->cap->pd_revision) {
1176 dev_dbg(dev, "VCONN swap depends on USB Power Delivery\n");
1180 if (!port->ops || !port->ops->vconn_set) {
1181 dev_dbg(dev, "VCONN swapping not supported\n");
1185 ret = kstrtobool(buf, &source);
1189 ret = port->ops->vconn_set(port, (enum typec_role)source);
1196 static ssize_t vconn_source_show(struct device *dev,
1197 struct device_attribute *attr, char *buf)
1199 struct typec_port *port = to_typec_port(dev);
1201 return sprintf(buf, "%s\n",
1202 port->vconn_role == TYPEC_SOURCE ? "yes" : "no");
1204 static DEVICE_ATTR_RW(vconn_source);
1206 static ssize_t supported_accessory_modes_show(struct device *dev,
1207 struct device_attribute *attr,
1210 struct typec_port *port = to_typec_port(dev);
1214 for (i = 0; i < ARRAY_SIZE(port->cap->accessory); i++) {
1215 if (port->cap->accessory[i])
1216 ret += sprintf(buf + ret, "%s ",
1217 typec_accessory_modes[port->cap->accessory[i]]);
1221 return sprintf(buf, "none\n");
1223 buf[ret - 1] = '\n';
1227 static DEVICE_ATTR_RO(supported_accessory_modes);
1229 static ssize_t usb_typec_revision_show(struct device *dev,
1230 struct device_attribute *attr,
1233 struct typec_port *port = to_typec_port(dev);
1234 u16 rev = port->cap->revision;
1236 return sprintf(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
1238 static DEVICE_ATTR_RO(usb_typec_revision);
1240 static ssize_t usb_power_delivery_revision_show(struct device *dev,
1241 struct device_attribute *attr,
1244 struct typec_port *p = to_typec_port(dev);
1246 return sprintf(buf, "%d\n", (p->cap->pd_revision >> 8) & 0xff);
1248 static DEVICE_ATTR_RO(usb_power_delivery_revision);
1250 static struct attribute *typec_attrs[] = {
1251 &dev_attr_data_role.attr,
1252 &dev_attr_power_operation_mode.attr,
1253 &dev_attr_power_role.attr,
1254 &dev_attr_preferred_role.attr,
1255 &dev_attr_supported_accessory_modes.attr,
1256 &dev_attr_usb_power_delivery_revision.attr,
1257 &dev_attr_usb_typec_revision.attr,
1258 &dev_attr_vconn_source.attr,
1259 &dev_attr_port_type.attr,
1262 ATTRIBUTE_GROUPS(typec);
1264 static int typec_uevent(struct device *dev, struct kobj_uevent_env *env)
1268 ret = add_uevent_var(env, "TYPEC_PORT=%s", dev_name(dev));
1270 dev_err(dev, "failed to add uevent TYPEC_PORT\n");
1275 static void typec_release(struct device *dev)
1277 struct typec_port *port = to_typec_port(dev);
1279 ida_simple_remove(&typec_index_ida, port->id);
1280 ida_destroy(&port->mode_ids);
1281 typec_switch_put(port->sw);
1282 typec_mux_put(port->mux);
1287 const struct device_type typec_port_dev_type = {
1288 .name = "typec_port",
1289 .groups = typec_groups,
1290 .uevent = typec_uevent,
1291 .release = typec_release,
1294 /* --------------------------------------- */
1295 /* Driver callbacks to report role updates */
1298 * typec_set_data_role - Report data role change
1299 * @port: The USB Type-C Port where the role was changed
1300 * @role: The new data role
1302 * This routine is used by the port drivers to report data role changes.
1304 void typec_set_data_role(struct typec_port *port, enum typec_data_role role)
1306 if (port->data_role == role)
1309 port->data_role = role;
1310 sysfs_notify(&port->dev.kobj, NULL, "data_role");
1311 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1313 EXPORT_SYMBOL_GPL(typec_set_data_role);
1316 * typec_set_pwr_role - Report power role change
1317 * @port: The USB Type-C Port where the role was changed
1318 * @role: The new data role
1320 * This routine is used by the port drivers to report power role changes.
1322 void typec_set_pwr_role(struct typec_port *port, enum typec_role role)
1324 if (port->pwr_role == role)
1327 port->pwr_role = role;
1328 sysfs_notify(&port->dev.kobj, NULL, "power_role");
1329 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1331 EXPORT_SYMBOL_GPL(typec_set_pwr_role);
1334 * typec_set_vconn_role - Report VCONN source change
1335 * @port: The USB Type-C Port which VCONN role changed
1336 * @role: Source when @port is sourcing VCONN, or Sink when it's not
1338 * This routine is used by the port drivers to report if the VCONN source is
1341 void typec_set_vconn_role(struct typec_port *port, enum typec_role role)
1343 if (port->vconn_role == role)
1346 port->vconn_role = role;
1347 sysfs_notify(&port->dev.kobj, NULL, "vconn_source");
1348 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1350 EXPORT_SYMBOL_GPL(typec_set_vconn_role);
1352 static int partner_match(struct device *dev, void *data)
1354 return is_typec_partner(dev);
1358 * typec_set_pwr_opmode - Report changed power operation mode
1359 * @port: The USB Type-C Port where the mode was changed
1360 * @opmode: New power operation mode
1362 * This routine is used by the port drivers to report changed power operation
1363 * mode in @port. The modes are USB (default), 1.5A, 3.0A as defined in USB
1364 * Type-C specification, and "USB Power Delivery" when the power levels are
1365 * negotiated with methods defined in USB Power Delivery specification.
1367 void typec_set_pwr_opmode(struct typec_port *port,
1368 enum typec_pwr_opmode opmode)
1370 struct device *partner_dev;
1372 if (port->pwr_opmode == opmode)
1375 port->pwr_opmode = opmode;
1376 sysfs_notify(&port->dev.kobj, NULL, "power_operation_mode");
1377 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1379 partner_dev = device_find_child(&port->dev, NULL, partner_match);
1381 struct typec_partner *partner = to_typec_partner(partner_dev);
1383 if (opmode == TYPEC_PWR_MODE_PD && !partner->usb_pd) {
1384 partner->usb_pd = 1;
1385 sysfs_notify(&partner_dev->kobj, NULL,
1386 "supports_usb_power_delivery");
1388 put_device(partner_dev);
1391 EXPORT_SYMBOL_GPL(typec_set_pwr_opmode);
1394 * typec_find_port_power_role - Get the typec port power capability
1395 * @name: port power capability string
1397 * This routine is used to find the typec_port_type by its string name.
1399 * Returns typec_port_type if success, otherwise negative error code.
1401 int typec_find_port_power_role(const char *name)
1403 return match_string(typec_port_power_roles,
1404 ARRAY_SIZE(typec_port_power_roles), name);
1406 EXPORT_SYMBOL_GPL(typec_find_port_power_role);
1409 * typec_find_power_role - Find the typec one specific power role
1410 * @name: power role string
1412 * This routine is used to find the typec_role by its string name.
1414 * Returns typec_role if success, otherwise negative error code.
1416 int typec_find_power_role(const char *name)
1418 return match_string(typec_roles, ARRAY_SIZE(typec_roles), name);
1420 EXPORT_SYMBOL_GPL(typec_find_power_role);
1423 * typec_find_port_data_role - Get the typec port data capability
1424 * @name: port data capability string
1426 * This routine is used to find the typec_port_data by its string name.
1428 * Returns typec_port_data if success, otherwise negative error code.
1430 int typec_find_port_data_role(const char *name)
1432 return match_string(typec_port_data_roles,
1433 ARRAY_SIZE(typec_port_data_roles), name);
1435 EXPORT_SYMBOL_GPL(typec_find_port_data_role);
1437 /* ------------------------------------------ */
1438 /* API for Multiplexer/DeMultiplexer Switches */
1441 * typec_set_orientation - Set USB Type-C cable plug orientation
1442 * @port: USB Type-C Port
1443 * @orientation: USB Type-C cable plug orientation
1445 * Set cable plug orientation for @port.
1447 int typec_set_orientation(struct typec_port *port,
1448 enum typec_orientation orientation)
1453 ret = port->sw->set(port->sw, orientation);
1458 port->orientation = orientation;
1462 EXPORT_SYMBOL_GPL(typec_set_orientation);
1465 * typec_get_orientation - Get USB Type-C cable plug orientation
1466 * @port: USB Type-C Port
1468 * Get current cable plug orientation for @port.
1470 enum typec_orientation typec_get_orientation(struct typec_port *port)
1472 return port->orientation;
1474 EXPORT_SYMBOL_GPL(typec_get_orientation);
1477 * typec_set_mode - Set mode of operation for USB Type-C connector
1478 * @port: USB Type-C connector
1479 * @mode: Accessory Mode, USB Operation or Safe State
1481 * Configure @port for Accessory Mode @mode. This function will configure the
1482 * muxes needed for @mode.
1484 int typec_set_mode(struct typec_port *port, int mode)
1486 return port->mux ? port->mux->set(port->mux, mode) : 0;
1488 EXPORT_SYMBOL_GPL(typec_set_mode);
1490 /* --------------------------------------- */
1493 * typec_get_drvdata - Return private driver data pointer
1494 * @port: USB Type-C port
1496 void *typec_get_drvdata(struct typec_port *port)
1498 return dev_get_drvdata(&port->dev);
1500 EXPORT_SYMBOL_GPL(typec_get_drvdata);
1503 * typec_port_register_altmode - Register USB Type-C Port Alternate Mode
1504 * @port: USB Type-C Port that supports the alternate mode
1505 * @desc: Description of the alternate mode
1507 * This routine is used to register an alternate mode that @port is capable of
1510 * Returns handle to the alternate mode on success or ERR_PTR on failure.
1512 struct typec_altmode *
1513 typec_port_register_altmode(struct typec_port *port,
1514 const struct typec_altmode_desc *desc)
1516 struct typec_altmode *adev;
1517 struct typec_mux *mux;
1519 mux = typec_mux_get(&port->dev, desc);
1521 return ERR_CAST(mux);
1523 adev = typec_register_altmode(&port->dev, desc);
1527 to_altmode(adev)->mux = mux;
1531 EXPORT_SYMBOL_GPL(typec_port_register_altmode);
1534 * typec_register_port - Register a USB Type-C Port
1535 * @parent: Parent device
1536 * @cap: Description of the port
1538 * Registers a device for USB Type-C Port described in @cap.
1540 * Returns handle to the port on success or ERR_PTR on failure.
1542 struct typec_port *typec_register_port(struct device *parent,
1543 const struct typec_capability *cap)
1545 struct typec_port *port;
1549 port = kzalloc(sizeof(*port), GFP_KERNEL);
1551 return ERR_PTR(-ENOMEM);
1553 id = ida_simple_get(&typec_index_ida, 0, 0, GFP_KERNEL);
1559 switch (cap->type) {
1560 case TYPEC_PORT_SRC:
1561 port->pwr_role = TYPEC_SOURCE;
1562 port->vconn_role = TYPEC_SOURCE;
1564 case TYPEC_PORT_SNK:
1565 port->pwr_role = TYPEC_SINK;
1566 port->vconn_role = TYPEC_SINK;
1568 case TYPEC_PORT_DRP:
1569 if (cap->prefer_role != TYPEC_NO_PREFERRED_ROLE)
1570 port->pwr_role = cap->prefer_role;
1572 port->pwr_role = TYPEC_SINK;
1576 switch (cap->data) {
1577 case TYPEC_PORT_DFP:
1578 port->data_role = TYPEC_HOST;
1580 case TYPEC_PORT_UFP:
1581 port->data_role = TYPEC_DEVICE;
1583 case TYPEC_PORT_DRD:
1584 if (cap->prefer_role == TYPEC_SOURCE)
1585 port->data_role = TYPEC_HOST;
1587 port->data_role = TYPEC_DEVICE;
1591 ida_init(&port->mode_ids);
1592 mutex_init(&port->port_type_lock);
1595 port->ops = cap->ops;
1596 port->port_type = cap->type;
1597 port->prefer_role = cap->prefer_role;
1599 device_initialize(&port->dev);
1600 port->dev.class = typec_class;
1601 port->dev.parent = parent;
1602 port->dev.fwnode = cap->fwnode;
1603 port->dev.type = &typec_port_dev_type;
1604 dev_set_name(&port->dev, "port%d", id);
1605 dev_set_drvdata(&port->dev, cap->driver_data);
1607 port->cap = kmemdup(cap, sizeof(*cap), GFP_KERNEL);
1609 put_device(&port->dev);
1610 return ERR_PTR(-ENOMEM);
1613 port->sw = typec_switch_get(&port->dev);
1614 if (IS_ERR(port->sw)) {
1615 put_device(&port->dev);
1616 return ERR_CAST(port->sw);
1619 port->mux = typec_mux_get(&port->dev, NULL);
1620 if (IS_ERR(port->mux)) {
1621 put_device(&port->dev);
1622 return ERR_CAST(port->mux);
1625 ret = device_add(&port->dev);
1627 dev_err(parent, "failed to register port (%d)\n", ret);
1628 put_device(&port->dev);
1629 return ERR_PTR(ret);
1634 EXPORT_SYMBOL_GPL(typec_register_port);
1637 * typec_unregister_port - Unregister a USB Type-C Port
1638 * @port: The port to be unregistered
1640 * Unregister device created with typec_register_port().
1642 void typec_unregister_port(struct typec_port *port)
1644 if (!IS_ERR_OR_NULL(port))
1645 device_unregister(&port->dev);
1647 EXPORT_SYMBOL_GPL(typec_unregister_port);
1649 static int __init typec_init(void)
1653 ret = bus_register(&typec_bus);
1657 ret = class_register(&typec_mux_class);
1659 goto err_unregister_bus;
1661 typec_class = class_create(THIS_MODULE, "typec");
1662 if (IS_ERR(typec_class)) {
1663 ret = PTR_ERR(typec_class);
1664 goto err_unregister_mux_class;
1669 err_unregister_mux_class:
1670 class_unregister(&typec_mux_class);
1673 bus_unregister(&typec_bus);
1677 subsys_initcall(typec_init);
1679 static void __exit typec_exit(void)
1681 class_destroy(typec_class);
1682 ida_destroy(&typec_index_ida);
1683 bus_unregister(&typec_bus);
1684 class_unregister(&typec_mux_class);
1686 module_exit(typec_exit);
1688 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1689 MODULE_LICENSE("GPL v2");
1690 MODULE_DESCRIPTION("USB Type-C Connector Class");