2 * USB Type-C Connector Class
4 * Copyright (C) 2017, Intel Corporation
5 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/device.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/usb/typec.h>
21 enum typec_port_type roles;
23 struct typec_altmode *alt_mode;
25 unsigned int active:1;
28 struct attribute_group group;
29 struct attribute *attrs[5];
30 struct device_attribute vdo_attr;
31 struct device_attribute desc_attr;
32 struct device_attribute active_attr;
33 struct device_attribute roles_attr;
36 struct typec_altmode {
40 struct typec_mode modes[ALTMODE_MAX_MODES];
41 const struct attribute_group *mode_groups[ALTMODE_MAX_MODES];
46 enum typec_plug_index index;
51 enum typec_plug_type type;
52 struct usb_pd_identity *identity;
53 unsigned int active:1;
56 struct typec_partner {
58 unsigned int usb_pd:1;
59 struct usb_pd_identity *identity;
60 enum typec_accessory accessory;
68 enum typec_data_role data_role;
69 enum typec_role pwr_role;
70 enum typec_role vconn_role;
71 enum typec_pwr_opmode pwr_opmode;
73 const struct typec_capability *cap;
76 #define to_typec_port(_dev_) container_of(_dev_, struct typec_port, dev)
77 #define to_typec_plug(_dev_) container_of(_dev_, struct typec_plug, dev)
78 #define to_typec_cable(_dev_) container_of(_dev_, struct typec_cable, dev)
79 #define to_typec_partner(_dev_) container_of(_dev_, struct typec_partner, dev)
80 #define to_altmode(_dev_) container_of(_dev_, struct typec_altmode, dev)
82 static const struct device_type typec_partner_dev_type;
83 static const struct device_type typec_cable_dev_type;
84 static const struct device_type typec_plug_dev_type;
85 static const struct device_type typec_port_dev_type;
87 #define is_typec_partner(_dev_) (_dev_->type == &typec_partner_dev_type)
88 #define is_typec_cable(_dev_) (_dev_->type == &typec_cable_dev_type)
89 #define is_typec_plug(_dev_) (_dev_->type == &typec_plug_dev_type)
90 #define is_typec_port(_dev_) (_dev_->type == &typec_port_dev_type)
92 static DEFINE_IDA(typec_index_ida);
93 static struct class *typec_class;
95 /* Common attributes */
97 static const char * const typec_accessory_modes[] = {
98 [TYPEC_ACCESSORY_NONE] = "none",
99 [TYPEC_ACCESSORY_AUDIO] = "analog_audio",
100 [TYPEC_ACCESSORY_DEBUG] = "debug",
103 static struct usb_pd_identity *get_pd_identity(struct device *dev)
105 if (is_typec_partner(dev)) {
106 struct typec_partner *partner = to_typec_partner(dev);
108 return partner->identity;
109 } else if (is_typec_cable(dev)) {
110 struct typec_cable *cable = to_typec_cable(dev);
112 return cable->identity;
117 static ssize_t id_header_show(struct device *dev, struct device_attribute *attr,
120 struct usb_pd_identity *id = get_pd_identity(dev);
122 return sprintf(buf, "0x%08x\n", id->id_header);
124 static DEVICE_ATTR_RO(id_header);
126 static ssize_t cert_stat_show(struct device *dev, struct device_attribute *attr,
129 struct usb_pd_identity *id = get_pd_identity(dev);
131 return sprintf(buf, "0x%08x\n", id->cert_stat);
133 static DEVICE_ATTR_RO(cert_stat);
135 static ssize_t product_show(struct device *dev, struct device_attribute *attr,
138 struct usb_pd_identity *id = get_pd_identity(dev);
140 return sprintf(buf, "0x%08x\n", id->product);
142 static DEVICE_ATTR_RO(product);
144 static struct attribute *usb_pd_id_attrs[] = {
145 &dev_attr_id_header.attr,
146 &dev_attr_cert_stat.attr,
147 &dev_attr_product.attr,
151 static const struct attribute_group usb_pd_id_group = {
153 .attrs = usb_pd_id_attrs,
156 static const struct attribute_group *usb_pd_id_groups[] = {
161 static void typec_report_identity(struct device *dev)
163 sysfs_notify(&dev->kobj, "identity", "id_header");
164 sysfs_notify(&dev->kobj, "identity", "cert_stat");
165 sysfs_notify(&dev->kobj, "identity", "product");
168 /* ------------------------------------------------------------------------- */
169 /* Alternate Modes */
172 * typec_altmode_update_active - Report Enter/Exit mode
173 * @alt: Handle to the alternate mode
175 * @active: True when the mode has been entered
177 * If a partner or cable plug executes Enter/Exit Mode command successfully, the
178 * drivers use this routine to report the updated state of the mode.
180 void typec_altmode_update_active(struct typec_altmode *alt, int mode,
183 struct typec_mode *m = &alt->modes[mode];
186 if (m->active == active)
190 snprintf(dir, sizeof(dir), "mode%d", mode);
191 sysfs_notify(&alt->dev.kobj, dir, "active");
192 kobject_uevent(&alt->dev.kobj, KOBJ_CHANGE);
194 EXPORT_SYMBOL_GPL(typec_altmode_update_active);
197 * typec_altmode2port - Alternate Mode to USB Type-C port
198 * @alt: The Alternate Mode
200 * Returns handle to the port that a cable plug or partner with @alt is
203 struct typec_port *typec_altmode2port(struct typec_altmode *alt)
205 if (is_typec_plug(alt->dev.parent))
206 return to_typec_port(alt->dev.parent->parent->parent);
207 if (is_typec_partner(alt->dev.parent))
208 return to_typec_port(alt->dev.parent->parent);
209 if (is_typec_port(alt->dev.parent))
210 return to_typec_port(alt->dev.parent);
214 EXPORT_SYMBOL_GPL(typec_altmode2port);
217 typec_altmode_vdo_show(struct device *dev, struct device_attribute *attr,
220 struct typec_mode *mode = container_of(attr, struct typec_mode,
223 return sprintf(buf, "0x%08x\n", mode->vdo);
227 typec_altmode_desc_show(struct device *dev, struct device_attribute *attr,
230 struct typec_mode *mode = container_of(attr, struct typec_mode,
233 return sprintf(buf, "%s\n", mode->desc ? mode->desc : "");
237 typec_altmode_active_show(struct device *dev, struct device_attribute *attr,
240 struct typec_mode *mode = container_of(attr, struct typec_mode,
243 return sprintf(buf, "%s\n", mode->active ? "yes" : "no");
247 typec_altmode_active_store(struct device *dev, struct device_attribute *attr,
248 const char *buf, size_t size)
250 struct typec_mode *mode = container_of(attr, struct typec_mode,
252 struct typec_port *port = typec_altmode2port(mode->alt_mode);
256 if (!port->cap->activate_mode)
259 ret = kstrtobool(buf, &activate);
263 ret = port->cap->activate_mode(port->cap, mode->index, activate);
271 typec_altmode_roles_show(struct device *dev, struct device_attribute *attr,
274 struct typec_mode *mode = container_of(attr, struct typec_mode,
278 switch (mode->roles) {
280 ret = sprintf(buf, "source\n");
283 ret = sprintf(buf, "sink\n");
287 ret = sprintf(buf, "source sink\n");
293 static void typec_init_modes(struct typec_altmode *alt,
294 struct typec_mode_desc *desc, bool is_port)
298 for (i = 0; i < alt->n_modes; i++, desc++) {
299 struct typec_mode *mode = &alt->modes[i];
301 /* Not considering the human readable description critical */
302 mode->desc = kstrdup(desc->desc, GFP_KERNEL);
303 if (desc->desc && !mode->desc)
304 dev_err(&alt->dev, "failed to copy mode%d desc\n", i);
306 mode->alt_mode = alt;
307 mode->vdo = desc->vdo;
308 mode->roles = desc->roles;
309 mode->index = desc->index;
310 sprintf(mode->group_name, "mode%d", desc->index);
312 sysfs_attr_init(&mode->vdo_attr.attr);
313 mode->vdo_attr.attr.name = "vdo";
314 mode->vdo_attr.attr.mode = 0444;
315 mode->vdo_attr.show = typec_altmode_vdo_show;
317 sysfs_attr_init(&mode->desc_attr.attr);
318 mode->desc_attr.attr.name = "description";
319 mode->desc_attr.attr.mode = 0444;
320 mode->desc_attr.show = typec_altmode_desc_show;
322 sysfs_attr_init(&mode->active_attr.attr);
323 mode->active_attr.attr.name = "active";
324 mode->active_attr.attr.mode = 0644;
325 mode->active_attr.show = typec_altmode_active_show;
326 mode->active_attr.store = typec_altmode_active_store;
328 mode->attrs[0] = &mode->vdo_attr.attr;
329 mode->attrs[1] = &mode->desc_attr.attr;
330 mode->attrs[2] = &mode->active_attr.attr;
332 /* With ports, list the roles that the mode is supported with */
334 sysfs_attr_init(&mode->roles_attr.attr);
335 mode->roles_attr.attr.name = "supported_roles";
336 mode->roles_attr.attr.mode = 0444;
337 mode->roles_attr.show = typec_altmode_roles_show;
339 mode->attrs[3] = &mode->roles_attr.attr;
342 mode->group.attrs = mode->attrs;
343 mode->group.name = mode->group_name;
345 alt->mode_groups[i] = &mode->group;
349 static ssize_t svid_show(struct device *dev, struct device_attribute *attr,
352 struct typec_altmode *alt = to_altmode(dev);
354 return sprintf(buf, "%04x\n", alt->svid);
356 static DEVICE_ATTR_RO(svid);
358 static struct attribute *typec_altmode_attrs[] = {
362 ATTRIBUTE_GROUPS(typec_altmode);
364 static void typec_altmode_release(struct device *dev)
366 struct typec_altmode *alt = to_altmode(dev);
369 for (i = 0; i < alt->n_modes; i++)
370 kfree(alt->modes[i].desc);
374 static const struct device_type typec_altmode_dev_type = {
375 .name = "typec_alternate_mode",
376 .groups = typec_altmode_groups,
377 .release = typec_altmode_release,
380 static struct typec_altmode *
381 typec_register_altmode(struct device *parent, struct typec_altmode_desc *desc)
383 struct typec_altmode *alt;
386 alt = kzalloc(sizeof(*alt), GFP_KERNEL);
390 alt->svid = desc->svid;
391 alt->n_modes = desc->n_modes;
392 typec_init_modes(alt, desc->modes, is_typec_port(parent));
394 alt->dev.parent = parent;
395 alt->dev.groups = alt->mode_groups;
396 alt->dev.type = &typec_altmode_dev_type;
397 dev_set_name(&alt->dev, "svid-%04x", alt->svid);
399 ret = device_register(&alt->dev);
401 dev_err(parent, "failed to register alternate mode (%d)\n",
403 put_device(&alt->dev);
411 * typec_unregister_altmode - Unregister Alternate Mode
412 * @alt: The alternate mode to be unregistered
414 * Unregister device created with typec_partner_register_altmode(),
415 * typec_plug_register_altmode() or typec_port_register_altmode().
417 void typec_unregister_altmode(struct typec_altmode *alt)
420 device_unregister(&alt->dev);
422 EXPORT_SYMBOL_GPL(typec_unregister_altmode);
424 /* ------------------------------------------------------------------------- */
425 /* Type-C Partners */
427 static ssize_t accessory_mode_show(struct device *dev,
428 struct device_attribute *attr,
431 struct typec_partner *p = to_typec_partner(dev);
433 return sprintf(buf, "%s\n", typec_accessory_modes[p->accessory]);
435 static DEVICE_ATTR_RO(accessory_mode);
437 static ssize_t supports_usb_power_delivery_show(struct device *dev,
438 struct device_attribute *attr,
441 struct typec_partner *p = to_typec_partner(dev);
443 return sprintf(buf, "%s\n", p->usb_pd ? "yes" : "no");
445 static DEVICE_ATTR_RO(supports_usb_power_delivery);
447 static struct attribute *typec_partner_attrs[] = {
448 &dev_attr_accessory_mode.attr,
449 &dev_attr_supports_usb_power_delivery.attr,
452 ATTRIBUTE_GROUPS(typec_partner);
454 static void typec_partner_release(struct device *dev)
456 struct typec_partner *partner = to_typec_partner(dev);
461 static const struct device_type typec_partner_dev_type = {
462 .name = "typec_partner",
463 .groups = typec_partner_groups,
464 .release = typec_partner_release,
468 * typec_partner_set_identity - Report result from Discover Identity command
469 * @partner: The partner updated identity values
471 * This routine is used to report that the result of Discover Identity USB power
472 * delivery command has become available.
474 int typec_partner_set_identity(struct typec_partner *partner)
476 if (!partner->identity)
479 typec_report_identity(&partner->dev);
482 EXPORT_SYMBOL_GPL(typec_partner_set_identity);
485 * typec_partner_register_altmode - Register USB Type-C Partner Alternate Mode
486 * @partner: USB Type-C Partner that supports the alternate mode
487 * @desc: Description of the alternate mode
489 * This routine is used to register each alternate mode individually that
490 * @partner has listed in response to Discover SVIDs command. The modes for a
491 * SVID listed in response to Discover Modes command need to be listed in an
494 * Returns handle to the alternate mode on success or NULL on failure.
496 struct typec_altmode *
497 typec_partner_register_altmode(struct typec_partner *partner,
498 struct typec_altmode_desc *desc)
500 return typec_register_altmode(&partner->dev, desc);
502 EXPORT_SYMBOL_GPL(typec_partner_register_altmode);
505 * typec_register_partner - Register a USB Type-C Partner
506 * @port: The USB Type-C Port the partner is connected to
507 * @desc: Description of the partner
509 * Registers a device for USB Type-C Partner described in @desc.
511 * Returns handle to the partner on success or NULL on failure.
513 struct typec_partner *typec_register_partner(struct typec_port *port,
514 struct typec_partner_desc *desc)
516 struct typec_partner *partner;
519 partner = kzalloc(sizeof(*partner), GFP_KERNEL);
523 partner->usb_pd = desc->usb_pd;
524 partner->accessory = desc->accessory;
526 if (desc->identity) {
528 * Creating directory for the identity only if the driver is
529 * able to provide data to it.
531 partner->dev.groups = usb_pd_id_groups;
532 partner->identity = desc->identity;
535 partner->dev.class = typec_class;
536 partner->dev.parent = &port->dev;
537 partner->dev.type = &typec_partner_dev_type;
538 dev_set_name(&partner->dev, "%s-partner", dev_name(&port->dev));
540 ret = device_register(&partner->dev);
542 dev_err(&port->dev, "failed to register partner (%d)\n", ret);
543 put_device(&partner->dev);
549 EXPORT_SYMBOL_GPL(typec_register_partner);
552 * typec_unregister_partner - Unregister a USB Type-C Partner
553 * @partner: The partner to be unregistered
555 * Unregister device created with typec_register_partner().
557 void typec_unregister_partner(struct typec_partner *partner)
560 device_unregister(&partner->dev);
562 EXPORT_SYMBOL_GPL(typec_unregister_partner);
564 /* ------------------------------------------------------------------------- */
565 /* Type-C Cable Plugs */
567 static void typec_plug_release(struct device *dev)
569 struct typec_plug *plug = to_typec_plug(dev);
574 static const struct device_type typec_plug_dev_type = {
575 .name = "typec_plug",
576 .release = typec_plug_release,
580 * typec_plug_register_altmode - Register USB Type-C Cable Plug Alternate Mode
581 * @plug: USB Type-C Cable Plug that supports the alternate mode
582 * @desc: Description of the alternate mode
584 * This routine is used to register each alternate mode individually that @plug
585 * has listed in response to Discover SVIDs command. The modes for a SVID that
586 * the plug lists in response to Discover Modes command need to be listed in an
589 * Returns handle to the alternate mode on success or NULL on failure.
591 struct typec_altmode *
592 typec_plug_register_altmode(struct typec_plug *plug,
593 struct typec_altmode_desc *desc)
595 return typec_register_altmode(&plug->dev, desc);
597 EXPORT_SYMBOL_GPL(typec_plug_register_altmode);
600 * typec_register_plug - Register a USB Type-C Cable Plug
601 * @cable: USB Type-C Cable with the plug
602 * @desc: Description of the cable plug
604 * Registers a device for USB Type-C Cable Plug described in @desc. A USB Type-C
605 * Cable Plug represents a plug with electronics in it that can response to USB
606 * Power Delivery SOP Prime or SOP Double Prime packages.
608 * Returns handle to the cable plug on success or NULL on failure.
610 struct typec_plug *typec_register_plug(struct typec_cable *cable,
611 struct typec_plug_desc *desc)
613 struct typec_plug *plug;
617 plug = kzalloc(sizeof(*plug), GFP_KERNEL);
621 sprintf(name, "plug%d", desc->index);
623 plug->index = desc->index;
624 plug->dev.class = typec_class;
625 plug->dev.parent = &cable->dev;
626 plug->dev.type = &typec_plug_dev_type;
627 dev_set_name(&plug->dev, "%s-%s", dev_name(cable->dev.parent), name);
629 ret = device_register(&plug->dev);
631 dev_err(&cable->dev, "failed to register plug (%d)\n", ret);
632 put_device(&plug->dev);
638 EXPORT_SYMBOL_GPL(typec_register_plug);
641 * typec_unregister_plug - Unregister a USB Type-C Cable Plug
642 * @plug: The cable plug to be unregistered
644 * Unregister device created with typec_register_plug().
646 void typec_unregister_plug(struct typec_plug *plug)
649 device_unregister(&plug->dev);
651 EXPORT_SYMBOL_GPL(typec_unregister_plug);
656 type_show(struct device *dev, struct device_attribute *attr, char *buf)
658 struct typec_cable *cable = to_typec_cable(dev);
660 return sprintf(buf, "%s\n", cable->active ? "active" : "passive");
662 static DEVICE_ATTR_RO(type);
664 static const char * const typec_plug_types[] = {
665 [USB_PLUG_NONE] = "unknown",
666 [USB_PLUG_TYPE_A] = "type-a",
667 [USB_PLUG_TYPE_B] = "type-b",
668 [USB_PLUG_TYPE_C] = "type-c",
669 [USB_PLUG_CAPTIVE] = "captive",
672 static ssize_t plug_type_show(struct device *dev,
673 struct device_attribute *attr, char *buf)
675 struct typec_cable *cable = to_typec_cable(dev);
677 return sprintf(buf, "%s\n", typec_plug_types[cable->type]);
679 static DEVICE_ATTR_RO(plug_type);
681 static struct attribute *typec_cable_attrs[] = {
683 &dev_attr_plug_type.attr,
686 ATTRIBUTE_GROUPS(typec_cable);
688 static void typec_cable_release(struct device *dev)
690 struct typec_cable *cable = to_typec_cable(dev);
695 static const struct device_type typec_cable_dev_type = {
696 .name = "typec_cable",
697 .groups = typec_cable_groups,
698 .release = typec_cable_release,
702 * typec_cable_set_identity - Report result from Discover Identity command
703 * @cable: The cable updated identity values
705 * This routine is used to report that the result of Discover Identity USB power
706 * delivery command has become available.
708 int typec_cable_set_identity(struct typec_cable *cable)
710 if (!cable->identity)
713 typec_report_identity(&cable->dev);
716 EXPORT_SYMBOL_GPL(typec_cable_set_identity);
719 * typec_register_cable - Register a USB Type-C Cable
720 * @port: The USB Type-C Port the cable is connected to
721 * @desc: Description of the cable
723 * Registers a device for USB Type-C Cable described in @desc. The cable will be
724 * parent for the optional cable plug devises.
726 * Returns handle to the cable on success or NULL on failure.
728 struct typec_cable *typec_register_cable(struct typec_port *port,
729 struct typec_cable_desc *desc)
731 struct typec_cable *cable;
734 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
738 cable->type = desc->type;
739 cable->active = desc->active;
741 if (desc->identity) {
743 * Creating directory for the identity only if the driver is
744 * able to provide data to it.
746 cable->dev.groups = usb_pd_id_groups;
747 cable->identity = desc->identity;
750 cable->dev.class = typec_class;
751 cable->dev.parent = &port->dev;
752 cable->dev.type = &typec_cable_dev_type;
753 dev_set_name(&cable->dev, "%s-cable", dev_name(&port->dev));
755 ret = device_register(&cable->dev);
757 dev_err(&port->dev, "failed to register cable (%d)\n", ret);
758 put_device(&cable->dev);
764 EXPORT_SYMBOL_GPL(typec_register_cable);
767 * typec_unregister_cable - Unregister a USB Type-C Cable
768 * @cable: The cable to be unregistered
770 * Unregister device created with typec_register_cable().
772 void typec_unregister_cable(struct typec_cable *cable)
775 device_unregister(&cable->dev);
777 EXPORT_SYMBOL_GPL(typec_unregister_cable);
779 /* ------------------------------------------------------------------------- */
780 /* USB Type-C ports */
782 static const char * const typec_roles[] = {
783 [TYPEC_SINK] = "sink",
784 [TYPEC_SOURCE] = "source",
787 static const char * const typec_data_roles[] = {
788 [TYPEC_DEVICE] = "device",
789 [TYPEC_HOST] = "host",
793 preferred_role_store(struct device *dev, struct device_attribute *attr,
794 const char *buf, size_t size)
796 struct typec_port *port = to_typec_port(dev);
800 if (port->cap->type != TYPEC_PORT_DRP) {
801 dev_dbg(dev, "Preferred role only supported with DRP ports\n");
805 if (!port->cap->try_role) {
806 dev_dbg(dev, "Setting preferred role not supported\n");
810 role = sysfs_match_string(typec_roles, buf);
812 if (sysfs_streq(buf, "none"))
813 role = TYPEC_NO_PREFERRED_ROLE;
818 ret = port->cap->try_role(port->cap, role);
822 port->prefer_role = role;
827 preferred_role_show(struct device *dev, struct device_attribute *attr,
830 struct typec_port *port = to_typec_port(dev);
832 if (port->cap->type != TYPEC_PORT_DRP)
835 if (port->prefer_role < 0)
838 return sprintf(buf, "%s\n", typec_roles[port->prefer_role]);
840 static DEVICE_ATTR_RW(preferred_role);
842 static ssize_t data_role_store(struct device *dev,
843 struct device_attribute *attr,
844 const char *buf, size_t size)
846 struct typec_port *port = to_typec_port(dev);
849 if (port->cap->type != TYPEC_PORT_DRP) {
850 dev_dbg(dev, "data role swap only supported with DRP ports\n");
854 if (!port->cap->dr_set) {
855 dev_dbg(dev, "data role swapping not supported\n");
859 ret = sysfs_match_string(typec_data_roles, buf);
863 ret = port->cap->dr_set(port->cap, ret);
870 static ssize_t data_role_show(struct device *dev,
871 struct device_attribute *attr, char *buf)
873 struct typec_port *port = to_typec_port(dev);
875 if (port->cap->type == TYPEC_PORT_DRP)
876 return sprintf(buf, "%s\n", port->data_role == TYPEC_HOST ?
877 "[host] device" : "host [device]");
879 return sprintf(buf, "[%s]\n", typec_data_roles[port->data_role]);
881 static DEVICE_ATTR_RW(data_role);
883 static ssize_t power_role_store(struct device *dev,
884 struct device_attribute *attr,
885 const char *buf, size_t size)
887 struct typec_port *port = to_typec_port(dev);
890 if (!port->cap->pd_revision) {
891 dev_dbg(dev, "USB Power Delivery not supported\n");
895 if (!port->cap->pr_set) {
896 dev_dbg(dev, "power role swapping not supported\n");
900 if (port->pwr_opmode != TYPEC_PWR_MODE_PD) {
901 dev_dbg(dev, "partner unable to swap power role\n");
905 ret = sysfs_match_string(typec_roles, buf);
909 ret = port->cap->pr_set(port->cap, ret);
916 static ssize_t power_role_show(struct device *dev,
917 struct device_attribute *attr, char *buf)
919 struct typec_port *port = to_typec_port(dev);
921 if (port->cap->type == TYPEC_PORT_DRP)
922 return sprintf(buf, "%s\n", port->pwr_role == TYPEC_SOURCE ?
923 "[source] sink" : "source [sink]");
925 return sprintf(buf, "[%s]\n", typec_roles[port->pwr_role]);
927 static DEVICE_ATTR_RW(power_role);
929 static const char * const typec_pwr_opmodes[] = {
930 [TYPEC_PWR_MODE_USB] = "default",
931 [TYPEC_PWR_MODE_1_5A] = "1.5A",
932 [TYPEC_PWR_MODE_3_0A] = "3.0A",
933 [TYPEC_PWR_MODE_PD] = "usb_power_delivery",
936 static ssize_t power_operation_mode_show(struct device *dev,
937 struct device_attribute *attr,
940 struct typec_port *port = to_typec_port(dev);
942 return sprintf(buf, "%s\n", typec_pwr_opmodes[port->pwr_opmode]);
944 static DEVICE_ATTR_RO(power_operation_mode);
946 static ssize_t vconn_source_store(struct device *dev,
947 struct device_attribute *attr,
948 const char *buf, size_t size)
950 struct typec_port *port = to_typec_port(dev);
954 if (!port->cap->pd_revision) {
955 dev_dbg(dev, "VCONN swap depends on USB Power Delivery\n");
959 if (!port->cap->vconn_set) {
960 dev_dbg(dev, "VCONN swapping not supported\n");
964 ret = kstrtobool(buf, &source);
968 ret = port->cap->vconn_set(port->cap, (enum typec_role)source);
975 static ssize_t vconn_source_show(struct device *dev,
976 struct device_attribute *attr, char *buf)
978 struct typec_port *port = to_typec_port(dev);
980 return sprintf(buf, "%s\n",
981 port->vconn_role == TYPEC_SOURCE ? "yes" : "no");
983 static DEVICE_ATTR_RW(vconn_source);
985 static ssize_t supported_accessory_modes_show(struct device *dev,
986 struct device_attribute *attr,
989 struct typec_port *port = to_typec_port(dev);
993 for (i = 0; i < ARRAY_SIZE(port->cap->accessory); i++) {
994 if (port->cap->accessory[i])
995 ret += sprintf(buf + ret, "%s ",
996 typec_accessory_modes[port->cap->accessory[i]]);
1000 return sprintf(buf, "none\n");
1002 buf[ret - 1] = '\n';
1006 static DEVICE_ATTR_RO(supported_accessory_modes);
1008 static ssize_t usb_typec_revision_show(struct device *dev,
1009 struct device_attribute *attr,
1012 struct typec_port *port = to_typec_port(dev);
1013 u16 rev = port->cap->revision;
1015 return sprintf(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
1017 static DEVICE_ATTR_RO(usb_typec_revision);
1019 static ssize_t usb_power_delivery_revision_show(struct device *dev,
1020 struct device_attribute *attr,
1023 struct typec_port *p = to_typec_port(dev);
1025 return sprintf(buf, "%d\n", (p->cap->pd_revision >> 8) & 0xff);
1027 static DEVICE_ATTR_RO(usb_power_delivery_revision);
1029 static struct attribute *typec_attrs[] = {
1030 &dev_attr_data_role.attr,
1031 &dev_attr_power_operation_mode.attr,
1032 &dev_attr_power_role.attr,
1033 &dev_attr_preferred_role.attr,
1034 &dev_attr_supported_accessory_modes.attr,
1035 &dev_attr_usb_power_delivery_revision.attr,
1036 &dev_attr_usb_typec_revision.attr,
1037 &dev_attr_vconn_source.attr,
1040 ATTRIBUTE_GROUPS(typec);
1042 static int typec_uevent(struct device *dev, struct kobj_uevent_env *env)
1046 ret = add_uevent_var(env, "TYPEC_PORT=%s", dev_name(dev));
1048 dev_err(dev, "failed to add uevent TYPEC_PORT\n");
1053 static void typec_release(struct device *dev)
1055 struct typec_port *port = to_typec_port(dev);
1057 ida_simple_remove(&typec_index_ida, port->id);
1061 static const struct device_type typec_port_dev_type = {
1062 .name = "typec_port",
1063 .groups = typec_groups,
1064 .uevent = typec_uevent,
1065 .release = typec_release,
1068 /* --------------------------------------- */
1069 /* Driver callbacks to report role updates */
1072 * typec_set_data_role - Report data role change
1073 * @port: The USB Type-C Port where the role was changed
1074 * @role: The new data role
1076 * This routine is used by the port drivers to report data role changes.
1078 void typec_set_data_role(struct typec_port *port, enum typec_data_role role)
1080 if (port->data_role == role)
1083 port->data_role = role;
1084 sysfs_notify(&port->dev.kobj, NULL, "data_role");
1085 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1087 EXPORT_SYMBOL_GPL(typec_set_data_role);
1090 * typec_set_pwr_role - Report power role change
1091 * @port: The USB Type-C Port where the role was changed
1092 * @role: The new data role
1094 * This routine is used by the port drivers to report power role changes.
1096 void typec_set_pwr_role(struct typec_port *port, enum typec_role role)
1098 if (port->pwr_role == role)
1101 port->pwr_role = role;
1102 sysfs_notify(&port->dev.kobj, NULL, "power_role");
1103 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1105 EXPORT_SYMBOL_GPL(typec_set_pwr_role);
1108 * typec_set_pwr_role - Report VCONN source change
1109 * @port: The USB Type-C Port which VCONN role changed
1110 * @role: Source when @port is sourcing VCONN, or Sink when it's not
1112 * This routine is used by the port drivers to report if the VCONN source is
1115 void typec_set_vconn_role(struct typec_port *port, enum typec_role role)
1117 if (port->vconn_role == role)
1120 port->vconn_role = role;
1121 sysfs_notify(&port->dev.kobj, NULL, "vconn_source");
1122 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1124 EXPORT_SYMBOL_GPL(typec_set_vconn_role);
1127 * typec_set_pwr_opmode - Report changed power operation mode
1128 * @port: The USB Type-C Port where the mode was changed
1129 * @opmode: New power operation mode
1131 * This routine is used by the port drivers to report changed power operation
1132 * mode in @port. The modes are USB (default), 1.5A, 3.0A as defined in USB
1133 * Type-C specification, and "USB Power Delivery" when the power levels are
1134 * negotiated with methods defined in USB Power Delivery specification.
1136 void typec_set_pwr_opmode(struct typec_port *port,
1137 enum typec_pwr_opmode opmode)
1139 if (port->pwr_opmode == opmode)
1142 port->pwr_opmode = opmode;
1143 sysfs_notify(&port->dev.kobj, NULL, "power_operation_mode");
1144 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1146 EXPORT_SYMBOL_GPL(typec_set_pwr_opmode);
1148 /* --------------------------------------- */
1151 * typec_port_register_altmode - Register USB Type-C Port Alternate Mode
1152 * @port: USB Type-C Port that supports the alternate mode
1153 * @desc: Description of the alternate mode
1155 * This routine is used to register an alternate mode that @port is capable of
1158 * Returns handle to the alternate mode on success or NULL on failure.
1160 struct typec_altmode *
1161 typec_port_register_altmode(struct typec_port *port,
1162 struct typec_altmode_desc *desc)
1164 return typec_register_altmode(&port->dev, desc);
1166 EXPORT_SYMBOL_GPL(typec_port_register_altmode);
1169 * typec_register_port - Register a USB Type-C Port
1170 * @parent: Parent device
1171 * @cap: Description of the port
1173 * Registers a device for USB Type-C Port described in @cap.
1175 * Returns handle to the port on success or NULL on failure.
1177 struct typec_port *typec_register_port(struct device *parent,
1178 const struct typec_capability *cap)
1180 struct typec_port *port;
1185 port = kzalloc(sizeof(*port), GFP_KERNEL);
1189 id = ida_simple_get(&typec_index_ida, 0, 0, GFP_KERNEL);
1195 if (cap->type == TYPEC_PORT_DFP)
1196 role = TYPEC_SOURCE;
1197 else if (cap->type == TYPEC_PORT_UFP)
1200 role = cap->prefer_role;
1202 if (role == TYPEC_SOURCE) {
1203 port->data_role = TYPEC_HOST;
1204 port->pwr_role = TYPEC_SOURCE;
1205 port->vconn_role = TYPEC_SOURCE;
1207 port->data_role = TYPEC_DEVICE;
1208 port->pwr_role = TYPEC_SINK;
1209 port->vconn_role = TYPEC_SINK;
1214 port->prefer_role = cap->prefer_role;
1216 port->dev.class = typec_class;
1217 port->dev.parent = parent;
1218 port->dev.fwnode = cap->fwnode;
1219 port->dev.type = &typec_port_dev_type;
1220 dev_set_name(&port->dev, "port%d", id);
1222 ret = device_register(&port->dev);
1224 dev_err(parent, "failed to register port (%d)\n", ret);
1225 put_device(&port->dev);
1231 EXPORT_SYMBOL_GPL(typec_register_port);
1234 * typec_unregister_port - Unregister a USB Type-C Port
1235 * @port: The port to be unregistered
1237 * Unregister device created with typec_register_port().
1239 void typec_unregister_port(struct typec_port *port)
1242 device_unregister(&port->dev);
1244 EXPORT_SYMBOL_GPL(typec_unregister_port);
1246 static int __init typec_init(void)
1248 typec_class = class_create(THIS_MODULE, "typec");
1249 return PTR_ERR_OR_ZERO(typec_class);
1251 subsys_initcall(typec_init);
1253 static void __exit typec_exit(void)
1255 class_destroy(typec_class);
1256 ida_destroy(&typec_index_ida);
1258 module_exit(typec_exit);
1260 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1261 MODULE_LICENSE("GPL v2");
1262 MODULE_DESCRIPTION("USB Type-C Connector Class");