Linux 4.19.133
[linux/fpc-iii.git] / drivers / usb / typec / class.c
blob1916ee1600b47c201203b35d7eb7664b636619dd
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * USB Type-C Connector Class
5 * Copyright (C) 2017, Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
7 */
9 #include <linux/device.h>
10 #include <linux/module.h>
11 #include <linux/mutex.h>
12 #include <linux/slab.h>
14 #include "bus.h"
16 struct typec_plug {
17 struct device dev;
18 enum typec_plug_index index;
19 struct ida mode_ids;
22 struct typec_cable {
23 struct device dev;
24 enum typec_plug_type type;
25 struct usb_pd_identity *identity;
26 unsigned int active:1;
29 struct typec_partner {
30 struct device dev;
31 unsigned int usb_pd:1;
32 struct usb_pd_identity *identity;
33 enum typec_accessory accessory;
34 struct ida mode_ids;
37 struct typec_port {
38 unsigned int id;
39 struct device dev;
40 struct ida mode_ids;
42 int prefer_role;
43 enum typec_data_role data_role;
44 enum typec_role pwr_role;
45 enum typec_role vconn_role;
46 enum typec_pwr_opmode pwr_opmode;
47 enum typec_port_type port_type;
48 struct mutex port_type_lock;
50 enum typec_orientation orientation;
51 struct typec_switch *sw;
52 struct typec_mux *mux;
54 const struct typec_capability *cap;
57 #define to_typec_port(_dev_) container_of(_dev_, struct typec_port, dev)
58 #define to_typec_plug(_dev_) container_of(_dev_, struct typec_plug, dev)
59 #define to_typec_cable(_dev_) container_of(_dev_, struct typec_cable, dev)
60 #define to_typec_partner(_dev_) container_of(_dev_, struct typec_partner, dev)
62 static const struct device_type typec_partner_dev_type;
63 static const struct device_type typec_cable_dev_type;
64 static const struct device_type typec_plug_dev_type;
66 #define is_typec_partner(_dev_) (_dev_->type == &typec_partner_dev_type)
67 #define is_typec_cable(_dev_) (_dev_->type == &typec_cable_dev_type)
68 #define is_typec_plug(_dev_) (_dev_->type == &typec_plug_dev_type)
70 static DEFINE_IDA(typec_index_ida);
71 static struct class *typec_class;
73 /* ------------------------------------------------------------------------- */
74 /* Common attributes */
76 static const char * const typec_accessory_modes[] = {
77 [TYPEC_ACCESSORY_NONE] = "none",
78 [TYPEC_ACCESSORY_AUDIO] = "analog_audio",
79 [TYPEC_ACCESSORY_DEBUG] = "debug",
82 static struct usb_pd_identity *get_pd_identity(struct device *dev)
84 if (is_typec_partner(dev)) {
85 struct typec_partner *partner = to_typec_partner(dev);
87 return partner->identity;
88 } else if (is_typec_cable(dev)) {
89 struct typec_cable *cable = to_typec_cable(dev);
91 return cable->identity;
93 return NULL;
96 static ssize_t id_header_show(struct device *dev, struct device_attribute *attr,
97 char *buf)
99 struct usb_pd_identity *id = get_pd_identity(dev);
101 return sprintf(buf, "0x%08x\n", id->id_header);
103 static DEVICE_ATTR_RO(id_header);
105 static ssize_t cert_stat_show(struct device *dev, struct device_attribute *attr,
106 char *buf)
108 struct usb_pd_identity *id = get_pd_identity(dev);
110 return sprintf(buf, "0x%08x\n", id->cert_stat);
112 static DEVICE_ATTR_RO(cert_stat);
114 static ssize_t product_show(struct device *dev, struct device_attribute *attr,
115 char *buf)
117 struct usb_pd_identity *id = get_pd_identity(dev);
119 return sprintf(buf, "0x%08x\n", id->product);
121 static DEVICE_ATTR_RO(product);
123 static struct attribute *usb_pd_id_attrs[] = {
124 &dev_attr_id_header.attr,
125 &dev_attr_cert_stat.attr,
126 &dev_attr_product.attr,
127 NULL
130 static const struct attribute_group usb_pd_id_group = {
131 .name = "identity",
132 .attrs = usb_pd_id_attrs,
135 static const struct attribute_group *usb_pd_id_groups[] = {
136 &usb_pd_id_group,
137 NULL,
140 static void typec_report_identity(struct device *dev)
142 sysfs_notify(&dev->kobj, "identity", "id_header");
143 sysfs_notify(&dev->kobj, "identity", "cert_stat");
144 sysfs_notify(&dev->kobj, "identity", "product");
147 /* ------------------------------------------------------------------------- */
148 /* Alternate Modes */
150 static int altmode_match(struct device *dev, void *data)
152 struct typec_altmode *adev = to_typec_altmode(dev);
153 struct typec_device_id *id = data;
155 if (!is_typec_altmode(dev))
156 return 0;
158 return ((adev->svid == id->svid) && (adev->mode == id->mode));
161 static void typec_altmode_set_partner(struct altmode *altmode)
163 struct typec_altmode *adev = &altmode->adev;
164 struct typec_device_id id = { adev->svid, adev->mode, };
165 struct typec_port *port = typec_altmode2port(adev);
166 struct altmode *partner;
167 struct device *dev;
169 dev = device_find_child(&port->dev, &id, altmode_match);
170 if (!dev)
171 return;
173 /* Bind the port alt mode to the partner/plug alt mode. */
174 partner = to_altmode(to_typec_altmode(dev));
175 altmode->partner = partner;
177 /* Bind the partner/plug alt mode to the port alt mode. */
178 if (is_typec_plug(adev->dev.parent)) {
179 struct typec_plug *plug = to_typec_plug(adev->dev.parent);
181 partner->plug[plug->index] = altmode;
182 } else {
183 partner->partner = altmode;
187 static void typec_altmode_put_partner(struct altmode *altmode)
189 struct altmode *partner = altmode->partner;
190 struct typec_altmode *adev;
192 if (!partner)
193 return;
195 adev = &partner->adev;
197 if (is_typec_plug(adev->dev.parent)) {
198 struct typec_plug *plug = to_typec_plug(adev->dev.parent);
200 partner->plug[plug->index] = NULL;
201 } else {
202 partner->partner = NULL;
204 put_device(&adev->dev);
207 static int __typec_port_match(struct device *dev, const void *name)
209 return !strcmp((const char *)name, dev_name(dev));
212 static void *typec_port_match(struct device_connection *con, int ep, void *data)
214 return class_find_device(typec_class, NULL, con->endpoint[ep],
215 __typec_port_match);
218 struct typec_altmode *
219 typec_altmode_register_notifier(struct device *dev, u16 svid, u8 mode,
220 struct notifier_block *nb)
222 struct typec_device_id id = { svid, mode, };
223 struct device *altmode_dev;
224 struct device *port_dev;
225 struct altmode *altmode;
226 int ret;
228 /* Find the port linked to the caller */
229 port_dev = device_connection_find_match(dev, NULL, NULL,
230 typec_port_match);
231 if (IS_ERR_OR_NULL(port_dev))
232 return port_dev ? ERR_CAST(port_dev) : ERR_PTR(-ENODEV);
234 /* Find the altmode with matching svid */
235 altmode_dev = device_find_child(port_dev, &id, altmode_match);
237 put_device(port_dev);
239 if (!altmode_dev)
240 return ERR_PTR(-ENODEV);
242 altmode = to_altmode(to_typec_altmode(altmode_dev));
244 /* Register notifier */
245 ret = blocking_notifier_chain_register(&altmode->nh, nb);
246 if (ret) {
247 put_device(altmode_dev);
248 return ERR_PTR(ret);
251 return &altmode->adev;
253 EXPORT_SYMBOL_GPL(typec_altmode_register_notifier);
255 void typec_altmode_unregister_notifier(struct typec_altmode *adev,
256 struct notifier_block *nb)
258 struct altmode *altmode = to_altmode(adev);
260 blocking_notifier_chain_unregister(&altmode->nh, nb);
261 put_device(&adev->dev);
263 EXPORT_SYMBOL_GPL(typec_altmode_unregister_notifier);
266 * typec_altmode_update_active - Report Enter/Exit mode
267 * @adev: Handle to the alternate mode
268 * @active: True when the mode has been entered
270 * If a partner or cable plug executes Enter/Exit Mode command successfully, the
271 * drivers use this routine to report the updated state of the mode.
273 void typec_altmode_update_active(struct typec_altmode *adev, bool active)
275 char dir[6];
277 if (adev->active == active)
278 return;
280 if (!is_typec_port(adev->dev.parent)) {
281 if (!active)
282 module_put(adev->dev.driver->owner);
283 else
284 WARN_ON(!try_module_get(adev->dev.driver->owner));
287 adev->active = active;
288 snprintf(dir, sizeof(dir), "mode%d", adev->mode);
289 sysfs_notify(&adev->dev.kobj, dir, "active");
290 sysfs_notify(&adev->dev.kobj, NULL, "active");
291 kobject_uevent(&adev->dev.kobj, KOBJ_CHANGE);
293 EXPORT_SYMBOL_GPL(typec_altmode_update_active);
296 * typec_altmode2port - Alternate Mode to USB Type-C port
297 * @alt: The Alternate Mode
299 * Returns handle to the port that a cable plug or partner with @alt is
300 * connected to.
302 struct typec_port *typec_altmode2port(struct typec_altmode *alt)
304 if (is_typec_plug(alt->dev.parent))
305 return to_typec_port(alt->dev.parent->parent->parent);
306 if (is_typec_partner(alt->dev.parent))
307 return to_typec_port(alt->dev.parent->parent);
308 if (is_typec_port(alt->dev.parent))
309 return to_typec_port(alt->dev.parent);
311 return NULL;
313 EXPORT_SYMBOL_GPL(typec_altmode2port);
315 static ssize_t
316 vdo_show(struct device *dev, struct device_attribute *attr, char *buf)
318 struct typec_altmode *alt = to_typec_altmode(dev);
320 return sprintf(buf, "0x%08x\n", alt->vdo);
322 static DEVICE_ATTR_RO(vdo);
324 static ssize_t
325 description_show(struct device *dev, struct device_attribute *attr, char *buf)
327 struct typec_altmode *alt = to_typec_altmode(dev);
329 return sprintf(buf, "%s\n", alt->desc ? alt->desc : "");
331 static DEVICE_ATTR_RO(description);
333 static ssize_t
334 active_show(struct device *dev, struct device_attribute *attr, char *buf)
336 struct typec_altmode *alt = to_typec_altmode(dev);
338 return sprintf(buf, "%s\n", alt->active ? "yes" : "no");
341 static ssize_t active_store(struct device *dev, struct device_attribute *attr,
342 const char *buf, size_t size)
344 struct typec_altmode *adev = to_typec_altmode(dev);
345 struct altmode *altmode = to_altmode(adev);
346 bool enter;
347 int ret;
349 ret = kstrtobool(buf, &enter);
350 if (ret)
351 return ret;
353 if (adev->active == enter)
354 return size;
356 if (is_typec_port(adev->dev.parent)) {
357 typec_altmode_update_active(adev, enter);
359 /* Make sure that the partner exits the mode before disabling */
360 if (altmode->partner && !enter && altmode->partner->adev.active)
361 typec_altmode_exit(&altmode->partner->adev);
362 } else if (altmode->partner) {
363 if (enter && !altmode->partner->adev.active) {
364 dev_warn(dev, "port has the mode disabled\n");
365 return -EPERM;
369 /* Note: If there is no driver, the mode will not be entered */
370 if (adev->ops && adev->ops->activate) {
371 ret = adev->ops->activate(adev, enter);
372 if (ret)
373 return ret;
376 return size;
378 static DEVICE_ATTR_RW(active);
380 static ssize_t
381 supported_roles_show(struct device *dev, struct device_attribute *attr,
382 char *buf)
384 struct altmode *alt = to_altmode(to_typec_altmode(dev));
385 ssize_t ret;
387 switch (alt->roles) {
388 case TYPEC_PORT_SRC:
389 ret = sprintf(buf, "source\n");
390 break;
391 case TYPEC_PORT_SNK:
392 ret = sprintf(buf, "sink\n");
393 break;
394 case TYPEC_PORT_DRP:
395 default:
396 ret = sprintf(buf, "source sink\n");
397 break;
399 return ret;
401 static DEVICE_ATTR_RO(supported_roles);
403 static ssize_t
404 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
406 struct typec_altmode *adev = to_typec_altmode(dev);
408 return sprintf(buf, "%u\n", adev->mode);
410 static DEVICE_ATTR_RO(mode);
412 static ssize_t
413 svid_show(struct device *dev, struct device_attribute *attr, char *buf)
415 struct typec_altmode *adev = to_typec_altmode(dev);
417 return sprintf(buf, "%04x\n", adev->svid);
419 static DEVICE_ATTR_RO(svid);
421 static struct attribute *typec_altmode_attrs[] = {
422 &dev_attr_active.attr,
423 &dev_attr_mode.attr,
424 &dev_attr_svid.attr,
425 &dev_attr_vdo.attr,
426 NULL
428 ATTRIBUTE_GROUPS(typec_altmode);
430 static int altmode_id_get(struct device *dev)
432 struct ida *ids;
434 if (is_typec_partner(dev))
435 ids = &to_typec_partner(dev)->mode_ids;
436 else if (is_typec_plug(dev))
437 ids = &to_typec_plug(dev)->mode_ids;
438 else
439 ids = &to_typec_port(dev)->mode_ids;
441 return ida_simple_get(ids, 0, 0, GFP_KERNEL);
444 static void altmode_id_remove(struct device *dev, int id)
446 struct ida *ids;
448 if (is_typec_partner(dev))
449 ids = &to_typec_partner(dev)->mode_ids;
450 else if (is_typec_plug(dev))
451 ids = &to_typec_plug(dev)->mode_ids;
452 else
453 ids = &to_typec_port(dev)->mode_ids;
455 ida_simple_remove(ids, id);
458 static void typec_altmode_release(struct device *dev)
460 struct altmode *alt = to_altmode(to_typec_altmode(dev));
462 typec_altmode_put_partner(alt);
464 altmode_id_remove(alt->adev.dev.parent, alt->id);
465 kfree(alt);
468 const struct device_type typec_altmode_dev_type = {
469 .name = "typec_alternate_mode",
470 .groups = typec_altmode_groups,
471 .release = typec_altmode_release,
474 static struct typec_altmode *
475 typec_register_altmode(struct device *parent,
476 const struct typec_altmode_desc *desc)
478 unsigned int id = altmode_id_get(parent);
479 bool is_port = is_typec_port(parent);
480 struct altmode *alt;
481 int ret;
483 alt = kzalloc(sizeof(*alt), GFP_KERNEL);
484 if (!alt)
485 return ERR_PTR(-ENOMEM);
487 alt->adev.svid = desc->svid;
488 alt->adev.mode = desc->mode;
489 alt->adev.vdo = desc->vdo;
490 alt->roles = desc->roles;
491 alt->id = id;
493 alt->attrs[0] = &dev_attr_vdo.attr;
494 alt->attrs[1] = &dev_attr_description.attr;
495 alt->attrs[2] = &dev_attr_active.attr;
497 if (is_port) {
498 alt->attrs[3] = &dev_attr_supported_roles.attr;
499 alt->adev.active = true; /* Enabled by default */
502 sprintf(alt->group_name, "mode%d", desc->mode);
503 alt->group.name = alt->group_name;
504 alt->group.attrs = alt->attrs;
505 alt->groups[0] = &alt->group;
507 alt->adev.dev.parent = parent;
508 alt->adev.dev.groups = alt->groups;
509 alt->adev.dev.type = &typec_altmode_dev_type;
510 dev_set_name(&alt->adev.dev, "%s.%u", dev_name(parent), id);
512 /* Link partners and plugs with the ports */
513 if (is_port)
514 BLOCKING_INIT_NOTIFIER_HEAD(&alt->nh);
515 else
516 typec_altmode_set_partner(alt);
518 /* The partners are bind to drivers */
519 if (is_typec_partner(parent))
520 alt->adev.dev.bus = &typec_bus;
522 ret = device_register(&alt->adev.dev);
523 if (ret) {
524 dev_err(parent, "failed to register alternate mode (%d)\n",
525 ret);
526 put_device(&alt->adev.dev);
527 return ERR_PTR(ret);
530 return &alt->adev;
534 * typec_unregister_altmode - Unregister Alternate Mode
535 * @adev: The alternate mode to be unregistered
537 * Unregister device created with typec_partner_register_altmode(),
538 * typec_plug_register_altmode() or typec_port_register_altmode().
540 void typec_unregister_altmode(struct typec_altmode *adev)
542 if (IS_ERR_OR_NULL(adev))
543 return;
544 typec_mux_put(to_altmode(adev)->mux);
545 device_unregister(&adev->dev);
547 EXPORT_SYMBOL_GPL(typec_unregister_altmode);
549 /* ------------------------------------------------------------------------- */
550 /* Type-C Partners */
552 static ssize_t accessory_mode_show(struct device *dev,
553 struct device_attribute *attr,
554 char *buf)
556 struct typec_partner *p = to_typec_partner(dev);
558 return sprintf(buf, "%s\n", typec_accessory_modes[p->accessory]);
560 static DEVICE_ATTR_RO(accessory_mode);
562 static ssize_t supports_usb_power_delivery_show(struct device *dev,
563 struct device_attribute *attr,
564 char *buf)
566 struct typec_partner *p = to_typec_partner(dev);
568 return sprintf(buf, "%s\n", p->usb_pd ? "yes" : "no");
570 static DEVICE_ATTR_RO(supports_usb_power_delivery);
572 static struct attribute *typec_partner_attrs[] = {
573 &dev_attr_accessory_mode.attr,
574 &dev_attr_supports_usb_power_delivery.attr,
575 NULL
577 ATTRIBUTE_GROUPS(typec_partner);
579 static void typec_partner_release(struct device *dev)
581 struct typec_partner *partner = to_typec_partner(dev);
583 ida_destroy(&partner->mode_ids);
584 kfree(partner);
587 static const struct device_type typec_partner_dev_type = {
588 .name = "typec_partner",
589 .groups = typec_partner_groups,
590 .release = typec_partner_release,
594 * typec_partner_set_identity - Report result from Discover Identity command
595 * @partner: The partner updated identity values
597 * This routine is used to report that the result of Discover Identity USB power
598 * delivery command has become available.
600 int typec_partner_set_identity(struct typec_partner *partner)
602 if (!partner->identity)
603 return -EINVAL;
605 typec_report_identity(&partner->dev);
606 return 0;
608 EXPORT_SYMBOL_GPL(typec_partner_set_identity);
611 * typec_partner_register_altmode - Register USB Type-C Partner Alternate Mode
612 * @partner: USB Type-C Partner that supports the alternate mode
613 * @desc: Description of the alternate mode
615 * This routine is used to register each alternate mode individually that
616 * @partner has listed in response to Discover SVIDs command. The modes for a
617 * SVID listed in response to Discover Modes command need to be listed in an
618 * array in @desc.
620 * Returns handle to the alternate mode on success or NULL on failure.
622 struct typec_altmode *
623 typec_partner_register_altmode(struct typec_partner *partner,
624 const struct typec_altmode_desc *desc)
626 return typec_register_altmode(&partner->dev, desc);
628 EXPORT_SYMBOL_GPL(typec_partner_register_altmode);
631 * typec_register_partner - Register a USB Type-C Partner
632 * @port: The USB Type-C Port the partner is connected to
633 * @desc: Description of the partner
635 * Registers a device for USB Type-C Partner described in @desc.
637 * Returns handle to the partner on success or ERR_PTR on failure.
639 struct typec_partner *typec_register_partner(struct typec_port *port,
640 struct typec_partner_desc *desc)
642 struct typec_partner *partner;
643 int ret;
645 partner = kzalloc(sizeof(*partner), GFP_KERNEL);
646 if (!partner)
647 return ERR_PTR(-ENOMEM);
649 ida_init(&partner->mode_ids);
650 partner->usb_pd = desc->usb_pd;
651 partner->accessory = desc->accessory;
653 if (desc->identity) {
655 * Creating directory for the identity only if the driver is
656 * able to provide data to it.
658 partner->dev.groups = usb_pd_id_groups;
659 partner->identity = desc->identity;
662 partner->dev.class = typec_class;
663 partner->dev.parent = &port->dev;
664 partner->dev.type = &typec_partner_dev_type;
665 dev_set_name(&partner->dev, "%s-partner", dev_name(&port->dev));
667 ret = device_register(&partner->dev);
668 if (ret) {
669 dev_err(&port->dev, "failed to register partner (%d)\n", ret);
670 put_device(&partner->dev);
671 return ERR_PTR(ret);
674 return partner;
676 EXPORT_SYMBOL_GPL(typec_register_partner);
679 * typec_unregister_partner - Unregister a USB Type-C Partner
680 * @partner: The partner to be unregistered
682 * Unregister device created with typec_register_partner().
684 void typec_unregister_partner(struct typec_partner *partner)
686 if (!IS_ERR_OR_NULL(partner))
687 device_unregister(&partner->dev);
689 EXPORT_SYMBOL_GPL(typec_unregister_partner);
691 /* ------------------------------------------------------------------------- */
692 /* Type-C Cable Plugs */
694 static void typec_plug_release(struct device *dev)
696 struct typec_plug *plug = to_typec_plug(dev);
698 ida_destroy(&plug->mode_ids);
699 kfree(plug);
702 static const struct device_type typec_plug_dev_type = {
703 .name = "typec_plug",
704 .release = typec_plug_release,
708 * typec_plug_register_altmode - Register USB Type-C Cable Plug Alternate Mode
709 * @plug: USB Type-C Cable Plug that supports the alternate mode
710 * @desc: Description of the alternate mode
712 * This routine is used to register each alternate mode individually that @plug
713 * has listed in response to Discover SVIDs command. The modes for a SVID that
714 * the plug lists in response to Discover Modes command need to be listed in an
715 * array in @desc.
717 * Returns handle to the alternate mode on success or ERR_PTR on failure.
719 struct typec_altmode *
720 typec_plug_register_altmode(struct typec_plug *plug,
721 const struct typec_altmode_desc *desc)
723 return typec_register_altmode(&plug->dev, desc);
725 EXPORT_SYMBOL_GPL(typec_plug_register_altmode);
728 * typec_register_plug - Register a USB Type-C Cable Plug
729 * @cable: USB Type-C Cable with the plug
730 * @desc: Description of the cable plug
732 * Registers a device for USB Type-C Cable Plug described in @desc. A USB Type-C
733 * Cable Plug represents a plug with electronics in it that can response to USB
734 * Power Delivery SOP Prime or SOP Double Prime packages.
736 * Returns handle to the cable plug on success or ERR_PTR on failure.
738 struct typec_plug *typec_register_plug(struct typec_cable *cable,
739 struct typec_plug_desc *desc)
741 struct typec_plug *plug;
742 char name[8];
743 int ret;
745 plug = kzalloc(sizeof(*plug), GFP_KERNEL);
746 if (!plug)
747 return ERR_PTR(-ENOMEM);
749 sprintf(name, "plug%d", desc->index);
751 ida_init(&plug->mode_ids);
752 plug->index = desc->index;
753 plug->dev.class = typec_class;
754 plug->dev.parent = &cable->dev;
755 plug->dev.type = &typec_plug_dev_type;
756 dev_set_name(&plug->dev, "%s-%s", dev_name(cable->dev.parent), name);
758 ret = device_register(&plug->dev);
759 if (ret) {
760 dev_err(&cable->dev, "failed to register plug (%d)\n", ret);
761 put_device(&plug->dev);
762 return ERR_PTR(ret);
765 return plug;
767 EXPORT_SYMBOL_GPL(typec_register_plug);
770 * typec_unregister_plug - Unregister a USB Type-C Cable Plug
771 * @plug: The cable plug to be unregistered
773 * Unregister device created with typec_register_plug().
775 void typec_unregister_plug(struct typec_plug *plug)
777 if (!IS_ERR_OR_NULL(plug))
778 device_unregister(&plug->dev);
780 EXPORT_SYMBOL_GPL(typec_unregister_plug);
782 /* Type-C Cables */
784 static ssize_t
785 type_show(struct device *dev, struct device_attribute *attr, char *buf)
787 struct typec_cable *cable = to_typec_cable(dev);
789 return sprintf(buf, "%s\n", cable->active ? "active" : "passive");
791 static DEVICE_ATTR_RO(type);
793 static const char * const typec_plug_types[] = {
794 [USB_PLUG_NONE] = "unknown",
795 [USB_PLUG_TYPE_A] = "type-a",
796 [USB_PLUG_TYPE_B] = "type-b",
797 [USB_PLUG_TYPE_C] = "type-c",
798 [USB_PLUG_CAPTIVE] = "captive",
801 static ssize_t plug_type_show(struct device *dev,
802 struct device_attribute *attr, char *buf)
804 struct typec_cable *cable = to_typec_cable(dev);
806 return sprintf(buf, "%s\n", typec_plug_types[cable->type]);
808 static DEVICE_ATTR_RO(plug_type);
810 static struct attribute *typec_cable_attrs[] = {
811 &dev_attr_type.attr,
812 &dev_attr_plug_type.attr,
813 NULL
815 ATTRIBUTE_GROUPS(typec_cable);
817 static void typec_cable_release(struct device *dev)
819 struct typec_cable *cable = to_typec_cable(dev);
821 kfree(cable);
824 static const struct device_type typec_cable_dev_type = {
825 .name = "typec_cable",
826 .groups = typec_cable_groups,
827 .release = typec_cable_release,
831 * typec_cable_set_identity - Report result from Discover Identity command
832 * @cable: The cable updated identity values
834 * This routine is used to report that the result of Discover Identity USB power
835 * delivery command has become available.
837 int typec_cable_set_identity(struct typec_cable *cable)
839 if (!cable->identity)
840 return -EINVAL;
842 typec_report_identity(&cable->dev);
843 return 0;
845 EXPORT_SYMBOL_GPL(typec_cable_set_identity);
848 * typec_register_cable - Register a USB Type-C Cable
849 * @port: The USB Type-C Port the cable is connected to
850 * @desc: Description of the cable
852 * Registers a device for USB Type-C Cable described in @desc. The cable will be
853 * parent for the optional cable plug devises.
855 * Returns handle to the cable on success or ERR_PTR on failure.
857 struct typec_cable *typec_register_cable(struct typec_port *port,
858 struct typec_cable_desc *desc)
860 struct typec_cable *cable;
861 int ret;
863 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
864 if (!cable)
865 return ERR_PTR(-ENOMEM);
867 cable->type = desc->type;
868 cable->active = desc->active;
870 if (desc->identity) {
872 * Creating directory for the identity only if the driver is
873 * able to provide data to it.
875 cable->dev.groups = usb_pd_id_groups;
876 cable->identity = desc->identity;
879 cable->dev.class = typec_class;
880 cable->dev.parent = &port->dev;
881 cable->dev.type = &typec_cable_dev_type;
882 dev_set_name(&cable->dev, "%s-cable", dev_name(&port->dev));
884 ret = device_register(&cable->dev);
885 if (ret) {
886 dev_err(&port->dev, "failed to register cable (%d)\n", ret);
887 put_device(&cable->dev);
888 return ERR_PTR(ret);
891 return cable;
893 EXPORT_SYMBOL_GPL(typec_register_cable);
896 * typec_unregister_cable - Unregister a USB Type-C Cable
897 * @cable: The cable to be unregistered
899 * Unregister device created with typec_register_cable().
901 void typec_unregister_cable(struct typec_cable *cable)
903 if (!IS_ERR_OR_NULL(cable))
904 device_unregister(&cable->dev);
906 EXPORT_SYMBOL_GPL(typec_unregister_cable);
908 /* ------------------------------------------------------------------------- */
909 /* USB Type-C ports */
911 static const char * const typec_roles[] = {
912 [TYPEC_SINK] = "sink",
913 [TYPEC_SOURCE] = "source",
916 static const char * const typec_data_roles[] = {
917 [TYPEC_DEVICE] = "device",
918 [TYPEC_HOST] = "host",
921 static const char * const typec_port_power_roles[] = {
922 [TYPEC_PORT_SRC] = "source",
923 [TYPEC_PORT_SNK] = "sink",
924 [TYPEC_PORT_DRP] = "dual",
927 static const char * const typec_port_data_roles[] = {
928 [TYPEC_PORT_DFP] = "host",
929 [TYPEC_PORT_UFP] = "device",
930 [TYPEC_PORT_DRD] = "dual",
933 static const char * const typec_port_types_drp[] = {
934 [TYPEC_PORT_SRC] = "dual [source] sink",
935 [TYPEC_PORT_SNK] = "dual source [sink]",
936 [TYPEC_PORT_DRP] = "[dual] source sink",
939 static ssize_t
940 preferred_role_store(struct device *dev, struct device_attribute *attr,
941 const char *buf, size_t size)
943 struct typec_port *port = to_typec_port(dev);
944 int role;
945 int ret;
947 if (port->cap->type != TYPEC_PORT_DRP) {
948 dev_dbg(dev, "Preferred role only supported with DRP ports\n");
949 return -EOPNOTSUPP;
952 if (!port->cap->try_role) {
953 dev_dbg(dev, "Setting preferred role not supported\n");
954 return -EOPNOTSUPP;
957 role = sysfs_match_string(typec_roles, buf);
958 if (role < 0) {
959 if (sysfs_streq(buf, "none"))
960 role = TYPEC_NO_PREFERRED_ROLE;
961 else
962 return -EINVAL;
965 ret = port->cap->try_role(port->cap, role);
966 if (ret)
967 return ret;
969 port->prefer_role = role;
970 return size;
973 static ssize_t
974 preferred_role_show(struct device *dev, struct device_attribute *attr,
975 char *buf)
977 struct typec_port *port = to_typec_port(dev);
979 if (port->cap->type != TYPEC_PORT_DRP)
980 return 0;
982 if (port->prefer_role < 0)
983 return 0;
985 return sprintf(buf, "%s\n", typec_roles[port->prefer_role]);
987 static DEVICE_ATTR_RW(preferred_role);
989 static ssize_t data_role_store(struct device *dev,
990 struct device_attribute *attr,
991 const char *buf, size_t size)
993 struct typec_port *port = to_typec_port(dev);
994 int ret;
996 if (!port->cap->dr_set) {
997 dev_dbg(dev, "data role swapping not supported\n");
998 return -EOPNOTSUPP;
1001 ret = sysfs_match_string(typec_data_roles, buf);
1002 if (ret < 0)
1003 return ret;
1005 mutex_lock(&port->port_type_lock);
1006 if (port->cap->data != TYPEC_PORT_DRD) {
1007 ret = -EOPNOTSUPP;
1008 goto unlock_and_ret;
1011 ret = port->cap->dr_set(port->cap, ret);
1012 if (ret)
1013 goto unlock_and_ret;
1015 ret = size;
1016 unlock_and_ret:
1017 mutex_unlock(&port->port_type_lock);
1018 return ret;
1021 static ssize_t data_role_show(struct device *dev,
1022 struct device_attribute *attr, char *buf)
1024 struct typec_port *port = to_typec_port(dev);
1026 if (port->cap->data == TYPEC_PORT_DRD)
1027 return sprintf(buf, "%s\n", port->data_role == TYPEC_HOST ?
1028 "[host] device" : "host [device]");
1030 return sprintf(buf, "[%s]\n", typec_data_roles[port->data_role]);
1032 static DEVICE_ATTR_RW(data_role);
1034 static ssize_t power_role_store(struct device *dev,
1035 struct device_attribute *attr,
1036 const char *buf, size_t size)
1038 struct typec_port *port = to_typec_port(dev);
1039 int ret;
1041 if (!port->cap->pd_revision) {
1042 dev_dbg(dev, "USB Power Delivery not supported\n");
1043 return -EOPNOTSUPP;
1046 if (!port->cap->pr_set) {
1047 dev_dbg(dev, "power role swapping not supported\n");
1048 return -EOPNOTSUPP;
1051 if (port->pwr_opmode != TYPEC_PWR_MODE_PD) {
1052 dev_dbg(dev, "partner unable to swap power role\n");
1053 return -EIO;
1056 ret = sysfs_match_string(typec_roles, buf);
1057 if (ret < 0)
1058 return ret;
1060 mutex_lock(&port->port_type_lock);
1061 if (port->port_type != TYPEC_PORT_DRP) {
1062 dev_dbg(dev, "port type fixed at \"%s\"",
1063 typec_port_power_roles[port->port_type]);
1064 ret = -EOPNOTSUPP;
1065 goto unlock_and_ret;
1068 ret = port->cap->pr_set(port->cap, ret);
1069 if (ret)
1070 goto unlock_and_ret;
1072 ret = size;
1073 unlock_and_ret:
1074 mutex_unlock(&port->port_type_lock);
1075 return ret;
1078 static ssize_t power_role_show(struct device *dev,
1079 struct device_attribute *attr, char *buf)
1081 struct typec_port *port = to_typec_port(dev);
1083 if (port->cap->type == TYPEC_PORT_DRP)
1084 return sprintf(buf, "%s\n", port->pwr_role == TYPEC_SOURCE ?
1085 "[source] sink" : "source [sink]");
1087 return sprintf(buf, "[%s]\n", typec_roles[port->pwr_role]);
1089 static DEVICE_ATTR_RW(power_role);
1091 static ssize_t
1092 port_type_store(struct device *dev, struct device_attribute *attr,
1093 const char *buf, size_t size)
1095 struct typec_port *port = to_typec_port(dev);
1096 int ret;
1097 enum typec_port_type type;
1099 if (!port->cap->port_type_set || port->cap->type != TYPEC_PORT_DRP) {
1100 dev_dbg(dev, "changing port type not supported\n");
1101 return -EOPNOTSUPP;
1104 ret = sysfs_match_string(typec_port_power_roles, buf);
1105 if (ret < 0)
1106 return ret;
1108 type = ret;
1109 mutex_lock(&port->port_type_lock);
1111 if (port->port_type == type) {
1112 ret = size;
1113 goto unlock_and_ret;
1116 ret = port->cap->port_type_set(port->cap, type);
1117 if (ret)
1118 goto unlock_and_ret;
1120 port->port_type = type;
1121 ret = size;
1123 unlock_and_ret:
1124 mutex_unlock(&port->port_type_lock);
1125 return ret;
1128 static ssize_t
1129 port_type_show(struct device *dev, struct device_attribute *attr,
1130 char *buf)
1132 struct typec_port *port = to_typec_port(dev);
1134 if (port->cap->type == TYPEC_PORT_DRP)
1135 return sprintf(buf, "%s\n",
1136 typec_port_types_drp[port->port_type]);
1138 return sprintf(buf, "[%s]\n", typec_port_power_roles[port->cap->type]);
1140 static DEVICE_ATTR_RW(port_type);
1142 static const char * const typec_pwr_opmodes[] = {
1143 [TYPEC_PWR_MODE_USB] = "default",
1144 [TYPEC_PWR_MODE_1_5A] = "1.5A",
1145 [TYPEC_PWR_MODE_3_0A] = "3.0A",
1146 [TYPEC_PWR_MODE_PD] = "usb_power_delivery",
1149 static ssize_t power_operation_mode_show(struct device *dev,
1150 struct device_attribute *attr,
1151 char *buf)
1153 struct typec_port *port = to_typec_port(dev);
1155 return sprintf(buf, "%s\n", typec_pwr_opmodes[port->pwr_opmode]);
1157 static DEVICE_ATTR_RO(power_operation_mode);
1159 static ssize_t vconn_source_store(struct device *dev,
1160 struct device_attribute *attr,
1161 const char *buf, size_t size)
1163 struct typec_port *port = to_typec_port(dev);
1164 bool source;
1165 int ret;
1167 if (!port->cap->pd_revision) {
1168 dev_dbg(dev, "VCONN swap depends on USB Power Delivery\n");
1169 return -EOPNOTSUPP;
1172 if (!port->cap->vconn_set) {
1173 dev_dbg(dev, "VCONN swapping not supported\n");
1174 return -EOPNOTSUPP;
1177 ret = kstrtobool(buf, &source);
1178 if (ret)
1179 return ret;
1181 ret = port->cap->vconn_set(port->cap, (enum typec_role)source);
1182 if (ret)
1183 return ret;
1185 return size;
1188 static ssize_t vconn_source_show(struct device *dev,
1189 struct device_attribute *attr, char *buf)
1191 struct typec_port *port = to_typec_port(dev);
1193 return sprintf(buf, "%s\n",
1194 port->vconn_role == TYPEC_SOURCE ? "yes" : "no");
1196 static DEVICE_ATTR_RW(vconn_source);
1198 static ssize_t supported_accessory_modes_show(struct device *dev,
1199 struct device_attribute *attr,
1200 char *buf)
1202 struct typec_port *port = to_typec_port(dev);
1203 ssize_t ret = 0;
1204 int i;
1206 for (i = 0; i < ARRAY_SIZE(port->cap->accessory); i++) {
1207 if (port->cap->accessory[i])
1208 ret += sprintf(buf + ret, "%s ",
1209 typec_accessory_modes[port->cap->accessory[i]]);
1212 if (!ret)
1213 return sprintf(buf, "none\n");
1215 buf[ret - 1] = '\n';
1217 return ret;
1219 static DEVICE_ATTR_RO(supported_accessory_modes);
1221 static ssize_t usb_typec_revision_show(struct device *dev,
1222 struct device_attribute *attr,
1223 char *buf)
1225 struct typec_port *port = to_typec_port(dev);
1226 u16 rev = port->cap->revision;
1228 return sprintf(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
1230 static DEVICE_ATTR_RO(usb_typec_revision);
1232 static ssize_t usb_power_delivery_revision_show(struct device *dev,
1233 struct device_attribute *attr,
1234 char *buf)
1236 struct typec_port *p = to_typec_port(dev);
1238 return sprintf(buf, "%d\n", (p->cap->pd_revision >> 8) & 0xff);
1240 static DEVICE_ATTR_RO(usb_power_delivery_revision);
1242 static struct attribute *typec_attrs[] = {
1243 &dev_attr_data_role.attr,
1244 &dev_attr_power_operation_mode.attr,
1245 &dev_attr_power_role.attr,
1246 &dev_attr_preferred_role.attr,
1247 &dev_attr_supported_accessory_modes.attr,
1248 &dev_attr_usb_power_delivery_revision.attr,
1249 &dev_attr_usb_typec_revision.attr,
1250 &dev_attr_vconn_source.attr,
1251 &dev_attr_port_type.attr,
1252 NULL,
1254 ATTRIBUTE_GROUPS(typec);
1256 static int typec_uevent(struct device *dev, struct kobj_uevent_env *env)
1258 int ret;
1260 ret = add_uevent_var(env, "TYPEC_PORT=%s", dev_name(dev));
1261 if (ret)
1262 dev_err(dev, "failed to add uevent TYPEC_PORT\n");
1264 return ret;
1267 static void typec_release(struct device *dev)
1269 struct typec_port *port = to_typec_port(dev);
1271 ida_simple_remove(&typec_index_ida, port->id);
1272 ida_destroy(&port->mode_ids);
1273 typec_switch_put(port->sw);
1274 typec_mux_put(port->mux);
1275 kfree(port);
1278 const struct device_type typec_port_dev_type = {
1279 .name = "typec_port",
1280 .groups = typec_groups,
1281 .uevent = typec_uevent,
1282 .release = typec_release,
1285 /* --------------------------------------- */
1286 /* Driver callbacks to report role updates */
1289 * typec_set_data_role - Report data role change
1290 * @port: The USB Type-C Port where the role was changed
1291 * @role: The new data role
1293 * This routine is used by the port drivers to report data role changes.
1295 void typec_set_data_role(struct typec_port *port, enum typec_data_role role)
1297 if (port->data_role == role)
1298 return;
1300 port->data_role = role;
1301 sysfs_notify(&port->dev.kobj, NULL, "data_role");
1302 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1304 EXPORT_SYMBOL_GPL(typec_set_data_role);
1307 * typec_set_pwr_role - Report power role change
1308 * @port: The USB Type-C Port where the role was changed
1309 * @role: The new data role
1311 * This routine is used by the port drivers to report power role changes.
1313 void typec_set_pwr_role(struct typec_port *port, enum typec_role role)
1315 if (port->pwr_role == role)
1316 return;
1318 port->pwr_role = role;
1319 sysfs_notify(&port->dev.kobj, NULL, "power_role");
1320 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1322 EXPORT_SYMBOL_GPL(typec_set_pwr_role);
1325 * typec_set_pwr_role - Report VCONN source change
1326 * @port: The USB Type-C Port which VCONN role changed
1327 * @role: Source when @port is sourcing VCONN, or Sink when it's not
1329 * This routine is used by the port drivers to report if the VCONN source is
1330 * changes.
1332 void typec_set_vconn_role(struct typec_port *port, enum typec_role role)
1334 if (port->vconn_role == role)
1335 return;
1337 port->vconn_role = role;
1338 sysfs_notify(&port->dev.kobj, NULL, "vconn_source");
1339 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1341 EXPORT_SYMBOL_GPL(typec_set_vconn_role);
1343 static int partner_match(struct device *dev, void *data)
1345 return is_typec_partner(dev);
1349 * typec_set_pwr_opmode - Report changed power operation mode
1350 * @port: The USB Type-C Port where the mode was changed
1351 * @opmode: New power operation mode
1353 * This routine is used by the port drivers to report changed power operation
1354 * mode in @port. The modes are USB (default), 1.5A, 3.0A as defined in USB
1355 * Type-C specification, and "USB Power Delivery" when the power levels are
1356 * negotiated with methods defined in USB Power Delivery specification.
1358 void typec_set_pwr_opmode(struct typec_port *port,
1359 enum typec_pwr_opmode opmode)
1361 struct device *partner_dev;
1363 if (port->pwr_opmode == opmode)
1364 return;
1366 port->pwr_opmode = opmode;
1367 sysfs_notify(&port->dev.kobj, NULL, "power_operation_mode");
1368 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1370 partner_dev = device_find_child(&port->dev, NULL, partner_match);
1371 if (partner_dev) {
1372 struct typec_partner *partner = to_typec_partner(partner_dev);
1374 if (opmode == TYPEC_PWR_MODE_PD && !partner->usb_pd) {
1375 partner->usb_pd = 1;
1376 sysfs_notify(&partner_dev->kobj, NULL,
1377 "supports_usb_power_delivery");
1379 put_device(partner_dev);
1382 EXPORT_SYMBOL_GPL(typec_set_pwr_opmode);
1385 * typec_find_port_power_role - Get the typec port power capability
1386 * @name: port power capability string
1388 * This routine is used to find the typec_port_type by its string name.
1390 * Returns typec_port_type if success, otherwise negative error code.
1392 int typec_find_port_power_role(const char *name)
1394 return match_string(typec_port_power_roles,
1395 ARRAY_SIZE(typec_port_power_roles), name);
1397 EXPORT_SYMBOL_GPL(typec_find_port_power_role);
1400 * typec_find_power_role - Find the typec one specific power role
1401 * @name: power role string
1403 * This routine is used to find the typec_role by its string name.
1405 * Returns typec_role if success, otherwise negative error code.
1407 int typec_find_power_role(const char *name)
1409 return match_string(typec_roles, ARRAY_SIZE(typec_roles), name);
1411 EXPORT_SYMBOL_GPL(typec_find_power_role);
1414 * typec_find_port_data_role - Get the typec port data capability
1415 * @name: port data capability string
1417 * This routine is used to find the typec_port_data by its string name.
1419 * Returns typec_port_data if success, otherwise negative error code.
1421 int typec_find_port_data_role(const char *name)
1423 return match_string(typec_port_data_roles,
1424 ARRAY_SIZE(typec_port_data_roles), name);
1426 EXPORT_SYMBOL_GPL(typec_find_port_data_role);
1428 /* ------------------------------------------ */
1429 /* API for Multiplexer/DeMultiplexer Switches */
1432 * typec_set_orientation - Set USB Type-C cable plug orientation
1433 * @port: USB Type-C Port
1434 * @orientation: USB Type-C cable plug orientation
1436 * Set cable plug orientation for @port.
1438 int typec_set_orientation(struct typec_port *port,
1439 enum typec_orientation orientation)
1441 int ret;
1443 if (port->sw) {
1444 ret = port->sw->set(port->sw, orientation);
1445 if (ret)
1446 return ret;
1449 port->orientation = orientation;
1451 return 0;
1453 EXPORT_SYMBOL_GPL(typec_set_orientation);
1456 * typec_get_orientation - Get USB Type-C cable plug orientation
1457 * @port: USB Type-C Port
1459 * Get current cable plug orientation for @port.
1461 enum typec_orientation typec_get_orientation(struct typec_port *port)
1463 return port->orientation;
1465 EXPORT_SYMBOL_GPL(typec_get_orientation);
1468 * typec_set_mode - Set mode of operation for USB Type-C connector
1469 * @port: USB Type-C connector
1470 * @mode: Accessory Mode, USB Operation or Safe State
1472 * Configure @port for Accessory Mode @mode. This function will configure the
1473 * muxes needed for @mode.
1475 int typec_set_mode(struct typec_port *port, int mode)
1477 return port->mux ? port->mux->set(port->mux, mode) : 0;
1479 EXPORT_SYMBOL_GPL(typec_set_mode);
1481 /* --------------------------------------- */
1484 * typec_port_register_altmode - Register USB Type-C Port Alternate Mode
1485 * @port: USB Type-C Port that supports the alternate mode
1486 * @desc: Description of the alternate mode
1488 * This routine is used to register an alternate mode that @port is capable of
1489 * supporting.
1491 * Returns handle to the alternate mode on success or ERR_PTR on failure.
1493 struct typec_altmode *
1494 typec_port_register_altmode(struct typec_port *port,
1495 const struct typec_altmode_desc *desc)
1497 struct typec_altmode *adev;
1498 struct typec_mux *mux;
1499 char id[10];
1501 sprintf(id, "id%04xm%02x", desc->svid, desc->mode);
1503 mux = typec_mux_get(&port->dev, id);
1504 if (IS_ERR(mux))
1505 return ERR_CAST(mux);
1507 adev = typec_register_altmode(&port->dev, desc);
1508 if (IS_ERR(adev))
1509 typec_mux_put(mux);
1510 else
1511 to_altmode(adev)->mux = mux;
1513 return adev;
1515 EXPORT_SYMBOL_GPL(typec_port_register_altmode);
1518 * typec_register_port - Register a USB Type-C Port
1519 * @parent: Parent device
1520 * @cap: Description of the port
1522 * Registers a device for USB Type-C Port described in @cap.
1524 * Returns handle to the port on success or ERR_PTR on failure.
1526 struct typec_port *typec_register_port(struct device *parent,
1527 const struct typec_capability *cap)
1529 struct typec_port *port;
1530 int ret;
1531 int id;
1533 port = kzalloc(sizeof(*port), GFP_KERNEL);
1534 if (!port)
1535 return ERR_PTR(-ENOMEM);
1537 id = ida_simple_get(&typec_index_ida, 0, 0, GFP_KERNEL);
1538 if (id < 0) {
1539 kfree(port);
1540 return ERR_PTR(id);
1543 switch (cap->type) {
1544 case TYPEC_PORT_SRC:
1545 port->pwr_role = TYPEC_SOURCE;
1546 port->vconn_role = TYPEC_SOURCE;
1547 break;
1548 case TYPEC_PORT_SNK:
1549 port->pwr_role = TYPEC_SINK;
1550 port->vconn_role = TYPEC_SINK;
1551 break;
1552 case TYPEC_PORT_DRP:
1553 if (cap->prefer_role != TYPEC_NO_PREFERRED_ROLE)
1554 port->pwr_role = cap->prefer_role;
1555 else
1556 port->pwr_role = TYPEC_SINK;
1557 break;
1560 switch (cap->data) {
1561 case TYPEC_PORT_DFP:
1562 port->data_role = TYPEC_HOST;
1563 break;
1564 case TYPEC_PORT_UFP:
1565 port->data_role = TYPEC_DEVICE;
1566 break;
1567 case TYPEC_PORT_DRD:
1568 if (cap->prefer_role == TYPEC_SOURCE)
1569 port->data_role = TYPEC_HOST;
1570 else
1571 port->data_role = TYPEC_DEVICE;
1572 break;
1575 ida_init(&port->mode_ids);
1576 mutex_init(&port->port_type_lock);
1578 port->id = id;
1579 port->cap = cap;
1580 port->port_type = cap->type;
1581 port->prefer_role = cap->prefer_role;
1583 device_initialize(&port->dev);
1584 port->dev.class = typec_class;
1585 port->dev.parent = parent;
1586 port->dev.fwnode = cap->fwnode;
1587 port->dev.type = &typec_port_dev_type;
1588 dev_set_name(&port->dev, "port%d", id);
1590 port->sw = typec_switch_get(&port->dev);
1591 if (IS_ERR(port->sw)) {
1592 ret = PTR_ERR(port->sw);
1593 put_device(&port->dev);
1594 return ERR_PTR(ret);
1597 port->mux = typec_mux_get(&port->dev, "typec-mux");
1598 if (IS_ERR(port->mux)) {
1599 ret = PTR_ERR(port->mux);
1600 put_device(&port->dev);
1601 return ERR_PTR(ret);
1604 ret = device_add(&port->dev);
1605 if (ret) {
1606 dev_err(parent, "failed to register port (%d)\n", ret);
1607 put_device(&port->dev);
1608 return ERR_PTR(ret);
1611 return port;
1613 EXPORT_SYMBOL_GPL(typec_register_port);
1616 * typec_unregister_port - Unregister a USB Type-C Port
1617 * @port: The port to be unregistered
1619 * Unregister device created with typec_register_port().
1621 void typec_unregister_port(struct typec_port *port)
1623 if (!IS_ERR_OR_NULL(port))
1624 device_unregister(&port->dev);
1626 EXPORT_SYMBOL_GPL(typec_unregister_port);
1628 static int __init typec_init(void)
1630 int ret;
1632 ret = bus_register(&typec_bus);
1633 if (ret)
1634 return ret;
1636 typec_class = class_create(THIS_MODULE, "typec");
1637 if (IS_ERR(typec_class)) {
1638 bus_unregister(&typec_bus);
1639 return PTR_ERR(typec_class);
1642 return 0;
1644 subsys_initcall(typec_init);
1646 static void __exit typec_exit(void)
1648 class_destroy(typec_class);
1649 ida_destroy(&typec_index_ida);
1650 bus_unregister(&typec_bus);
1652 module_exit(typec_exit);
1654 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1655 MODULE_LICENSE("GPL v2");
1656 MODULE_DESCRIPTION("USB Type-C Connector Class");