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/slab.h>
13 #include <linux/usb/typec.h>
14 #include <linux/usb/typec_mux.h>
20 enum typec_port_type roles;
22 struct typec_altmode *alt_mode;
24 unsigned int active:1;
27 struct attribute_group group;
28 struct attribute *attrs[5];
29 struct device_attribute vdo_attr;
30 struct device_attribute desc_attr;
31 struct device_attribute active_attr;
32 struct device_attribute roles_attr;
35 struct typec_altmode {
39 struct typec_mode modes[ALTMODE_MAX_MODES];
40 const struct attribute_group *mode_groups[ALTMODE_MAX_MODES];
45 enum typec_plug_index index;
50 enum typec_plug_type type;
51 struct usb_pd_identity *identity;
52 unsigned int active:1;
55 struct typec_partner {
57 unsigned int usb_pd:1;
58 struct usb_pd_identity *identity;
59 enum typec_accessory accessory;
67 enum typec_data_role data_role;
68 enum typec_role pwr_role;
69 enum typec_role vconn_role;
70 enum typec_pwr_opmode pwr_opmode;
71 enum typec_port_type port_type;
72 struct mutex port_type_lock;
74 enum typec_orientation orientation;
75 struct typec_switch *sw;
76 struct typec_mux *mux;
78 const struct typec_capability *cap;
81 #define to_typec_port(_dev_) container_of(_dev_, struct typec_port, dev)
82 #define to_typec_plug(_dev_) container_of(_dev_, struct typec_plug, dev)
83 #define to_typec_cable(_dev_) container_of(_dev_, struct typec_cable, dev)
84 #define to_typec_partner(_dev_) container_of(_dev_, struct typec_partner, dev)
85 #define to_altmode(_dev_) container_of(_dev_, struct typec_altmode, dev)
87 static const struct device_type typec_partner_dev_type;
88 static const struct device_type typec_cable_dev_type;
89 static const struct device_type typec_plug_dev_type;
90 static const struct device_type typec_port_dev_type;
92 #define is_typec_partner(_dev_) (_dev_->type == &typec_partner_dev_type)
93 #define is_typec_cable(_dev_) (_dev_->type == &typec_cable_dev_type)
94 #define is_typec_plug(_dev_) (_dev_->type == &typec_plug_dev_type)
95 #define is_typec_port(_dev_) (_dev_->type == &typec_port_dev_type)
97 static DEFINE_IDA(typec_index_ida);
98 static struct class *typec_class;
100 /* ------------------------------------------------------------------------- */
101 /* Common attributes */
103 static const char * const typec_accessory_modes[] = {
104 [TYPEC_ACCESSORY_NONE] = "none",
105 [TYPEC_ACCESSORY_AUDIO] = "analog_audio",
106 [TYPEC_ACCESSORY_DEBUG] = "debug",
109 static struct usb_pd_identity *get_pd_identity(struct device *dev)
111 if (is_typec_partner(dev)) {
112 struct typec_partner *partner = to_typec_partner(dev);
114 return partner->identity;
115 } else if (is_typec_cable(dev)) {
116 struct typec_cable *cable = to_typec_cable(dev);
118 return cable->identity;
123 static ssize_t id_header_show(struct device *dev, struct device_attribute *attr,
126 struct usb_pd_identity *id = get_pd_identity(dev);
128 return sprintf(buf, "0x%08x\n", id->id_header);
130 static DEVICE_ATTR_RO(id_header);
132 static ssize_t cert_stat_show(struct device *dev, struct device_attribute *attr,
135 struct usb_pd_identity *id = get_pd_identity(dev);
137 return sprintf(buf, "0x%08x\n", id->cert_stat);
139 static DEVICE_ATTR_RO(cert_stat);
141 static ssize_t product_show(struct device *dev, struct device_attribute *attr,
144 struct usb_pd_identity *id = get_pd_identity(dev);
146 return sprintf(buf, "0x%08x\n", id->product);
148 static DEVICE_ATTR_RO(product);
150 static struct attribute *usb_pd_id_attrs[] = {
151 &dev_attr_id_header.attr,
152 &dev_attr_cert_stat.attr,
153 &dev_attr_product.attr,
157 static const struct attribute_group usb_pd_id_group = {
159 .attrs = usb_pd_id_attrs,
162 static const struct attribute_group *usb_pd_id_groups[] = {
167 static void typec_report_identity(struct device *dev)
169 sysfs_notify(&dev->kobj, "identity", "id_header");
170 sysfs_notify(&dev->kobj, "identity", "cert_stat");
171 sysfs_notify(&dev->kobj, "identity", "product");
174 /* ------------------------------------------------------------------------- */
175 /* Alternate Modes */
178 * typec_altmode_update_active - Report Enter/Exit mode
179 * @alt: Handle to the alternate mode
181 * @active: True when the mode has been entered
183 * If a partner or cable plug executes Enter/Exit Mode command successfully, the
184 * drivers use this routine to report the updated state of the mode.
186 void typec_altmode_update_active(struct typec_altmode *alt, int mode,
189 struct typec_mode *m = &alt->modes[mode];
192 if (m->active == active)
196 snprintf(dir, sizeof(dir), "mode%d", mode);
197 sysfs_notify(&alt->dev.kobj, dir, "active");
198 kobject_uevent(&alt->dev.kobj, KOBJ_CHANGE);
200 EXPORT_SYMBOL_GPL(typec_altmode_update_active);
203 * typec_altmode2port - Alternate Mode to USB Type-C port
204 * @alt: The Alternate Mode
206 * Returns handle to the port that a cable plug or partner with @alt is
209 struct typec_port *typec_altmode2port(struct typec_altmode *alt)
211 if (is_typec_plug(alt->dev.parent))
212 return to_typec_port(alt->dev.parent->parent->parent);
213 if (is_typec_partner(alt->dev.parent))
214 return to_typec_port(alt->dev.parent->parent);
215 if (is_typec_port(alt->dev.parent))
216 return to_typec_port(alt->dev.parent);
220 EXPORT_SYMBOL_GPL(typec_altmode2port);
223 typec_altmode_vdo_show(struct device *dev, struct device_attribute *attr,
226 struct typec_mode *mode = container_of(attr, struct typec_mode,
229 return sprintf(buf, "0x%08x\n", mode->vdo);
233 typec_altmode_desc_show(struct device *dev, struct device_attribute *attr,
236 struct typec_mode *mode = container_of(attr, struct typec_mode,
239 return sprintf(buf, "%s\n", mode->desc ? mode->desc : "");
243 typec_altmode_active_show(struct device *dev, struct device_attribute *attr,
246 struct typec_mode *mode = container_of(attr, struct typec_mode,
249 return sprintf(buf, "%s\n", mode->active ? "yes" : "no");
253 typec_altmode_active_store(struct device *dev, struct device_attribute *attr,
254 const char *buf, size_t size)
256 struct typec_mode *mode = container_of(attr, struct typec_mode,
258 struct typec_port *port = typec_altmode2port(mode->alt_mode);
262 if (!port->cap->activate_mode)
265 ret = kstrtobool(buf, &activate);
269 ret = port->cap->activate_mode(port->cap, mode->index, activate);
277 typec_altmode_roles_show(struct device *dev, struct device_attribute *attr,
280 struct typec_mode *mode = container_of(attr, struct typec_mode,
284 switch (mode->roles) {
286 ret = sprintf(buf, "source\n");
289 ret = sprintf(buf, "sink\n");
293 ret = sprintf(buf, "source sink\n");
299 static void typec_init_modes(struct typec_altmode *alt,
300 const struct typec_mode_desc *desc, bool is_port)
304 for (i = 0; i < alt->n_modes; i++, desc++) {
305 struct typec_mode *mode = &alt->modes[i];
307 /* Not considering the human readable description critical */
308 mode->desc = kstrdup(desc->desc, GFP_KERNEL);
309 if (desc->desc && !mode->desc)
310 dev_err(&alt->dev, "failed to copy mode%d desc\n", i);
312 mode->alt_mode = alt;
313 mode->vdo = desc->vdo;
314 mode->roles = desc->roles;
315 mode->index = desc->index;
316 sprintf(mode->group_name, "mode%d", desc->index);
318 sysfs_attr_init(&mode->vdo_attr.attr);
319 mode->vdo_attr.attr.name = "vdo";
320 mode->vdo_attr.attr.mode = 0444;
321 mode->vdo_attr.show = typec_altmode_vdo_show;
323 sysfs_attr_init(&mode->desc_attr.attr);
324 mode->desc_attr.attr.name = "description";
325 mode->desc_attr.attr.mode = 0444;
326 mode->desc_attr.show = typec_altmode_desc_show;
328 sysfs_attr_init(&mode->active_attr.attr);
329 mode->active_attr.attr.name = "active";
330 mode->active_attr.attr.mode = 0644;
331 mode->active_attr.show = typec_altmode_active_show;
332 mode->active_attr.store = typec_altmode_active_store;
334 mode->attrs[0] = &mode->vdo_attr.attr;
335 mode->attrs[1] = &mode->desc_attr.attr;
336 mode->attrs[2] = &mode->active_attr.attr;
338 /* With ports, list the roles that the mode is supported with */
340 sysfs_attr_init(&mode->roles_attr.attr);
341 mode->roles_attr.attr.name = "supported_roles";
342 mode->roles_attr.attr.mode = 0444;
343 mode->roles_attr.show = typec_altmode_roles_show;
345 mode->attrs[3] = &mode->roles_attr.attr;
348 mode->group.attrs = mode->attrs;
349 mode->group.name = mode->group_name;
351 alt->mode_groups[i] = &mode->group;
355 static ssize_t svid_show(struct device *dev, struct device_attribute *attr,
358 struct typec_altmode *alt = to_altmode(dev);
360 return sprintf(buf, "%04x\n", alt->svid);
362 static DEVICE_ATTR_RO(svid);
364 static struct attribute *typec_altmode_attrs[] = {
368 ATTRIBUTE_GROUPS(typec_altmode);
370 static void typec_altmode_release(struct device *dev)
372 struct typec_altmode *alt = to_altmode(dev);
375 for (i = 0; i < alt->n_modes; i++)
376 kfree(alt->modes[i].desc);
380 static const struct device_type typec_altmode_dev_type = {
381 .name = "typec_alternate_mode",
382 .groups = typec_altmode_groups,
383 .release = typec_altmode_release,
386 static struct typec_altmode *
387 typec_register_altmode(struct device *parent,
388 const struct typec_altmode_desc *desc)
390 struct typec_altmode *alt;
393 alt = kzalloc(sizeof(*alt), GFP_KERNEL);
395 return ERR_PTR(-ENOMEM);
397 alt->svid = desc->svid;
398 alt->n_modes = desc->n_modes;
399 typec_init_modes(alt, desc->modes, is_typec_port(parent));
401 alt->dev.parent = parent;
402 alt->dev.groups = alt->mode_groups;
403 alt->dev.type = &typec_altmode_dev_type;
404 dev_set_name(&alt->dev, "svid-%04x", alt->svid);
406 ret = device_register(&alt->dev);
408 dev_err(parent, "failed to register alternate mode (%d)\n",
410 put_device(&alt->dev);
418 * typec_unregister_altmode - Unregister Alternate Mode
419 * @alt: The alternate mode to be unregistered
421 * Unregister device created with typec_partner_register_altmode(),
422 * typec_plug_register_altmode() or typec_port_register_altmode().
424 void typec_unregister_altmode(struct typec_altmode *alt)
426 if (!IS_ERR_OR_NULL(alt))
427 device_unregister(&alt->dev);
429 EXPORT_SYMBOL_GPL(typec_unregister_altmode);
431 /* ------------------------------------------------------------------------- */
432 /* Type-C Partners */
434 static ssize_t accessory_mode_show(struct device *dev,
435 struct device_attribute *attr,
438 struct typec_partner *p = to_typec_partner(dev);
440 return sprintf(buf, "%s\n", typec_accessory_modes[p->accessory]);
442 static DEVICE_ATTR_RO(accessory_mode);
444 static ssize_t supports_usb_power_delivery_show(struct device *dev,
445 struct device_attribute *attr,
448 struct typec_partner *p = to_typec_partner(dev);
450 return sprintf(buf, "%s\n", p->usb_pd ? "yes" : "no");
452 static DEVICE_ATTR_RO(supports_usb_power_delivery);
454 static struct attribute *typec_partner_attrs[] = {
455 &dev_attr_accessory_mode.attr,
456 &dev_attr_supports_usb_power_delivery.attr,
459 ATTRIBUTE_GROUPS(typec_partner);
461 static void typec_partner_release(struct device *dev)
463 struct typec_partner *partner = to_typec_partner(dev);
468 static const struct device_type typec_partner_dev_type = {
469 .name = "typec_partner",
470 .groups = typec_partner_groups,
471 .release = typec_partner_release,
475 * typec_partner_set_identity - Report result from Discover Identity command
476 * @partner: The partner updated identity values
478 * This routine is used to report that the result of Discover Identity USB power
479 * delivery command has become available.
481 int typec_partner_set_identity(struct typec_partner *partner)
483 if (!partner->identity)
486 typec_report_identity(&partner->dev);
489 EXPORT_SYMBOL_GPL(typec_partner_set_identity);
492 * typec_partner_register_altmode - Register USB Type-C Partner Alternate Mode
493 * @partner: USB Type-C Partner that supports the alternate mode
494 * @desc: Description of the alternate mode
496 * This routine is used to register each alternate mode individually that
497 * @partner has listed in response to Discover SVIDs command. The modes for a
498 * SVID listed in response to Discover Modes command need to be listed in an
501 * Returns handle to the alternate mode on success or NULL on failure.
503 struct typec_altmode *
504 typec_partner_register_altmode(struct typec_partner *partner,
505 const struct typec_altmode_desc *desc)
507 return typec_register_altmode(&partner->dev, desc);
509 EXPORT_SYMBOL_GPL(typec_partner_register_altmode);
512 * typec_register_partner - Register a USB Type-C Partner
513 * @port: The USB Type-C Port the partner is connected to
514 * @desc: Description of the partner
516 * Registers a device for USB Type-C Partner described in @desc.
518 * Returns handle to the partner on success or ERR_PTR on failure.
520 struct typec_partner *typec_register_partner(struct typec_port *port,
521 struct typec_partner_desc *desc)
523 struct typec_partner *partner;
526 partner = kzalloc(sizeof(*partner), GFP_KERNEL);
528 return ERR_PTR(-ENOMEM);
530 partner->usb_pd = desc->usb_pd;
531 partner->accessory = desc->accessory;
533 if (desc->identity) {
535 * Creating directory for the identity only if the driver is
536 * able to provide data to it.
538 partner->dev.groups = usb_pd_id_groups;
539 partner->identity = desc->identity;
542 partner->dev.class = typec_class;
543 partner->dev.parent = &port->dev;
544 partner->dev.type = &typec_partner_dev_type;
545 dev_set_name(&partner->dev, "%s-partner", dev_name(&port->dev));
547 ret = device_register(&partner->dev);
549 dev_err(&port->dev, "failed to register partner (%d)\n", ret);
550 put_device(&partner->dev);
556 EXPORT_SYMBOL_GPL(typec_register_partner);
559 * typec_unregister_partner - Unregister a USB Type-C Partner
560 * @partner: The partner to be unregistered
562 * Unregister device created with typec_register_partner().
564 void typec_unregister_partner(struct typec_partner *partner)
566 if (!IS_ERR_OR_NULL(partner))
567 device_unregister(&partner->dev);
569 EXPORT_SYMBOL_GPL(typec_unregister_partner);
571 /* ------------------------------------------------------------------------- */
572 /* Type-C Cable Plugs */
574 static void typec_plug_release(struct device *dev)
576 struct typec_plug *plug = to_typec_plug(dev);
581 static const struct device_type typec_plug_dev_type = {
582 .name = "typec_plug",
583 .release = typec_plug_release,
587 * typec_plug_register_altmode - Register USB Type-C Cable Plug Alternate Mode
588 * @plug: USB Type-C Cable Plug that supports the alternate mode
589 * @desc: Description of the alternate mode
591 * This routine is used to register each alternate mode individually that @plug
592 * has listed in response to Discover SVIDs command. The modes for a SVID that
593 * the plug lists in response to Discover Modes command need to be listed in an
596 * Returns handle to the alternate mode on success or ERR_PTR on failure.
598 struct typec_altmode *
599 typec_plug_register_altmode(struct typec_plug *plug,
600 const struct typec_altmode_desc *desc)
602 return typec_register_altmode(&plug->dev, desc);
604 EXPORT_SYMBOL_GPL(typec_plug_register_altmode);
607 * typec_register_plug - Register a USB Type-C Cable Plug
608 * @cable: USB Type-C Cable with the plug
609 * @desc: Description of the cable plug
611 * Registers a device for USB Type-C Cable Plug described in @desc. A USB Type-C
612 * Cable Plug represents a plug with electronics in it that can response to USB
613 * Power Delivery SOP Prime or SOP Double Prime packages.
615 * Returns handle to the cable plug on success or ERR_PTR on failure.
617 struct typec_plug *typec_register_plug(struct typec_cable *cable,
618 struct typec_plug_desc *desc)
620 struct typec_plug *plug;
624 plug = kzalloc(sizeof(*plug), GFP_KERNEL);
626 return ERR_PTR(-ENOMEM);
628 sprintf(name, "plug%d", desc->index);
630 plug->index = desc->index;
631 plug->dev.class = typec_class;
632 plug->dev.parent = &cable->dev;
633 plug->dev.type = &typec_plug_dev_type;
634 dev_set_name(&plug->dev, "%s-%s", dev_name(cable->dev.parent), name);
636 ret = device_register(&plug->dev);
638 dev_err(&cable->dev, "failed to register plug (%d)\n", ret);
639 put_device(&plug->dev);
645 EXPORT_SYMBOL_GPL(typec_register_plug);
648 * typec_unregister_plug - Unregister a USB Type-C Cable Plug
649 * @plug: The cable plug to be unregistered
651 * Unregister device created with typec_register_plug().
653 void typec_unregister_plug(struct typec_plug *plug)
655 if (!IS_ERR_OR_NULL(plug))
656 device_unregister(&plug->dev);
658 EXPORT_SYMBOL_GPL(typec_unregister_plug);
663 type_show(struct device *dev, struct device_attribute *attr, char *buf)
665 struct typec_cable *cable = to_typec_cable(dev);
667 return sprintf(buf, "%s\n", cable->active ? "active" : "passive");
669 static DEVICE_ATTR_RO(type);
671 static const char * const typec_plug_types[] = {
672 [USB_PLUG_NONE] = "unknown",
673 [USB_PLUG_TYPE_A] = "type-a",
674 [USB_PLUG_TYPE_B] = "type-b",
675 [USB_PLUG_TYPE_C] = "type-c",
676 [USB_PLUG_CAPTIVE] = "captive",
679 static ssize_t plug_type_show(struct device *dev,
680 struct device_attribute *attr, char *buf)
682 struct typec_cable *cable = to_typec_cable(dev);
684 return sprintf(buf, "%s\n", typec_plug_types[cable->type]);
686 static DEVICE_ATTR_RO(plug_type);
688 static struct attribute *typec_cable_attrs[] = {
690 &dev_attr_plug_type.attr,
693 ATTRIBUTE_GROUPS(typec_cable);
695 static void typec_cable_release(struct device *dev)
697 struct typec_cable *cable = to_typec_cable(dev);
702 static const struct device_type typec_cable_dev_type = {
703 .name = "typec_cable",
704 .groups = typec_cable_groups,
705 .release = typec_cable_release,
709 * typec_cable_set_identity - Report result from Discover Identity command
710 * @cable: The cable updated identity values
712 * This routine is used to report that the result of Discover Identity USB power
713 * delivery command has become available.
715 int typec_cable_set_identity(struct typec_cable *cable)
717 if (!cable->identity)
720 typec_report_identity(&cable->dev);
723 EXPORT_SYMBOL_GPL(typec_cable_set_identity);
726 * typec_register_cable - Register a USB Type-C Cable
727 * @port: The USB Type-C Port the cable is connected to
728 * @desc: Description of the cable
730 * Registers a device for USB Type-C Cable described in @desc. The cable will be
731 * parent for the optional cable plug devises.
733 * Returns handle to the cable on success or ERR_PTR on failure.
735 struct typec_cable *typec_register_cable(struct typec_port *port,
736 struct typec_cable_desc *desc)
738 struct typec_cable *cable;
741 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
743 return ERR_PTR(-ENOMEM);
745 cable->type = desc->type;
746 cable->active = desc->active;
748 if (desc->identity) {
750 * Creating directory for the identity only if the driver is
751 * able to provide data to it.
753 cable->dev.groups = usb_pd_id_groups;
754 cable->identity = desc->identity;
757 cable->dev.class = typec_class;
758 cable->dev.parent = &port->dev;
759 cable->dev.type = &typec_cable_dev_type;
760 dev_set_name(&cable->dev, "%s-cable", dev_name(&port->dev));
762 ret = device_register(&cable->dev);
764 dev_err(&port->dev, "failed to register cable (%d)\n", ret);
765 put_device(&cable->dev);
771 EXPORT_SYMBOL_GPL(typec_register_cable);
774 * typec_unregister_cable - Unregister a USB Type-C Cable
775 * @cable: The cable to be unregistered
777 * Unregister device created with typec_register_cable().
779 void typec_unregister_cable(struct typec_cable *cable)
781 if (!IS_ERR_OR_NULL(cable))
782 device_unregister(&cable->dev);
784 EXPORT_SYMBOL_GPL(typec_unregister_cable);
786 /* ------------------------------------------------------------------------- */
787 /* USB Type-C ports */
789 static const char * const typec_roles[] = {
790 [TYPEC_SINK] = "sink",
791 [TYPEC_SOURCE] = "source",
794 static const char * const typec_data_roles[] = {
795 [TYPEC_DEVICE] = "device",
796 [TYPEC_HOST] = "host",
799 static const char * const typec_port_types[] = {
800 [TYPEC_PORT_SRC] = "source",
801 [TYPEC_PORT_SNK] = "sink",
802 [TYPEC_PORT_DRP] = "dual",
805 static const char * const typec_port_types_drp[] = {
806 [TYPEC_PORT_SRC] = "dual [source] sink",
807 [TYPEC_PORT_SNK] = "dual source [sink]",
808 [TYPEC_PORT_DRP] = "[dual] source sink",
812 preferred_role_store(struct device *dev, struct device_attribute *attr,
813 const char *buf, size_t size)
815 struct typec_port *port = to_typec_port(dev);
819 if (port->cap->type != TYPEC_PORT_DRP) {
820 dev_dbg(dev, "Preferred role only supported with DRP ports\n");
824 if (!port->cap->try_role) {
825 dev_dbg(dev, "Setting preferred role not supported\n");
829 role = sysfs_match_string(typec_roles, buf);
831 if (sysfs_streq(buf, "none"))
832 role = TYPEC_NO_PREFERRED_ROLE;
837 ret = port->cap->try_role(port->cap, role);
841 port->prefer_role = role;
846 preferred_role_show(struct device *dev, struct device_attribute *attr,
849 struct typec_port *port = to_typec_port(dev);
851 if (port->cap->type != TYPEC_PORT_DRP)
854 if (port->prefer_role < 0)
857 return sprintf(buf, "%s\n", typec_roles[port->prefer_role]);
859 static DEVICE_ATTR_RW(preferred_role);
861 static ssize_t data_role_store(struct device *dev,
862 struct device_attribute *attr,
863 const char *buf, size_t size)
865 struct typec_port *port = to_typec_port(dev);
868 if (!port->cap->dr_set) {
869 dev_dbg(dev, "data role swapping not supported\n");
873 ret = sysfs_match_string(typec_data_roles, buf);
877 mutex_lock(&port->port_type_lock);
878 if (port->cap->data != TYPEC_PORT_DRD) {
883 ret = port->cap->dr_set(port->cap, ret);
889 mutex_unlock(&port->port_type_lock);
893 static ssize_t data_role_show(struct device *dev,
894 struct device_attribute *attr, char *buf)
896 struct typec_port *port = to_typec_port(dev);
898 if (port->cap->data == TYPEC_PORT_DRD)
899 return sprintf(buf, "%s\n", port->data_role == TYPEC_HOST ?
900 "[host] device" : "host [device]");
902 return sprintf(buf, "[%s]\n", typec_data_roles[port->data_role]);
904 static DEVICE_ATTR_RW(data_role);
906 static ssize_t power_role_store(struct device *dev,
907 struct device_attribute *attr,
908 const char *buf, size_t size)
910 struct typec_port *port = to_typec_port(dev);
913 if (!port->cap->pd_revision) {
914 dev_dbg(dev, "USB Power Delivery not supported\n");
918 if (!port->cap->pr_set) {
919 dev_dbg(dev, "power role swapping not supported\n");
923 if (port->pwr_opmode != TYPEC_PWR_MODE_PD) {
924 dev_dbg(dev, "partner unable to swap power role\n");
928 ret = sysfs_match_string(typec_roles, buf);
932 mutex_lock(&port->port_type_lock);
933 if (port->port_type != TYPEC_PORT_DRP) {
934 dev_dbg(dev, "port type fixed at \"%s\"",
935 typec_port_types[port->port_type]);
940 ret = port->cap->pr_set(port->cap, ret);
946 mutex_unlock(&port->port_type_lock);
950 static ssize_t power_role_show(struct device *dev,
951 struct device_attribute *attr, char *buf)
953 struct typec_port *port = to_typec_port(dev);
955 if (port->cap->type == TYPEC_PORT_DRP)
956 return sprintf(buf, "%s\n", port->pwr_role == TYPEC_SOURCE ?
957 "[source] sink" : "source [sink]");
959 return sprintf(buf, "[%s]\n", typec_roles[port->pwr_role]);
961 static DEVICE_ATTR_RW(power_role);
964 port_type_store(struct device *dev, struct device_attribute *attr,
965 const char *buf, size_t size)
967 struct typec_port *port = to_typec_port(dev);
969 enum typec_port_type type;
971 if (!port->cap->port_type_set || port->cap->type != TYPEC_PORT_DRP) {
972 dev_dbg(dev, "changing port type not supported\n");
976 ret = sysfs_match_string(typec_port_types, buf);
981 mutex_lock(&port->port_type_lock);
983 if (port->port_type == type) {
988 ret = port->cap->port_type_set(port->cap, type);
992 port->port_type = type;
996 mutex_unlock(&port->port_type_lock);
1001 port_type_show(struct device *dev, struct device_attribute *attr,
1004 struct typec_port *port = to_typec_port(dev);
1006 if (port->cap->type == TYPEC_PORT_DRP)
1007 return sprintf(buf, "%s\n",
1008 typec_port_types_drp[port->port_type]);
1010 return sprintf(buf, "[%s]\n", typec_port_types[port->cap->type]);
1012 static DEVICE_ATTR_RW(port_type);
1014 static const char * const typec_pwr_opmodes[] = {
1015 [TYPEC_PWR_MODE_USB] = "default",
1016 [TYPEC_PWR_MODE_1_5A] = "1.5A",
1017 [TYPEC_PWR_MODE_3_0A] = "3.0A",
1018 [TYPEC_PWR_MODE_PD] = "usb_power_delivery",
1021 static ssize_t power_operation_mode_show(struct device *dev,
1022 struct device_attribute *attr,
1025 struct typec_port *port = to_typec_port(dev);
1027 return sprintf(buf, "%s\n", typec_pwr_opmodes[port->pwr_opmode]);
1029 static DEVICE_ATTR_RO(power_operation_mode);
1031 static ssize_t vconn_source_store(struct device *dev,
1032 struct device_attribute *attr,
1033 const char *buf, size_t size)
1035 struct typec_port *port = to_typec_port(dev);
1039 if (!port->cap->pd_revision) {
1040 dev_dbg(dev, "VCONN swap depends on USB Power Delivery\n");
1044 if (!port->cap->vconn_set) {
1045 dev_dbg(dev, "VCONN swapping not supported\n");
1049 ret = kstrtobool(buf, &source);
1053 ret = port->cap->vconn_set(port->cap, (enum typec_role)source);
1060 static ssize_t vconn_source_show(struct device *dev,
1061 struct device_attribute *attr, char *buf)
1063 struct typec_port *port = to_typec_port(dev);
1065 return sprintf(buf, "%s\n",
1066 port->vconn_role == TYPEC_SOURCE ? "yes" : "no");
1068 static DEVICE_ATTR_RW(vconn_source);
1070 static ssize_t supported_accessory_modes_show(struct device *dev,
1071 struct device_attribute *attr,
1074 struct typec_port *port = to_typec_port(dev);
1078 for (i = 0; i < ARRAY_SIZE(port->cap->accessory); i++) {
1079 if (port->cap->accessory[i])
1080 ret += sprintf(buf + ret, "%s ",
1081 typec_accessory_modes[port->cap->accessory[i]]);
1085 return sprintf(buf, "none\n");
1087 buf[ret - 1] = '\n';
1091 static DEVICE_ATTR_RO(supported_accessory_modes);
1093 static ssize_t usb_typec_revision_show(struct device *dev,
1094 struct device_attribute *attr,
1097 struct typec_port *port = to_typec_port(dev);
1098 u16 rev = port->cap->revision;
1100 return sprintf(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
1102 static DEVICE_ATTR_RO(usb_typec_revision);
1104 static ssize_t usb_power_delivery_revision_show(struct device *dev,
1105 struct device_attribute *attr,
1108 struct typec_port *p = to_typec_port(dev);
1110 return sprintf(buf, "%d\n", (p->cap->pd_revision >> 8) & 0xff);
1112 static DEVICE_ATTR_RO(usb_power_delivery_revision);
1114 static struct attribute *typec_attrs[] = {
1115 &dev_attr_data_role.attr,
1116 &dev_attr_power_operation_mode.attr,
1117 &dev_attr_power_role.attr,
1118 &dev_attr_preferred_role.attr,
1119 &dev_attr_supported_accessory_modes.attr,
1120 &dev_attr_usb_power_delivery_revision.attr,
1121 &dev_attr_usb_typec_revision.attr,
1122 &dev_attr_vconn_source.attr,
1123 &dev_attr_port_type.attr,
1126 ATTRIBUTE_GROUPS(typec);
1128 static int typec_uevent(struct device *dev, struct kobj_uevent_env *env)
1132 ret = add_uevent_var(env, "TYPEC_PORT=%s", dev_name(dev));
1134 dev_err(dev, "failed to add uevent TYPEC_PORT\n");
1139 static void typec_release(struct device *dev)
1141 struct typec_port *port = to_typec_port(dev);
1143 ida_simple_remove(&typec_index_ida, port->id);
1144 typec_switch_put(port->sw);
1145 typec_mux_put(port->mux);
1149 static const struct device_type typec_port_dev_type = {
1150 .name = "typec_port",
1151 .groups = typec_groups,
1152 .uevent = typec_uevent,
1153 .release = typec_release,
1156 /* --------------------------------------- */
1157 /* Driver callbacks to report role updates */
1160 * typec_set_data_role - Report data role change
1161 * @port: The USB Type-C Port where the role was changed
1162 * @role: The new data role
1164 * This routine is used by the port drivers to report data role changes.
1166 void typec_set_data_role(struct typec_port *port, enum typec_data_role role)
1168 if (port->data_role == role)
1171 port->data_role = role;
1172 sysfs_notify(&port->dev.kobj, NULL, "data_role");
1173 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1175 EXPORT_SYMBOL_GPL(typec_set_data_role);
1178 * typec_set_pwr_role - Report power role change
1179 * @port: The USB Type-C Port where the role was changed
1180 * @role: The new data role
1182 * This routine is used by the port drivers to report power role changes.
1184 void typec_set_pwr_role(struct typec_port *port, enum typec_role role)
1186 if (port->pwr_role == role)
1189 port->pwr_role = role;
1190 sysfs_notify(&port->dev.kobj, NULL, "power_role");
1191 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1193 EXPORT_SYMBOL_GPL(typec_set_pwr_role);
1196 * typec_set_pwr_role - Report VCONN source change
1197 * @port: The USB Type-C Port which VCONN role changed
1198 * @role: Source when @port is sourcing VCONN, or Sink when it's not
1200 * This routine is used by the port drivers to report if the VCONN source is
1203 void typec_set_vconn_role(struct typec_port *port, enum typec_role role)
1205 if (port->vconn_role == role)
1208 port->vconn_role = role;
1209 sysfs_notify(&port->dev.kobj, NULL, "vconn_source");
1210 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1212 EXPORT_SYMBOL_GPL(typec_set_vconn_role);
1214 static int partner_match(struct device *dev, void *data)
1216 return is_typec_partner(dev);
1220 * typec_set_pwr_opmode - Report changed power operation mode
1221 * @port: The USB Type-C Port where the mode was changed
1222 * @opmode: New power operation mode
1224 * This routine is used by the port drivers to report changed power operation
1225 * mode in @port. The modes are USB (default), 1.5A, 3.0A as defined in USB
1226 * Type-C specification, and "USB Power Delivery" when the power levels are
1227 * negotiated with methods defined in USB Power Delivery specification.
1229 void typec_set_pwr_opmode(struct typec_port *port,
1230 enum typec_pwr_opmode opmode)
1232 struct device *partner_dev;
1234 if (port->pwr_opmode == opmode)
1237 port->pwr_opmode = opmode;
1238 sysfs_notify(&port->dev.kobj, NULL, "power_operation_mode");
1239 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1241 partner_dev = device_find_child(&port->dev, NULL, partner_match);
1243 struct typec_partner *partner = to_typec_partner(partner_dev);
1245 if (opmode == TYPEC_PWR_MODE_PD && !partner->usb_pd) {
1246 partner->usb_pd = 1;
1247 sysfs_notify(&partner_dev->kobj, NULL,
1248 "supports_usb_power_delivery");
1250 put_device(partner_dev);
1253 EXPORT_SYMBOL_GPL(typec_set_pwr_opmode);
1255 /* ------------------------------------------ */
1256 /* API for Multiplexer/DeMultiplexer Switches */
1259 * typec_set_orientation - Set USB Type-C cable plug orientation
1260 * @port: USB Type-C Port
1261 * @orientation: USB Type-C cable plug orientation
1263 * Set cable plug orientation for @port.
1265 int typec_set_orientation(struct typec_port *port,
1266 enum typec_orientation orientation)
1271 ret = port->sw->set(port->sw, orientation);
1276 port->orientation = orientation;
1280 EXPORT_SYMBOL_GPL(typec_set_orientation);
1283 * typec_set_mode - Set mode of operation for USB Type-C connector
1284 * @port: USB Type-C port for the connector
1285 * @mode: Operation mode for the connector
1287 * Set mode @mode for @port. This function will configure the muxes needed to
1290 int typec_set_mode(struct typec_port *port, int mode)
1292 return port->mux ? port->mux->set(port->mux, mode) : 0;
1294 EXPORT_SYMBOL_GPL(typec_set_mode);
1296 /* --------------------------------------- */
1299 * typec_port_register_altmode - Register USB Type-C Port Alternate Mode
1300 * @port: USB Type-C Port that supports the alternate mode
1301 * @desc: Description of the alternate mode
1303 * This routine is used to register an alternate mode that @port is capable of
1306 * Returns handle to the alternate mode on success or ERR_PTR on failure.
1308 struct typec_altmode *
1309 typec_port_register_altmode(struct typec_port *port,
1310 const struct typec_altmode_desc *desc)
1312 return typec_register_altmode(&port->dev, desc);
1314 EXPORT_SYMBOL_GPL(typec_port_register_altmode);
1317 * typec_register_port - Register a USB Type-C Port
1318 * @parent: Parent device
1319 * @cap: Description of the port
1321 * Registers a device for USB Type-C Port described in @cap.
1323 * Returns handle to the port on success or ERR_PTR on failure.
1325 struct typec_port *typec_register_port(struct device *parent,
1326 const struct typec_capability *cap)
1328 struct typec_port *port;
1332 port = kzalloc(sizeof(*port), GFP_KERNEL);
1334 return ERR_PTR(-ENOMEM);
1336 id = ida_simple_get(&typec_index_ida, 0, 0, GFP_KERNEL);
1342 port->sw = typec_switch_get(cap->fwnode ? &port->dev : parent);
1343 if (IS_ERR(port->sw)) {
1344 ret = PTR_ERR(port->sw);
1348 port->mux = typec_mux_get(cap->fwnode ? &port->dev : parent);
1349 if (IS_ERR(port->mux)) {
1350 ret = PTR_ERR(port->mux);
1354 switch (cap->type) {
1355 case TYPEC_PORT_SRC:
1356 port->pwr_role = TYPEC_SOURCE;
1357 port->vconn_role = TYPEC_SOURCE;
1359 case TYPEC_PORT_SNK:
1360 port->pwr_role = TYPEC_SINK;
1361 port->vconn_role = TYPEC_SINK;
1363 case TYPEC_PORT_DRP:
1364 if (cap->prefer_role != TYPEC_NO_PREFERRED_ROLE)
1365 port->pwr_role = cap->prefer_role;
1367 port->pwr_role = TYPEC_SINK;
1371 switch (cap->data) {
1372 case TYPEC_PORT_DFP:
1373 port->data_role = TYPEC_HOST;
1375 case TYPEC_PORT_UFP:
1376 port->data_role = TYPEC_DEVICE;
1378 case TYPEC_PORT_DRD:
1379 if (cap->prefer_role == TYPEC_SOURCE)
1380 port->data_role = TYPEC_HOST;
1382 port->data_role = TYPEC_DEVICE;
1388 port->port_type = cap->type;
1389 mutex_init(&port->port_type_lock);
1390 port->prefer_role = cap->prefer_role;
1392 port->dev.class = typec_class;
1393 port->dev.parent = parent;
1394 port->dev.fwnode = cap->fwnode;
1395 port->dev.type = &typec_port_dev_type;
1396 dev_set_name(&port->dev, "port%d", id);
1398 ret = device_register(&port->dev);
1400 dev_err(parent, "failed to register port (%d)\n", ret);
1401 put_device(&port->dev);
1402 return ERR_PTR(ret);
1408 typec_switch_put(port->sw);
1411 ida_simple_remove(&typec_index_ida, port->id);
1414 return ERR_PTR(ret);
1416 EXPORT_SYMBOL_GPL(typec_register_port);
1419 * typec_unregister_port - Unregister a USB Type-C Port
1420 * @port: The port to be unregistered
1422 * Unregister device created with typec_register_port().
1424 void typec_unregister_port(struct typec_port *port)
1426 if (!IS_ERR_OR_NULL(port))
1427 device_unregister(&port->dev);
1429 EXPORT_SYMBOL_GPL(typec_unregister_port);
1431 static int __init typec_init(void)
1433 typec_class = class_create(THIS_MODULE, "typec");
1434 return PTR_ERR_OR_ZERO(typec_class);
1436 subsys_initcall(typec_init);
1438 static void __exit typec_exit(void)
1440 class_destroy(typec_class);
1441 ida_destroy(&typec_index_ida);
1443 module_exit(typec_exit);
1446 MODULE_LICENSE("GPL v2");
1447 MODULE_DESCRIPTION("USB Type-C Connector Class");