1 // SPDX-License-Identifier: GPL-2.0
3 * USB Type-C Connector Class
5 * Copyright (C) 2017, Intel Corporation
9 #include <linux/device.h>
10 #include <linux/module.h>
11 #include <linux/mutex.h>
12 #include <linux/property.h>
13 #include <linux/slab.h>
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;
58 #define to_typec_port(_dev_) container_of(_dev_, struct typec_port, dev)
59 #define to_typec_plug(_dev_) container_of(_dev_, struct typec_plug, dev)
60 #define to_typec_cable(_dev_) container_of(_dev_, struct typec_cable, dev)
61 #define to_typec_partner(_dev_) container_of(_dev_, struct typec_partner, dev)
63 static const struct device_type typec_partner_dev_type;
64 static const struct device_type typec_cable_dev_type;
65 static const struct device_type typec_plug_dev_type;
67 #define is_typec_partner(_dev_) (_dev_->type == &typec_partner_dev_type)
68 #define is_typec_cable(_dev_) (_dev_->type == &typec_cable_dev_type)
69 #define is_typec_plug(_dev_) (_dev_->type == &typec_plug_dev_type)
71 static DEFINE_IDA(typec_index_ida);
72 static struct class *typec_class;
74 /* ------------------------------------------------------------------------- */
75 /* Common attributes */
77 static const char * const typec_accessory_modes[] = {
78 [TYPEC_ACCESSORY_NONE] = "none",
79 [TYPEC_ACCESSORY_AUDIO] = "analog_audio",
80 [TYPEC_ACCESSORY_DEBUG] = "debug",
83 static struct usb_pd_identity *get_pd_identity(struct device *dev)
85 if (is_typec_partner(dev)) {
86 struct typec_partner *partner = to_typec_partner(dev);
88 return partner->identity;
89 } else if (is_typec_cable(dev)) {
90 struct typec_cable *cable = to_typec_cable(dev);
92 return cable->identity;
97 static ssize_t id_header_show(struct device *dev, struct device_attribute *attr,
100 struct usb_pd_identity *id = get_pd_identity(dev);
102 return sprintf(buf, "0x%08x\n", id->id_header);
104 static DEVICE_ATTR_RO(id_header);
106 static ssize_t cert_stat_show(struct device *dev, struct device_attribute *attr,
109 struct usb_pd_identity *id = get_pd_identity(dev);
111 return sprintf(buf, "0x%08x\n", id->cert_stat);
113 static DEVICE_ATTR_RO(cert_stat);
115 static ssize_t product_show(struct device *dev, struct device_attribute *attr,
118 struct usb_pd_identity *id = get_pd_identity(dev);
120 return sprintf(buf, "0x%08x\n", id->product);
122 static DEVICE_ATTR_RO(product);
124 static struct attribute *usb_pd_id_attrs[] = {
125 &dev_attr_id_header.attr,
126 &dev_attr_cert_stat.attr,
127 &dev_attr_product.attr,
131 static const struct attribute_group usb_pd_id_group = {
133 .attrs = usb_pd_id_attrs,
136 static const struct attribute_group *usb_pd_id_groups[] = {
141 static void typec_report_identity(struct device *dev)
143 sysfs_notify(&dev->kobj, "identity", "id_header");
144 sysfs_notify(&dev->kobj, "identity", "cert_stat");
145 sysfs_notify(&dev->kobj, "identity", "product");
148 /* ------------------------------------------------------------------------- */
149 /* Alternate Modes */
151 static int altmode_match(struct device *dev, void *data)
153 struct typec_altmode *adev = to_typec_altmode(dev);
154 struct typec_device_id *id = data;
156 if (!is_typec_altmode(dev))
159 return ((adev->svid == id->svid) && (adev->mode == id->mode));
162 static void typec_altmode_set_partner(struct altmode *altmode)
164 struct typec_altmode *adev = &altmode->adev;
165 struct typec_device_id id = { adev->svid, adev->mode, };
166 struct typec_port *port = typec_altmode2port(adev);
167 struct altmode *partner;
170 dev = device_find_child(&port->dev, &id, altmode_match);
174 /* Bind the port alt mode to the partner/plug alt mode. */
175 partner = to_altmode(to_typec_altmode(dev));
176 altmode->partner = partner;
178 /* Bind the partner/plug alt mode to the port alt mode. */
179 if (is_typec_plug(adev->dev.parent)) {
180 struct typec_plug *plug = to_typec_plug(adev->dev.parent);
182 partner->plug[plug->index] = altmode;
184 partner->partner = altmode;
188 static void typec_altmode_put_partner(struct altmode *altmode)
190 struct altmode *partner = altmode->partner;
191 struct typec_altmode *adev;
196 adev = &partner->adev;
198 if (is_typec_plug(adev->dev.parent)) {
199 struct typec_plug *plug = to_typec_plug(adev->dev.parent);
201 partner->plug[plug->index] = NULL;
203 partner->partner = NULL;
205 put_device(&adev->dev);
208 static int typec_port_fwnode_match(struct device *dev, const void *fwnode)
210 return dev_fwnode(dev) == fwnode;
213 static void *typec_port_match(struct device_connection *con, int ep, void *data)
218 * FIXME: Check does the fwnode supports the requested SVID. If it does
219 * we need to return ERR_PTR(-PROBE_DEFER) when there is no device.
222 return class_find_device(typec_class, NULL, con->fwnode,
223 typec_port_fwnode_match);
225 dev = class_find_device_by_name(typec_class, con->endpoint[ep]);
227 return dev ? dev : ERR_PTR(-EPROBE_DEFER);
230 struct typec_altmode *
231 typec_altmode_register_notifier(struct device *dev, u16 svid, u8 mode,
232 struct notifier_block *nb)
234 struct typec_device_id id = { svid, mode, };
235 struct device *altmode_dev;
236 struct device *port_dev;
237 struct altmode *altmode;
240 /* Find the port linked to the caller */
241 port_dev = device_connection_find_match(dev, NULL, NULL,
243 if (IS_ERR_OR_NULL(port_dev))
244 return port_dev ? ERR_CAST(port_dev) : ERR_PTR(-ENODEV);
246 /* Find the altmode with matching svid */
247 altmode_dev = device_find_child(port_dev, &id, altmode_match);
249 put_device(port_dev);
252 return ERR_PTR(-ENODEV);
254 altmode = to_altmode(to_typec_altmode(altmode_dev));
256 /* Register notifier */
257 ret = blocking_notifier_chain_register(&altmode->nh, nb);
259 put_device(altmode_dev);
263 return &altmode->adev;
265 EXPORT_SYMBOL_GPL(typec_altmode_register_notifier);
267 void typec_altmode_unregister_notifier(struct typec_altmode *adev,
268 struct notifier_block *nb)
270 struct altmode *altmode = to_altmode(adev);
272 blocking_notifier_chain_unregister(&altmode->nh, nb);
273 put_device(&adev->dev);
275 EXPORT_SYMBOL_GPL(typec_altmode_unregister_notifier);
278 * typec_altmode_update_active - Report Enter/Exit mode
279 * @adev: Handle to the alternate mode
280 * @active: True when the mode has been entered
282 * If a partner or cable plug executes Enter/Exit Mode command successfully, the
283 * drivers use this routine to report the updated state of the mode.
285 void typec_altmode_update_active(struct typec_altmode *adev, bool active)
289 if (adev->active == active)
292 if (!is_typec_port(adev->dev.parent) && adev->dev.driver) {
294 module_put(adev->dev.driver->owner);
296 WARN_ON(!try_module_get(adev->dev.driver->owner));
299 adev->active = active;
300 snprintf(dir, sizeof(dir), "mode%d", adev->mode);
301 sysfs_notify(&adev->dev.kobj, dir, "active");
302 sysfs_notify(&adev->dev.kobj, NULL, "active");
303 kobject_uevent(&adev->dev.kobj, KOBJ_CHANGE);
305 EXPORT_SYMBOL_GPL(typec_altmode_update_active);
308 * typec_altmode2port - Alternate Mode to USB Type-C port
309 * @alt: The Alternate Mode
311 * Returns handle to the port that a cable plug or partner with @alt is
314 struct typec_port *typec_altmode2port(struct typec_altmode *alt)
316 if (is_typec_plug(alt->dev.parent))
317 return to_typec_port(alt->dev.parent->parent->parent);
318 if (is_typec_partner(alt->dev.parent))
319 return to_typec_port(alt->dev.parent->parent);
320 if (is_typec_port(alt->dev.parent))
321 return to_typec_port(alt->dev.parent);
325 EXPORT_SYMBOL_GPL(typec_altmode2port);
328 vdo_show(struct device *dev, struct device_attribute *attr, char *buf)
330 struct typec_altmode *alt = to_typec_altmode(dev);
332 return sprintf(buf, "0x%08x\n", alt->vdo);
334 static DEVICE_ATTR_RO(vdo);
337 description_show(struct device *dev, struct device_attribute *attr, char *buf)
339 struct typec_altmode *alt = to_typec_altmode(dev);
341 return sprintf(buf, "%s\n", alt->desc ? alt->desc : "");
343 static DEVICE_ATTR_RO(description);
346 active_show(struct device *dev, struct device_attribute *attr, char *buf)
348 struct typec_altmode *alt = to_typec_altmode(dev);
350 return sprintf(buf, "%s\n", alt->active ? "yes" : "no");
353 static ssize_t active_store(struct device *dev, struct device_attribute *attr,
354 const char *buf, size_t size)
356 struct typec_altmode *adev = to_typec_altmode(dev);
357 struct altmode *altmode = to_altmode(adev);
361 ret = kstrtobool(buf, &enter);
365 if (adev->active == enter)
368 if (is_typec_port(adev->dev.parent)) {
369 typec_altmode_update_active(adev, enter);
371 /* Make sure that the partner exits the mode before disabling */
372 if (altmode->partner && !enter && altmode->partner->adev.active)
373 typec_altmode_exit(&altmode->partner->adev);
374 } else if (altmode->partner) {
375 if (enter && !altmode->partner->adev.active) {
376 dev_warn(dev, "port has the mode disabled\n");
381 /* Note: If there is no driver, the mode will not be entered */
382 if (adev->ops && adev->ops->activate) {
383 ret = adev->ops->activate(adev, enter);
390 static DEVICE_ATTR_RW(active);
393 supported_roles_show(struct device *dev, struct device_attribute *attr,
396 struct altmode *alt = to_altmode(to_typec_altmode(dev));
399 switch (alt->roles) {
401 ret = sprintf(buf, "source\n");
404 ret = sprintf(buf, "sink\n");
408 ret = sprintf(buf, "source sink\n");
413 static DEVICE_ATTR_RO(supported_roles);
416 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
418 struct typec_altmode *adev = to_typec_altmode(dev);
420 return sprintf(buf, "%u\n", adev->mode);
422 static DEVICE_ATTR_RO(mode);
425 svid_show(struct device *dev, struct device_attribute *attr, char *buf)
427 struct typec_altmode *adev = to_typec_altmode(dev);
429 return sprintf(buf, "%04x\n", adev->svid);
431 static DEVICE_ATTR_RO(svid);
433 static struct attribute *typec_altmode_attrs[] = {
434 &dev_attr_active.attr,
440 ATTRIBUTE_GROUPS(typec_altmode);
442 static int altmode_id_get(struct device *dev)
446 if (is_typec_partner(dev))
447 ids = &to_typec_partner(dev)->mode_ids;
448 else if (is_typec_plug(dev))
449 ids = &to_typec_plug(dev)->mode_ids;
451 ids = &to_typec_port(dev)->mode_ids;
453 return ida_simple_get(ids, 0, 0, GFP_KERNEL);
456 static void altmode_id_remove(struct device *dev, int id)
460 if (is_typec_partner(dev))
461 ids = &to_typec_partner(dev)->mode_ids;
462 else if (is_typec_plug(dev))
463 ids = &to_typec_plug(dev)->mode_ids;
465 ids = &to_typec_port(dev)->mode_ids;
467 ida_simple_remove(ids, id);
470 static void typec_altmode_release(struct device *dev)
472 struct altmode *alt = to_altmode(to_typec_altmode(dev));
474 typec_altmode_put_partner(alt);
476 altmode_id_remove(alt->adev.dev.parent, alt->id);
480 const struct device_type typec_altmode_dev_type = {
481 .name = "typec_alternate_mode",
482 .groups = typec_altmode_groups,
483 .release = typec_altmode_release,
486 static struct typec_altmode *
487 typec_register_altmode(struct device *parent,
488 const struct typec_altmode_desc *desc)
490 unsigned int id = altmode_id_get(parent);
491 bool is_port = is_typec_port(parent);
495 alt = kzalloc(sizeof(*alt), GFP_KERNEL);
497 return ERR_PTR(-ENOMEM);
499 alt->adev.svid = desc->svid;
500 alt->adev.mode = desc->mode;
501 alt->adev.vdo = desc->vdo;
502 alt->roles = desc->roles;
505 alt->attrs[0] = &dev_attr_vdo.attr;
506 alt->attrs[1] = &dev_attr_description.attr;
507 alt->attrs[2] = &dev_attr_active.attr;
510 alt->attrs[3] = &dev_attr_supported_roles.attr;
511 alt->adev.active = true; /* Enabled by default */
514 sprintf(alt->group_name, "mode%d", desc->mode);
515 alt->group.name = alt->group_name;
516 alt->group.attrs = alt->attrs;
517 alt->groups[0] = &alt->group;
519 alt->adev.dev.parent = parent;
520 alt->adev.dev.groups = alt->groups;
521 alt->adev.dev.type = &typec_altmode_dev_type;
522 dev_set_name(&alt->adev.dev, "%s.%u", dev_name(parent), id);
524 /* Link partners and plugs with the ports */
526 BLOCKING_INIT_NOTIFIER_HEAD(&alt->nh);
528 typec_altmode_set_partner(alt);
530 /* The partners are bind to drivers */
531 if (is_typec_partner(parent))
532 alt->adev.dev.bus = &typec_bus;
534 ret = device_register(&alt->adev.dev);
536 dev_err(parent, "failed to register alternate mode (%d)\n",
538 put_device(&alt->adev.dev);
546 * typec_unregister_altmode - Unregister Alternate Mode
547 * @adev: The alternate mode to be unregistered
549 * Unregister device created with typec_partner_register_altmode(),
550 * typec_plug_register_altmode() or typec_port_register_altmode().
552 void typec_unregister_altmode(struct typec_altmode *adev)
554 if (IS_ERR_OR_NULL(adev))
556 typec_mux_put(to_altmode(adev)->mux);
557 device_unregister(&adev->dev);
559 EXPORT_SYMBOL_GPL(typec_unregister_altmode);
561 /* ------------------------------------------------------------------------- */
562 /* Type-C Partners */
564 static ssize_t accessory_mode_show(struct device *dev,
565 struct device_attribute *attr,
568 struct typec_partner *p = to_typec_partner(dev);
570 return sprintf(buf, "%s\n", typec_accessory_modes[p->accessory]);
572 static DEVICE_ATTR_RO(accessory_mode);
574 static ssize_t supports_usb_power_delivery_show(struct device *dev,
575 struct device_attribute *attr,
578 struct typec_partner *p = to_typec_partner(dev);
580 return sprintf(buf, "%s\n", p->usb_pd ? "yes" : "no");
582 static DEVICE_ATTR_RO(supports_usb_power_delivery);
584 static struct attribute *typec_partner_attrs[] = {
585 &dev_attr_accessory_mode.attr,
586 &dev_attr_supports_usb_power_delivery.attr,
589 ATTRIBUTE_GROUPS(typec_partner);
591 static void typec_partner_release(struct device *dev)
593 struct typec_partner *partner = to_typec_partner(dev);
595 ida_destroy(&partner->mode_ids);
599 static const struct device_type typec_partner_dev_type = {
600 .name = "typec_partner",
601 .groups = typec_partner_groups,
602 .release = typec_partner_release,
606 * typec_partner_set_identity - Report result from Discover Identity command
607 * @partner: The partner updated identity values
609 * This routine is used to report that the result of Discover Identity USB power
610 * delivery command has become available.
612 int typec_partner_set_identity(struct typec_partner *partner)
614 if (!partner->identity)
617 typec_report_identity(&partner->dev);
620 EXPORT_SYMBOL_GPL(typec_partner_set_identity);
623 * typec_partner_register_altmode - Register USB Type-C Partner Alternate Mode
624 * @partner: USB Type-C Partner that supports the alternate mode
625 * @desc: Description of the alternate mode
627 * This routine is used to register each alternate mode individually that
628 * @partner has listed in response to Discover SVIDs command. The modes for a
629 * SVID listed in response to Discover Modes command need to be listed in an
632 * Returns handle to the alternate mode on success or NULL on failure.
634 struct typec_altmode *
635 typec_partner_register_altmode(struct typec_partner *partner,
636 const struct typec_altmode_desc *desc)
638 return typec_register_altmode(&partner->dev, desc);
640 EXPORT_SYMBOL_GPL(typec_partner_register_altmode);
643 * typec_register_partner - Register a USB Type-C Partner
644 * @port: The USB Type-C Port the partner is connected to
645 * @desc: Description of the partner
647 * Registers a device for USB Type-C Partner described in @desc.
649 * Returns handle to the partner on success or ERR_PTR on failure.
651 struct typec_partner *typec_register_partner(struct typec_port *port,
652 struct typec_partner_desc *desc)
654 struct typec_partner *partner;
657 partner = kzalloc(sizeof(*partner), GFP_KERNEL);
659 return ERR_PTR(-ENOMEM);
661 ida_init(&partner->mode_ids);
662 partner->usb_pd = desc->usb_pd;
663 partner->accessory = desc->accessory;
665 if (desc->identity) {
667 * Creating directory for the identity only if the driver is
668 * able to provide data to it.
670 partner->dev.groups = usb_pd_id_groups;
671 partner->identity = desc->identity;
674 partner->dev.class = typec_class;
675 partner->dev.parent = &port->dev;
676 partner->dev.type = &typec_partner_dev_type;
677 dev_set_name(&partner->dev, "%s-partner", dev_name(&port->dev));
679 ret = device_register(&partner->dev);
681 dev_err(&port->dev, "failed to register partner (%d)\n", ret);
682 put_device(&partner->dev);
688 EXPORT_SYMBOL_GPL(typec_register_partner);
691 * typec_unregister_partner - Unregister a USB Type-C Partner
692 * @partner: The partner to be unregistered
694 * Unregister device created with typec_register_partner().
696 void typec_unregister_partner(struct typec_partner *partner)
698 if (!IS_ERR_OR_NULL(partner))
699 device_unregister(&partner->dev);
701 EXPORT_SYMBOL_GPL(typec_unregister_partner);
703 /* ------------------------------------------------------------------------- */
704 /* Type-C Cable Plugs */
706 static void typec_plug_release(struct device *dev)
708 struct typec_plug *plug = to_typec_plug(dev);
710 ida_destroy(&plug->mode_ids);
714 static const struct device_type typec_plug_dev_type = {
715 .name = "typec_plug",
716 .release = typec_plug_release,
720 * typec_plug_register_altmode - Register USB Type-C Cable Plug Alternate Mode
721 * @plug: USB Type-C Cable Plug that supports the alternate mode
722 * @desc: Description of the alternate mode
724 * This routine is used to register each alternate mode individually that @plug
725 * has listed in response to Discover SVIDs command. The modes for a SVID that
726 * the plug lists in response to Discover Modes command need to be listed in an
729 * Returns handle to the alternate mode on success or ERR_PTR on failure.
731 struct typec_altmode *
732 typec_plug_register_altmode(struct typec_plug *plug,
733 const struct typec_altmode_desc *desc)
735 return typec_register_altmode(&plug->dev, desc);
737 EXPORT_SYMBOL_GPL(typec_plug_register_altmode);
740 * typec_register_plug - Register a USB Type-C Cable Plug
741 * @cable: USB Type-C Cable with the plug
742 * @desc: Description of the cable plug
744 * Registers a device for USB Type-C Cable Plug described in @desc. A USB Type-C
745 * Cable Plug represents a plug with electronics in it that can response to USB
746 * Power Delivery SOP Prime or SOP Double Prime packages.
748 * Returns handle to the cable plug on success or ERR_PTR on failure.
750 struct typec_plug *typec_register_plug(struct typec_cable *cable,
751 struct typec_plug_desc *desc)
753 struct typec_plug *plug;
757 plug = kzalloc(sizeof(*plug), GFP_KERNEL);
759 return ERR_PTR(-ENOMEM);
761 sprintf(name, "plug%d", desc->index);
763 ida_init(&plug->mode_ids);
764 plug->index = desc->index;
765 plug->dev.class = typec_class;
766 plug->dev.parent = &cable->dev;
767 plug->dev.type = &typec_plug_dev_type;
768 dev_set_name(&plug->dev, "%s-%s", dev_name(cable->dev.parent), name);
770 ret = device_register(&plug->dev);
772 dev_err(&cable->dev, "failed to register plug (%d)\n", ret);
773 put_device(&plug->dev);
779 EXPORT_SYMBOL_GPL(typec_register_plug);
782 * typec_unregister_plug - Unregister a USB Type-C Cable Plug
783 * @plug: The cable plug to be unregistered
785 * Unregister device created with typec_register_plug().
787 void typec_unregister_plug(struct typec_plug *plug)
789 if (!IS_ERR_OR_NULL(plug))
790 device_unregister(&plug->dev);
792 EXPORT_SYMBOL_GPL(typec_unregister_plug);
797 type_show(struct device *dev, struct device_attribute *attr, char *buf)
799 struct typec_cable *cable = to_typec_cable(dev);
801 return sprintf(buf, "%s\n", cable->active ? "active" : "passive");
803 static DEVICE_ATTR_RO(type);
805 static const char * const typec_plug_types[] = {
806 [USB_PLUG_NONE] = "unknown",
807 [USB_PLUG_TYPE_A] = "type-a",
808 [USB_PLUG_TYPE_B] = "type-b",
809 [USB_PLUG_TYPE_C] = "type-c",
810 [USB_PLUG_CAPTIVE] = "captive",
813 static ssize_t plug_type_show(struct device *dev,
814 struct device_attribute *attr, char *buf)
816 struct typec_cable *cable = to_typec_cable(dev);
818 return sprintf(buf, "%s\n", typec_plug_types[cable->type]);
820 static DEVICE_ATTR_RO(plug_type);
822 static struct attribute *typec_cable_attrs[] = {
824 &dev_attr_plug_type.attr,
827 ATTRIBUTE_GROUPS(typec_cable);
829 static void typec_cable_release(struct device *dev)
831 struct typec_cable *cable = to_typec_cable(dev);
836 static const struct device_type typec_cable_dev_type = {
837 .name = "typec_cable",
838 .groups = typec_cable_groups,
839 .release = typec_cable_release,
843 * typec_cable_set_identity - Report result from Discover Identity command
844 * @cable: The cable updated identity values
846 * This routine is used to report that the result of Discover Identity USB power
847 * delivery command has become available.
849 int typec_cable_set_identity(struct typec_cable *cable)
851 if (!cable->identity)
854 typec_report_identity(&cable->dev);
857 EXPORT_SYMBOL_GPL(typec_cable_set_identity);
860 * typec_register_cable - Register a USB Type-C Cable
861 * @port: The USB Type-C Port the cable is connected to
862 * @desc: Description of the cable
864 * Registers a device for USB Type-C Cable described in @desc. The cable will be
865 * parent for the optional cable plug devises.
867 * Returns handle to the cable on success or ERR_PTR on failure.
869 struct typec_cable *typec_register_cable(struct typec_port *port,
870 struct typec_cable_desc *desc)
872 struct typec_cable *cable;
875 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
877 return ERR_PTR(-ENOMEM);
879 cable->type = desc->type;
880 cable->active = desc->active;
882 if (desc->identity) {
884 * Creating directory for the identity only if the driver is
885 * able to provide data to it.
887 cable->dev.groups = usb_pd_id_groups;
888 cable->identity = desc->identity;
891 cable->dev.class = typec_class;
892 cable->dev.parent = &port->dev;
893 cable->dev.type = &typec_cable_dev_type;
894 dev_set_name(&cable->dev, "%s-cable", dev_name(&port->dev));
896 ret = device_register(&cable->dev);
898 dev_err(&port->dev, "failed to register cable (%d)\n", ret);
899 put_device(&cable->dev);
905 EXPORT_SYMBOL_GPL(typec_register_cable);
908 * typec_unregister_cable - Unregister a USB Type-C Cable
909 * @cable: The cable to be unregistered
911 * Unregister device created with typec_register_cable().
913 void typec_unregister_cable(struct typec_cable *cable)
915 if (!IS_ERR_OR_NULL(cable))
916 device_unregister(&cable->dev);
918 EXPORT_SYMBOL_GPL(typec_unregister_cable);
920 /* ------------------------------------------------------------------------- */
921 /* USB Type-C ports */
923 static const char * const typec_roles[] = {
924 [TYPEC_SINK] = "sink",
925 [TYPEC_SOURCE] = "source",
928 static const char * const typec_data_roles[] = {
929 [TYPEC_DEVICE] = "device",
930 [TYPEC_HOST] = "host",
933 static const char * const typec_port_power_roles[] = {
934 [TYPEC_PORT_SRC] = "source",
935 [TYPEC_PORT_SNK] = "sink",
936 [TYPEC_PORT_DRP] = "dual",
939 static const char * const typec_port_data_roles[] = {
940 [TYPEC_PORT_DFP] = "host",
941 [TYPEC_PORT_UFP] = "device",
942 [TYPEC_PORT_DRD] = "dual",
945 static const char * const typec_port_types_drp[] = {
946 [TYPEC_PORT_SRC] = "dual [source] sink",
947 [TYPEC_PORT_SNK] = "dual source [sink]",
948 [TYPEC_PORT_DRP] = "[dual] source sink",
952 preferred_role_store(struct device *dev, struct device_attribute *attr,
953 const char *buf, size_t size)
955 struct typec_port *port = to_typec_port(dev);
959 if (port->cap->type != TYPEC_PORT_DRP) {
960 dev_dbg(dev, "Preferred role only supported with DRP ports\n");
964 if (!port->cap->try_role) {
965 dev_dbg(dev, "Setting preferred role not supported\n");
969 role = sysfs_match_string(typec_roles, buf);
971 if (sysfs_streq(buf, "none"))
972 role = TYPEC_NO_PREFERRED_ROLE;
977 ret = port->cap->try_role(port->cap, role);
981 port->prefer_role = role;
986 preferred_role_show(struct device *dev, struct device_attribute *attr,
989 struct typec_port *port = to_typec_port(dev);
991 if (port->cap->type != TYPEC_PORT_DRP)
994 if (port->prefer_role < 0)
997 return sprintf(buf, "%s\n", typec_roles[port->prefer_role]);
999 static DEVICE_ATTR_RW(preferred_role);
1001 static ssize_t data_role_store(struct device *dev,
1002 struct device_attribute *attr,
1003 const char *buf, size_t size)
1005 struct typec_port *port = to_typec_port(dev);
1008 if (!port->cap->dr_set) {
1009 dev_dbg(dev, "data role swapping not supported\n");
1013 ret = sysfs_match_string(typec_data_roles, buf);
1017 mutex_lock(&port->port_type_lock);
1018 if (port->cap->data != TYPEC_PORT_DRD) {
1020 goto unlock_and_ret;
1023 ret = port->cap->dr_set(port->cap, ret);
1025 goto unlock_and_ret;
1029 mutex_unlock(&port->port_type_lock);
1033 static ssize_t data_role_show(struct device *dev,
1034 struct device_attribute *attr, char *buf)
1036 struct typec_port *port = to_typec_port(dev);
1038 if (port->cap->data == TYPEC_PORT_DRD)
1039 return sprintf(buf, "%s\n", port->data_role == TYPEC_HOST ?
1040 "[host] device" : "host [device]");
1042 return sprintf(buf, "[%s]\n", typec_data_roles[port->data_role]);
1044 static DEVICE_ATTR_RW(data_role);
1046 static ssize_t power_role_store(struct device *dev,
1047 struct device_attribute *attr,
1048 const char *buf, size_t size)
1050 struct typec_port *port = to_typec_port(dev);
1053 if (!port->cap->pd_revision) {
1054 dev_dbg(dev, "USB Power Delivery not supported\n");
1058 if (!port->cap->pr_set) {
1059 dev_dbg(dev, "power role swapping not supported\n");
1063 if (port->pwr_opmode != TYPEC_PWR_MODE_PD) {
1064 dev_dbg(dev, "partner unable to swap power role\n");
1068 ret = sysfs_match_string(typec_roles, buf);
1072 mutex_lock(&port->port_type_lock);
1073 if (port->port_type != TYPEC_PORT_DRP) {
1074 dev_dbg(dev, "port type fixed at \"%s\"",
1075 typec_port_power_roles[port->port_type]);
1077 goto unlock_and_ret;
1080 ret = port->cap->pr_set(port->cap, ret);
1082 goto unlock_and_ret;
1086 mutex_unlock(&port->port_type_lock);
1090 static ssize_t power_role_show(struct device *dev,
1091 struct device_attribute *attr, char *buf)
1093 struct typec_port *port = to_typec_port(dev);
1095 if (port->cap->type == TYPEC_PORT_DRP)
1096 return sprintf(buf, "%s\n", port->pwr_role == TYPEC_SOURCE ?
1097 "[source] sink" : "source [sink]");
1099 return sprintf(buf, "[%s]\n", typec_roles[port->pwr_role]);
1101 static DEVICE_ATTR_RW(power_role);
1104 port_type_store(struct device *dev, struct device_attribute *attr,
1105 const char *buf, size_t size)
1107 struct typec_port *port = to_typec_port(dev);
1109 enum typec_port_type type;
1111 if (!port->cap->port_type_set || port->cap->type != TYPEC_PORT_DRP) {
1112 dev_dbg(dev, "changing port type not supported\n");
1116 ret = sysfs_match_string(typec_port_power_roles, buf);
1121 mutex_lock(&port->port_type_lock);
1123 if (port->port_type == type) {
1125 goto unlock_and_ret;
1128 ret = port->cap->port_type_set(port->cap, type);
1130 goto unlock_and_ret;
1132 port->port_type = type;
1136 mutex_unlock(&port->port_type_lock);
1141 port_type_show(struct device *dev, struct device_attribute *attr,
1144 struct typec_port *port = to_typec_port(dev);
1146 if (port->cap->type == TYPEC_PORT_DRP)
1147 return sprintf(buf, "%s\n",
1148 typec_port_types_drp[port->port_type]);
1150 return sprintf(buf, "[%s]\n", typec_port_power_roles[port->cap->type]);
1152 static DEVICE_ATTR_RW(port_type);
1154 static const char * const typec_pwr_opmodes[] = {
1155 [TYPEC_PWR_MODE_USB] = "default",
1156 [TYPEC_PWR_MODE_1_5A] = "1.5A",
1157 [TYPEC_PWR_MODE_3_0A] = "3.0A",
1158 [TYPEC_PWR_MODE_PD] = "usb_power_delivery",
1161 static ssize_t power_operation_mode_show(struct device *dev,
1162 struct device_attribute *attr,
1165 struct typec_port *port = to_typec_port(dev);
1167 return sprintf(buf, "%s\n", typec_pwr_opmodes[port->pwr_opmode]);
1169 static DEVICE_ATTR_RO(power_operation_mode);
1171 static ssize_t vconn_source_store(struct device *dev,
1172 struct device_attribute *attr,
1173 const char *buf, size_t size)
1175 struct typec_port *port = to_typec_port(dev);
1179 if (!port->cap->pd_revision) {
1180 dev_dbg(dev, "VCONN swap depends on USB Power Delivery\n");
1184 if (!port->cap->vconn_set) {
1185 dev_dbg(dev, "VCONN swapping not supported\n");
1189 ret = kstrtobool(buf, &source);
1193 ret = port->cap->vconn_set(port->cap, (enum typec_role)source);
1200 static ssize_t vconn_source_show(struct device *dev,
1201 struct device_attribute *attr, char *buf)
1203 struct typec_port *port = to_typec_port(dev);
1205 return sprintf(buf, "%s\n",
1206 port->vconn_role == TYPEC_SOURCE ? "yes" : "no");
1208 static DEVICE_ATTR_RW(vconn_source);
1210 static ssize_t supported_accessory_modes_show(struct device *dev,
1211 struct device_attribute *attr,
1214 struct typec_port *port = to_typec_port(dev);
1218 for (i = 0; i < ARRAY_SIZE(port->cap->accessory); i++) {
1219 if (port->cap->accessory[i])
1220 ret += sprintf(buf + ret, "%s ",
1221 typec_accessory_modes[port->cap->accessory[i]]);
1225 return sprintf(buf, "none\n");
1227 buf[ret - 1] = '\n';
1231 static DEVICE_ATTR_RO(supported_accessory_modes);
1233 static ssize_t usb_typec_revision_show(struct device *dev,
1234 struct device_attribute *attr,
1237 struct typec_port *port = to_typec_port(dev);
1238 u16 rev = port->cap->revision;
1240 return sprintf(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
1242 static DEVICE_ATTR_RO(usb_typec_revision);
1244 static ssize_t usb_power_delivery_revision_show(struct device *dev,
1245 struct device_attribute *attr,
1248 struct typec_port *p = to_typec_port(dev);
1250 return sprintf(buf, "%d\n", (p->cap->pd_revision >> 8) & 0xff);
1252 static DEVICE_ATTR_RO(usb_power_delivery_revision);
1254 static struct attribute *typec_attrs[] = {
1255 &dev_attr_data_role.attr,
1256 &dev_attr_power_operation_mode.attr,
1257 &dev_attr_power_role.attr,
1258 &dev_attr_preferred_role.attr,
1259 &dev_attr_supported_accessory_modes.attr,
1260 &dev_attr_usb_power_delivery_revision.attr,
1261 &dev_attr_usb_typec_revision.attr,
1262 &dev_attr_vconn_source.attr,
1263 &dev_attr_port_type.attr,
1266 ATTRIBUTE_GROUPS(typec);
1268 static int typec_uevent(struct device *dev, struct kobj_uevent_env *env)
1272 ret = add_uevent_var(env, "TYPEC_PORT=%s", dev_name(dev));
1274 dev_err(dev, "failed to add uevent TYPEC_PORT\n");
1279 static void typec_release(struct device *dev)
1281 struct typec_port *port = to_typec_port(dev);
1283 ida_simple_remove(&typec_index_ida, port->id);
1284 ida_destroy(&port->mode_ids);
1285 typec_switch_put(port->sw);
1286 typec_mux_put(port->mux);
1290 const struct device_type typec_port_dev_type = {
1291 .name = "typec_port",
1292 .groups = typec_groups,
1293 .uevent = typec_uevent,
1294 .release = typec_release,
1297 /* --------------------------------------- */
1298 /* Driver callbacks to report role updates */
1301 * typec_set_data_role - Report data role change
1302 * @port: The USB Type-C Port where the role was changed
1303 * @role: The new data role
1305 * This routine is used by the port drivers to report data role changes.
1307 void typec_set_data_role(struct typec_port *port, enum typec_data_role role)
1309 if (port->data_role == role)
1312 port->data_role = role;
1313 sysfs_notify(&port->dev.kobj, NULL, "data_role");
1314 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1316 EXPORT_SYMBOL_GPL(typec_set_data_role);
1319 * typec_set_pwr_role - Report power role change
1320 * @port: The USB Type-C Port where the role was changed
1321 * @role: The new data role
1323 * This routine is used by the port drivers to report power role changes.
1325 void typec_set_pwr_role(struct typec_port *port, enum typec_role role)
1327 if (port->pwr_role == role)
1330 port->pwr_role = role;
1331 sysfs_notify(&port->dev.kobj, NULL, "power_role");
1332 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1334 EXPORT_SYMBOL_GPL(typec_set_pwr_role);
1337 * typec_set_vconn_role - Report VCONN source change
1338 * @port: The USB Type-C Port which VCONN role changed
1339 * @role: Source when @port is sourcing VCONN, or Sink when it's not
1341 * This routine is used by the port drivers to report if the VCONN source is
1344 void typec_set_vconn_role(struct typec_port *port, enum typec_role role)
1346 if (port->vconn_role == role)
1349 port->vconn_role = role;
1350 sysfs_notify(&port->dev.kobj, NULL, "vconn_source");
1351 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1353 EXPORT_SYMBOL_GPL(typec_set_vconn_role);
1355 static int partner_match(struct device *dev, void *data)
1357 return is_typec_partner(dev);
1361 * typec_set_pwr_opmode - Report changed power operation mode
1362 * @port: The USB Type-C Port where the mode was changed
1363 * @opmode: New power operation mode
1365 * This routine is used by the port drivers to report changed power operation
1366 * mode in @port. The modes are USB (default), 1.5A, 3.0A as defined in USB
1367 * Type-C specification, and "USB Power Delivery" when the power levels are
1368 * negotiated with methods defined in USB Power Delivery specification.
1370 void typec_set_pwr_opmode(struct typec_port *port,
1371 enum typec_pwr_opmode opmode)
1373 struct device *partner_dev;
1375 if (port->pwr_opmode == opmode)
1378 port->pwr_opmode = opmode;
1379 sysfs_notify(&port->dev.kobj, NULL, "power_operation_mode");
1380 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1382 partner_dev = device_find_child(&port->dev, NULL, partner_match);
1384 struct typec_partner *partner = to_typec_partner(partner_dev);
1386 if (opmode == TYPEC_PWR_MODE_PD && !partner->usb_pd) {
1387 partner->usb_pd = 1;
1388 sysfs_notify(&partner_dev->kobj, NULL,
1389 "supports_usb_power_delivery");
1391 put_device(partner_dev);
1394 EXPORT_SYMBOL_GPL(typec_set_pwr_opmode);
1397 * typec_find_port_power_role - Get the typec port power capability
1398 * @name: port power capability string
1400 * This routine is used to find the typec_port_type by its string name.
1402 * Returns typec_port_type if success, otherwise negative error code.
1404 int typec_find_port_power_role(const char *name)
1406 return match_string(typec_port_power_roles,
1407 ARRAY_SIZE(typec_port_power_roles), name);
1409 EXPORT_SYMBOL_GPL(typec_find_port_power_role);
1412 * typec_find_power_role - Find the typec one specific power role
1413 * @name: power role string
1415 * This routine is used to find the typec_role by its string name.
1417 * Returns typec_role if success, otherwise negative error code.
1419 int typec_find_power_role(const char *name)
1421 return match_string(typec_roles, ARRAY_SIZE(typec_roles), name);
1423 EXPORT_SYMBOL_GPL(typec_find_power_role);
1426 * typec_find_port_data_role - Get the typec port data capability
1427 * @name: port data capability string
1429 * This routine is used to find the typec_port_data by its string name.
1431 * Returns typec_port_data if success, otherwise negative error code.
1433 int typec_find_port_data_role(const char *name)
1435 return match_string(typec_port_data_roles,
1436 ARRAY_SIZE(typec_port_data_roles), name);
1438 EXPORT_SYMBOL_GPL(typec_find_port_data_role);
1440 /* ------------------------------------------ */
1441 /* API for Multiplexer/DeMultiplexer Switches */
1444 * typec_set_orientation - Set USB Type-C cable plug orientation
1445 * @port: USB Type-C Port
1446 * @orientation: USB Type-C cable plug orientation
1448 * Set cable plug orientation for @port.
1450 int typec_set_orientation(struct typec_port *port,
1451 enum typec_orientation orientation)
1456 ret = port->sw->set(port->sw, orientation);
1461 port->orientation = orientation;
1465 EXPORT_SYMBOL_GPL(typec_set_orientation);
1468 * typec_get_orientation - Get USB Type-C cable plug orientation
1469 * @port: USB Type-C Port
1471 * Get current cable plug orientation for @port.
1473 enum typec_orientation typec_get_orientation(struct typec_port *port)
1475 return port->orientation;
1477 EXPORT_SYMBOL_GPL(typec_get_orientation);
1480 * typec_set_mode - Set mode of operation for USB Type-C connector
1481 * @port: USB Type-C connector
1482 * @mode: Accessory Mode, USB Operation or Safe State
1484 * Configure @port for Accessory Mode @mode. This function will configure the
1485 * muxes needed for @mode.
1487 int typec_set_mode(struct typec_port *port, int mode)
1489 return port->mux ? port->mux->set(port->mux, mode) : 0;
1491 EXPORT_SYMBOL_GPL(typec_set_mode);
1493 /* --------------------------------------- */
1496 * typec_port_register_altmode - Register USB Type-C Port Alternate Mode
1497 * @port: USB Type-C Port that supports the alternate mode
1498 * @desc: Description of the alternate mode
1500 * This routine is used to register an alternate mode that @port is capable of
1503 * Returns handle to the alternate mode on success or ERR_PTR on failure.
1505 struct typec_altmode *
1506 typec_port_register_altmode(struct typec_port *port,
1507 const struct typec_altmode_desc *desc)
1509 struct typec_altmode *adev;
1510 struct typec_mux *mux;
1512 mux = typec_mux_get(&port->dev, desc);
1514 return ERR_CAST(mux);
1516 adev = typec_register_altmode(&port->dev, desc);
1520 to_altmode(adev)->mux = mux;
1524 EXPORT_SYMBOL_GPL(typec_port_register_altmode);
1527 * typec_register_port - Register a USB Type-C Port
1528 * @parent: Parent device
1529 * @cap: Description of the port
1531 * Registers a device for USB Type-C Port described in @cap.
1533 * Returns handle to the port on success or ERR_PTR on failure.
1535 struct typec_port *typec_register_port(struct device *parent,
1536 const struct typec_capability *cap)
1538 struct typec_port *port;
1542 port = kzalloc(sizeof(*port), GFP_KERNEL);
1544 return ERR_PTR(-ENOMEM);
1546 id = ida_simple_get(&typec_index_ida, 0, 0, GFP_KERNEL);
1552 switch (cap->type) {
1553 case TYPEC_PORT_SRC:
1554 port->pwr_role = TYPEC_SOURCE;
1555 port->vconn_role = TYPEC_SOURCE;
1557 case TYPEC_PORT_SNK:
1558 port->pwr_role = TYPEC_SINK;
1559 port->vconn_role = TYPEC_SINK;
1561 case TYPEC_PORT_DRP:
1562 if (cap->prefer_role != TYPEC_NO_PREFERRED_ROLE)
1563 port->pwr_role = cap->prefer_role;
1565 port->pwr_role = TYPEC_SINK;
1569 switch (cap->data) {
1570 case TYPEC_PORT_DFP:
1571 port->data_role = TYPEC_HOST;
1573 case TYPEC_PORT_UFP:
1574 port->data_role = TYPEC_DEVICE;
1576 case TYPEC_PORT_DRD:
1577 if (cap->prefer_role == TYPEC_SOURCE)
1578 port->data_role = TYPEC_HOST;
1580 port->data_role = TYPEC_DEVICE;
1584 ida_init(&port->mode_ids);
1585 mutex_init(&port->port_type_lock);
1589 port->port_type = cap->type;
1590 port->prefer_role = cap->prefer_role;
1592 device_initialize(&port->dev);
1593 port->dev.class = typec_class;
1594 port->dev.parent = parent;
1595 port->dev.fwnode = cap->fwnode;
1596 port->dev.type = &typec_port_dev_type;
1597 dev_set_name(&port->dev, "port%d", id);
1599 port->sw = typec_switch_get(&port->dev);
1600 if (IS_ERR(port->sw)) {
1601 put_device(&port->dev);
1602 return ERR_CAST(port->sw);
1605 port->mux = typec_mux_get(&port->dev, NULL);
1606 if (IS_ERR(port->mux)) {
1607 put_device(&port->dev);
1608 return ERR_CAST(port->mux);
1611 ret = device_add(&port->dev);
1613 dev_err(parent, "failed to register port (%d)\n", ret);
1614 put_device(&port->dev);
1615 return ERR_PTR(ret);
1620 EXPORT_SYMBOL_GPL(typec_register_port);
1623 * typec_unregister_port - Unregister a USB Type-C Port
1624 * @port: The port to be unregistered
1626 * Unregister device created with typec_register_port().
1628 void typec_unregister_port(struct typec_port *port)
1630 if (!IS_ERR_OR_NULL(port))
1631 device_unregister(&port->dev);
1633 EXPORT_SYMBOL_GPL(typec_unregister_port);
1635 static int __init typec_init(void)
1639 ret = bus_register(&typec_bus);
1643 ret = class_register(&typec_mux_class);
1645 goto err_unregister_bus;
1647 typec_class = class_create(THIS_MODULE, "typec");
1648 if (IS_ERR(typec_class)) {
1649 ret = PTR_ERR(typec_class);
1650 goto err_unregister_mux_class;
1655 err_unregister_mux_class:
1656 class_unregister(&typec_mux_class);
1659 bus_unregister(&typec_bus);
1663 subsys_initcall(typec_init);
1665 static void __exit typec_exit(void)
1667 class_destroy(typec_class);
1668 ida_destroy(&typec_index_ida);
1669 bus_unregister(&typec_bus);
1670 class_unregister(&typec_mux_class);
1672 module_exit(typec_exit);
1675 MODULE_LICENSE("GPL v2");
1676 MODULE_DESCRIPTION("USB Type-C Connector Class");