1 // SPDX-License-Identifier: GPL-2.0
3 * USB Type-C Connector Class
5 * Copyright (C) 2017, Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
9 #include <linux/device.h>
10 #include <linux/module.h>
11 #include <linux/mutex.h>
12 #include <linux/property.h>
13 #include <linux/slab.h>
14 #include <linux/usb/pd_vdo.h>
20 enum typec_plug_index index
;
27 enum typec_plug_type type
;
28 struct usb_pd_identity
*identity
;
29 unsigned int active
:1;
32 struct typec_partner
{
34 unsigned int usb_pd
:1;
35 struct usb_pd_identity
*identity
;
36 enum typec_accessory accessory
;
47 enum typec_data_role data_role
;
48 enum typec_role pwr_role
;
49 enum typec_role vconn_role
;
50 enum typec_pwr_opmode pwr_opmode
;
51 enum typec_port_type port_type
;
52 struct mutex port_type_lock
;
54 enum typec_orientation orientation
;
55 struct typec_switch
*sw
;
56 struct typec_mux
*mux
;
58 const struct typec_capability
*cap
;
59 const struct typec_operations
*ops
;
62 #define to_typec_port(_dev_) container_of(_dev_, struct typec_port, dev)
63 #define to_typec_plug(_dev_) container_of(_dev_, struct typec_plug, dev)
64 #define to_typec_cable(_dev_) container_of(_dev_, struct typec_cable, dev)
65 #define to_typec_partner(_dev_) container_of(_dev_, struct typec_partner, dev)
67 static const struct device_type typec_partner_dev_type
;
68 static const struct device_type typec_cable_dev_type
;
69 static const struct device_type typec_plug_dev_type
;
71 #define is_typec_partner(_dev_) (_dev_->type == &typec_partner_dev_type)
72 #define is_typec_cable(_dev_) (_dev_->type == &typec_cable_dev_type)
73 #define is_typec_plug(_dev_) (_dev_->type == &typec_plug_dev_type)
75 static DEFINE_IDA(typec_index_ida
);
76 static struct class *typec_class
;
78 /* ------------------------------------------------------------------------- */
79 /* Common attributes */
81 static const char * const typec_accessory_modes
[] = {
82 [TYPEC_ACCESSORY_NONE
] = "none",
83 [TYPEC_ACCESSORY_AUDIO
] = "analog_audio",
84 [TYPEC_ACCESSORY_DEBUG
] = "debug",
87 /* Product types defined in USB PD Specification R3.0 V2.0 */
88 static const char * const product_type_ufp
[8] = {
89 [IDH_PTYPE_UNDEF
] = "undefined",
90 [IDH_PTYPE_HUB
] = "hub",
91 [IDH_PTYPE_PERIPH
] = "peripheral",
92 [IDH_PTYPE_PSD
] = "psd",
93 [IDH_PTYPE_AMA
] = "ama",
96 static const char * const product_type_dfp
[8] = {
97 [IDH_PTYPE_DFP_UNDEF
] = "undefined",
98 [IDH_PTYPE_DFP_HUB
] = "hub",
99 [IDH_PTYPE_DFP_HOST
] = "host",
100 [IDH_PTYPE_DFP_PB
] = "power_brick",
101 [IDH_PTYPE_DFP_AMC
] = "amc",
104 static const char * const product_type_cable
[8] = {
105 [IDH_PTYPE_UNDEF
] = "undefined",
106 [IDH_PTYPE_PCABLE
] = "passive",
107 [IDH_PTYPE_ACABLE
] = "active",
110 static struct usb_pd_identity
*get_pd_identity(struct device
*dev
)
112 if (is_typec_partner(dev
)) {
113 struct typec_partner
*partner
= to_typec_partner(dev
);
115 return partner
->identity
;
116 } else if (is_typec_cable(dev
)) {
117 struct typec_cable
*cable
= to_typec_cable(dev
);
119 return cable
->identity
;
124 static const char *get_pd_product_type(struct device
*dev
)
126 struct typec_port
*port
= to_typec_port(dev
->parent
);
127 struct usb_pd_identity
*id
= get_pd_identity(dev
);
128 const char *ptype
= NULL
;
130 if (is_typec_partner(dev
)) {
134 if (port
->data_role
== TYPEC_HOST
)
135 ptype
= product_type_ufp
[PD_IDH_PTYPE(id
->id_header
)];
137 ptype
= product_type_dfp
[PD_IDH_DFP_PTYPE(id
->id_header
)];
138 } else if (is_typec_cable(dev
)) {
140 ptype
= product_type_cable
[PD_IDH_PTYPE(id
->id_header
)];
142 ptype
= to_typec_cable(dev
)->active
?
143 product_type_cable
[IDH_PTYPE_ACABLE
] :
144 product_type_cable
[IDH_PTYPE_PCABLE
];
150 static ssize_t
id_header_show(struct device
*dev
, struct device_attribute
*attr
,
153 struct usb_pd_identity
*id
= get_pd_identity(dev
);
155 return sprintf(buf
, "0x%08x\n", id
->id_header
);
157 static DEVICE_ATTR_RO(id_header
);
159 static ssize_t
cert_stat_show(struct device
*dev
, struct device_attribute
*attr
,
162 struct usb_pd_identity
*id
= get_pd_identity(dev
);
164 return sprintf(buf
, "0x%08x\n", id
->cert_stat
);
166 static DEVICE_ATTR_RO(cert_stat
);
168 static ssize_t
product_show(struct device
*dev
, struct device_attribute
*attr
,
171 struct usb_pd_identity
*id
= get_pd_identity(dev
);
173 return sprintf(buf
, "0x%08x\n", id
->product
);
175 static DEVICE_ATTR_RO(product
);
177 static ssize_t
product_type_vdo1_show(struct device
*dev
, struct device_attribute
*attr
,
180 struct usb_pd_identity
*id
= get_pd_identity(dev
);
182 return sysfs_emit(buf
, "0x%08x\n", id
->vdo
[0]);
184 static DEVICE_ATTR_RO(product_type_vdo1
);
186 static ssize_t
product_type_vdo2_show(struct device
*dev
, struct device_attribute
*attr
,
189 struct usb_pd_identity
*id
= get_pd_identity(dev
);
191 return sysfs_emit(buf
, "0x%08x\n", id
->vdo
[1]);
193 static DEVICE_ATTR_RO(product_type_vdo2
);
195 static ssize_t
product_type_vdo3_show(struct device
*dev
, struct device_attribute
*attr
,
198 struct usb_pd_identity
*id
= get_pd_identity(dev
);
200 return sysfs_emit(buf
, "0x%08x\n", id
->vdo
[2]);
202 static DEVICE_ATTR_RO(product_type_vdo3
);
204 static struct attribute
*usb_pd_id_attrs
[] = {
205 &dev_attr_id_header
.attr
,
206 &dev_attr_cert_stat
.attr
,
207 &dev_attr_product
.attr
,
208 &dev_attr_product_type_vdo1
.attr
,
209 &dev_attr_product_type_vdo2
.attr
,
210 &dev_attr_product_type_vdo3
.attr
,
214 static const struct attribute_group usb_pd_id_group
= {
216 .attrs
= usb_pd_id_attrs
,
219 static const struct attribute_group
*usb_pd_id_groups
[] = {
224 static void typec_product_type_notify(struct device
*dev
)
229 ptype
= get_pd_product_type(dev
);
233 sysfs_notify(&dev
->kobj
, NULL
, "type");
235 envp
[0] = kasprintf(GFP_KERNEL
, "PRODUCT_TYPE=%s", ptype
);
239 kobject_uevent_env(&dev
->kobj
, KOBJ_CHANGE
, envp
);
243 static void typec_report_identity(struct device
*dev
)
245 sysfs_notify(&dev
->kobj
, "identity", "id_header");
246 sysfs_notify(&dev
->kobj
, "identity", "cert_stat");
247 sysfs_notify(&dev
->kobj
, "identity", "product");
248 sysfs_notify(&dev
->kobj
, "identity", "product_type_vdo1");
249 sysfs_notify(&dev
->kobj
, "identity", "product_type_vdo2");
250 sysfs_notify(&dev
->kobj
, "identity", "product_type_vdo3");
251 typec_product_type_notify(dev
);
255 type_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
259 ptype
= get_pd_product_type(dev
);
263 return sysfs_emit(buf
, "%s\n", ptype
);
265 static DEVICE_ATTR_RO(type
);
267 /* ------------------------------------------------------------------------- */
268 /* Alternate Modes */
270 static int altmode_match(struct device
*dev
, void *data
)
272 struct typec_altmode
*adev
= to_typec_altmode(dev
);
273 struct typec_device_id
*id
= data
;
275 if (!is_typec_altmode(dev
))
278 return ((adev
->svid
== id
->svid
) && (adev
->mode
== id
->mode
));
281 static void typec_altmode_set_partner(struct altmode
*altmode
)
283 struct typec_altmode
*adev
= &altmode
->adev
;
284 struct typec_device_id id
= { adev
->svid
, adev
->mode
, };
285 struct typec_port
*port
= typec_altmode2port(adev
);
286 struct altmode
*partner
;
289 dev
= device_find_child(&port
->dev
, &id
, altmode_match
);
293 /* Bind the port alt mode to the partner/plug alt mode. */
294 partner
= to_altmode(to_typec_altmode(dev
));
295 altmode
->partner
= partner
;
297 /* Bind the partner/plug alt mode to the port alt mode. */
298 if (is_typec_plug(adev
->dev
.parent
)) {
299 struct typec_plug
*plug
= to_typec_plug(adev
->dev
.parent
);
301 partner
->plug
[plug
->index
] = altmode
;
303 partner
->partner
= altmode
;
307 static void typec_altmode_put_partner(struct altmode
*altmode
)
309 struct altmode
*partner
= altmode
->partner
;
310 struct typec_altmode
*adev
;
315 adev
= &partner
->adev
;
317 if (is_typec_plug(adev
->dev
.parent
)) {
318 struct typec_plug
*plug
= to_typec_plug(adev
->dev
.parent
);
320 partner
->plug
[plug
->index
] = NULL
;
322 partner
->partner
= NULL
;
324 put_device(&adev
->dev
);
328 * typec_altmode_update_active - Report Enter/Exit mode
329 * @adev: Handle to the alternate mode
330 * @active: True when the mode has been entered
332 * If a partner or cable plug executes Enter/Exit Mode command successfully, the
333 * drivers use this routine to report the updated state of the mode.
335 void typec_altmode_update_active(struct typec_altmode
*adev
, bool active
)
339 if (adev
->active
== active
)
342 if (!is_typec_port(adev
->dev
.parent
) && adev
->dev
.driver
) {
344 module_put(adev
->dev
.driver
->owner
);
346 WARN_ON(!try_module_get(adev
->dev
.driver
->owner
));
349 adev
->active
= active
;
350 snprintf(dir
, sizeof(dir
), "mode%d", adev
->mode
);
351 sysfs_notify(&adev
->dev
.kobj
, dir
, "active");
352 sysfs_notify(&adev
->dev
.kobj
, NULL
, "active");
353 kobject_uevent(&adev
->dev
.kobj
, KOBJ_CHANGE
);
355 EXPORT_SYMBOL_GPL(typec_altmode_update_active
);
358 * typec_altmode2port - Alternate Mode to USB Type-C port
359 * @alt: The Alternate Mode
361 * Returns handle to the port that a cable plug or partner with @alt is
364 struct typec_port
*typec_altmode2port(struct typec_altmode
*alt
)
366 if (is_typec_plug(alt
->dev
.parent
))
367 return to_typec_port(alt
->dev
.parent
->parent
->parent
);
368 if (is_typec_partner(alt
->dev
.parent
))
369 return to_typec_port(alt
->dev
.parent
->parent
);
370 if (is_typec_port(alt
->dev
.parent
))
371 return to_typec_port(alt
->dev
.parent
);
375 EXPORT_SYMBOL_GPL(typec_altmode2port
);
378 vdo_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
380 struct typec_altmode
*alt
= to_typec_altmode(dev
);
382 return sprintf(buf
, "0x%08x\n", alt
->vdo
);
384 static DEVICE_ATTR_RO(vdo
);
387 description_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
389 struct typec_altmode
*alt
= to_typec_altmode(dev
);
391 return sprintf(buf
, "%s\n", alt
->desc
? alt
->desc
: "");
393 static DEVICE_ATTR_RO(description
);
396 active_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
398 struct typec_altmode
*alt
= to_typec_altmode(dev
);
400 return sprintf(buf
, "%s\n", alt
->active
? "yes" : "no");
403 static ssize_t
active_store(struct device
*dev
, struct device_attribute
*attr
,
404 const char *buf
, size_t size
)
406 struct typec_altmode
*adev
= to_typec_altmode(dev
);
407 struct altmode
*altmode
= to_altmode(adev
);
411 ret
= kstrtobool(buf
, &enter
);
415 if (adev
->active
== enter
)
418 if (is_typec_port(adev
->dev
.parent
)) {
419 typec_altmode_update_active(adev
, enter
);
421 /* Make sure that the partner exits the mode before disabling */
422 if (altmode
->partner
&& !enter
&& altmode
->partner
->adev
.active
)
423 typec_altmode_exit(&altmode
->partner
->adev
);
424 } else if (altmode
->partner
) {
425 if (enter
&& !altmode
->partner
->adev
.active
) {
426 dev_warn(dev
, "port has the mode disabled\n");
431 /* Note: If there is no driver, the mode will not be entered */
432 if (adev
->ops
&& adev
->ops
->activate
) {
433 ret
= adev
->ops
->activate(adev
, enter
);
440 static DEVICE_ATTR_RW(active
);
443 supported_roles_show(struct device
*dev
, struct device_attribute
*attr
,
446 struct altmode
*alt
= to_altmode(to_typec_altmode(dev
));
449 switch (alt
->roles
) {
451 ret
= sprintf(buf
, "source\n");
454 ret
= sprintf(buf
, "sink\n");
458 ret
= sprintf(buf
, "source sink\n");
463 static DEVICE_ATTR_RO(supported_roles
);
466 mode_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
468 struct typec_altmode
*adev
= to_typec_altmode(dev
);
470 return sprintf(buf
, "%u\n", adev
->mode
);
472 static DEVICE_ATTR_RO(mode
);
475 svid_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
477 struct typec_altmode
*adev
= to_typec_altmode(dev
);
479 return sprintf(buf
, "%04x\n", adev
->svid
);
481 static DEVICE_ATTR_RO(svid
);
483 static struct attribute
*typec_altmode_attrs
[] = {
484 &dev_attr_active
.attr
,
491 static umode_t
typec_altmode_attr_is_visible(struct kobject
*kobj
,
492 struct attribute
*attr
, int n
)
494 struct typec_altmode
*adev
= to_typec_altmode(kobj_to_dev(kobj
));
496 if (attr
== &dev_attr_active
.attr
)
497 if (!adev
->ops
|| !adev
->ops
->activate
)
503 static const struct attribute_group typec_altmode_group
= {
504 .is_visible
= typec_altmode_attr_is_visible
,
505 .attrs
= typec_altmode_attrs
,
508 static const struct attribute_group
*typec_altmode_groups
[] = {
509 &typec_altmode_group
,
513 static int altmode_id_get(struct device
*dev
)
517 if (is_typec_partner(dev
))
518 ids
= &to_typec_partner(dev
)->mode_ids
;
519 else if (is_typec_plug(dev
))
520 ids
= &to_typec_plug(dev
)->mode_ids
;
522 ids
= &to_typec_port(dev
)->mode_ids
;
524 return ida_simple_get(ids
, 0, 0, GFP_KERNEL
);
527 static void altmode_id_remove(struct device
*dev
, int id
)
531 if (is_typec_partner(dev
))
532 ids
= &to_typec_partner(dev
)->mode_ids
;
533 else if (is_typec_plug(dev
))
534 ids
= &to_typec_plug(dev
)->mode_ids
;
536 ids
= &to_typec_port(dev
)->mode_ids
;
538 ida_simple_remove(ids
, id
);
541 static void typec_altmode_release(struct device
*dev
)
543 struct altmode
*alt
= to_altmode(to_typec_altmode(dev
));
545 typec_altmode_put_partner(alt
);
547 altmode_id_remove(alt
->adev
.dev
.parent
, alt
->id
);
551 const struct device_type typec_altmode_dev_type
= {
552 .name
= "typec_alternate_mode",
553 .groups
= typec_altmode_groups
,
554 .release
= typec_altmode_release
,
557 static struct typec_altmode
*
558 typec_register_altmode(struct device
*parent
,
559 const struct typec_altmode_desc
*desc
)
561 unsigned int id
= altmode_id_get(parent
);
562 bool is_port
= is_typec_port(parent
);
566 alt
= kzalloc(sizeof(*alt
), GFP_KERNEL
);
568 return ERR_PTR(-ENOMEM
);
570 alt
->adev
.svid
= desc
->svid
;
571 alt
->adev
.mode
= desc
->mode
;
572 alt
->adev
.vdo
= desc
->vdo
;
573 alt
->roles
= desc
->roles
;
576 alt
->attrs
[0] = &dev_attr_vdo
.attr
;
577 alt
->attrs
[1] = &dev_attr_description
.attr
;
578 alt
->attrs
[2] = &dev_attr_active
.attr
;
581 alt
->attrs
[3] = &dev_attr_supported_roles
.attr
;
582 alt
->adev
.active
= true; /* Enabled by default */
585 sprintf(alt
->group_name
, "mode%d", desc
->mode
);
586 alt
->group
.name
= alt
->group_name
;
587 alt
->group
.attrs
= alt
->attrs
;
588 alt
->groups
[0] = &alt
->group
;
590 alt
->adev
.dev
.parent
= parent
;
591 alt
->adev
.dev
.groups
= alt
->groups
;
592 alt
->adev
.dev
.type
= &typec_altmode_dev_type
;
593 dev_set_name(&alt
->adev
.dev
, "%s.%u", dev_name(parent
), id
);
595 /* Link partners and plugs with the ports */
597 typec_altmode_set_partner(alt
);
599 /* The partners are bind to drivers */
600 if (is_typec_partner(parent
))
601 alt
->adev
.dev
.bus
= &typec_bus
;
603 /* Plug alt modes need a class to generate udev events. */
604 if (is_typec_plug(parent
))
605 alt
->adev
.dev
.class = typec_class
;
607 ret
= device_register(&alt
->adev
.dev
);
609 dev_err(parent
, "failed to register alternate mode (%d)\n",
611 put_device(&alt
->adev
.dev
);
619 * typec_unregister_altmode - Unregister Alternate Mode
620 * @adev: The alternate mode to be unregistered
622 * Unregister device created with typec_partner_register_altmode(),
623 * typec_plug_register_altmode() or typec_port_register_altmode().
625 void typec_unregister_altmode(struct typec_altmode
*adev
)
627 if (IS_ERR_OR_NULL(adev
))
629 typec_mux_put(to_altmode(adev
)->mux
);
630 device_unregister(&adev
->dev
);
632 EXPORT_SYMBOL_GPL(typec_unregister_altmode
);
634 /* ------------------------------------------------------------------------- */
635 /* Type-C Partners */
637 static ssize_t
accessory_mode_show(struct device
*dev
,
638 struct device_attribute
*attr
,
641 struct typec_partner
*p
= to_typec_partner(dev
);
643 return sprintf(buf
, "%s\n", typec_accessory_modes
[p
->accessory
]);
645 static DEVICE_ATTR_RO(accessory_mode
);
647 static ssize_t
supports_usb_power_delivery_show(struct device
*dev
,
648 struct device_attribute
*attr
,
651 struct typec_partner
*p
= to_typec_partner(dev
);
653 return sprintf(buf
, "%s\n", p
->usb_pd
? "yes" : "no");
655 static DEVICE_ATTR_RO(supports_usb_power_delivery
);
657 static ssize_t
number_of_alternate_modes_show(struct device
*dev
, struct device_attribute
*attr
,
660 struct typec_partner
*partner
;
661 struct typec_plug
*plug
;
664 if (is_typec_partner(dev
)) {
665 partner
= to_typec_partner(dev
);
666 num_altmodes
= partner
->num_altmodes
;
667 } else if (is_typec_plug(dev
)) {
668 plug
= to_typec_plug(dev
);
669 num_altmodes
= plug
->num_altmodes
;
674 return sysfs_emit(buf
, "%d\n", num_altmodes
);
676 static DEVICE_ATTR_RO(number_of_alternate_modes
);
678 static struct attribute
*typec_partner_attrs
[] = {
679 &dev_attr_accessory_mode
.attr
,
680 &dev_attr_supports_usb_power_delivery
.attr
,
681 &dev_attr_number_of_alternate_modes
.attr
,
686 static umode_t
typec_partner_attr_is_visible(struct kobject
*kobj
, struct attribute
*attr
, int n
)
688 struct typec_partner
*partner
= to_typec_partner(kobj_to_dev(kobj
));
690 if (attr
== &dev_attr_number_of_alternate_modes
.attr
) {
691 if (partner
->num_altmodes
< 0)
695 if (attr
== &dev_attr_type
.attr
)
696 if (!get_pd_product_type(kobj_to_dev(kobj
)))
702 static const struct attribute_group typec_partner_group
= {
703 .is_visible
= typec_partner_attr_is_visible
,
704 .attrs
= typec_partner_attrs
707 static const struct attribute_group
*typec_partner_groups
[] = {
708 &typec_partner_group
,
712 static void typec_partner_release(struct device
*dev
)
714 struct typec_partner
*partner
= to_typec_partner(dev
);
716 ida_destroy(&partner
->mode_ids
);
720 static const struct device_type typec_partner_dev_type
= {
721 .name
= "typec_partner",
722 .groups
= typec_partner_groups
,
723 .release
= typec_partner_release
,
727 * typec_partner_set_identity - Report result from Discover Identity command
728 * @partner: The partner updated identity values
730 * This routine is used to report that the result of Discover Identity USB power
731 * delivery command has become available.
733 int typec_partner_set_identity(struct typec_partner
*partner
)
735 if (!partner
->identity
)
738 typec_report_identity(&partner
->dev
);
741 EXPORT_SYMBOL_GPL(typec_partner_set_identity
);
744 * typec_partner_set_num_altmodes - Set the number of available partner altmodes
745 * @partner: The partner to be updated.
746 * @num_altmodes: The number of altmodes we want to specify as available.
748 * This routine is used to report the number of alternate modes supported by the
749 * partner. This value is *not* enforced in alternate mode registration routines.
751 * @partner.num_altmodes is set to -1 on partner registration, denoting that
752 * a valid value has not been set for it yet.
754 * Returns 0 on success or negative error number on failure.
756 int typec_partner_set_num_altmodes(struct typec_partner
*partner
, int num_altmodes
)
760 if (num_altmodes
< 0)
763 partner
->num_altmodes
= num_altmodes
;
764 ret
= sysfs_update_group(&partner
->dev
.kobj
, &typec_partner_group
);
768 sysfs_notify(&partner
->dev
.kobj
, NULL
, "number_of_alternate_modes");
772 EXPORT_SYMBOL_GPL(typec_partner_set_num_altmodes
);
775 * typec_partner_register_altmode - Register USB Type-C Partner Alternate Mode
776 * @partner: USB Type-C Partner that supports the alternate mode
777 * @desc: Description of the alternate mode
779 * This routine is used to register each alternate mode individually that
780 * @partner has listed in response to Discover SVIDs command. The modes for a
781 * SVID listed in response to Discover Modes command need to be listed in an
784 * Returns handle to the alternate mode on success or ERR_PTR on failure.
786 struct typec_altmode
*
787 typec_partner_register_altmode(struct typec_partner
*partner
,
788 const struct typec_altmode_desc
*desc
)
790 return typec_register_altmode(&partner
->dev
, desc
);
792 EXPORT_SYMBOL_GPL(typec_partner_register_altmode
);
795 * typec_register_partner - Register a USB Type-C Partner
796 * @port: The USB Type-C Port the partner is connected to
797 * @desc: Description of the partner
799 * Registers a device for USB Type-C Partner described in @desc.
801 * Returns handle to the partner on success or ERR_PTR on failure.
803 struct typec_partner
*typec_register_partner(struct typec_port
*port
,
804 struct typec_partner_desc
*desc
)
806 struct typec_partner
*partner
;
809 partner
= kzalloc(sizeof(*partner
), GFP_KERNEL
);
811 return ERR_PTR(-ENOMEM
);
813 ida_init(&partner
->mode_ids
);
814 partner
->usb_pd
= desc
->usb_pd
;
815 partner
->accessory
= desc
->accessory
;
816 partner
->num_altmodes
= -1;
818 if (desc
->identity
) {
820 * Creating directory for the identity only if the driver is
821 * able to provide data to it.
823 partner
->dev
.groups
= usb_pd_id_groups
;
824 partner
->identity
= desc
->identity
;
827 partner
->dev
.class = typec_class
;
828 partner
->dev
.parent
= &port
->dev
;
829 partner
->dev
.type
= &typec_partner_dev_type
;
830 dev_set_name(&partner
->dev
, "%s-partner", dev_name(&port
->dev
));
832 ret
= device_register(&partner
->dev
);
834 dev_err(&port
->dev
, "failed to register partner (%d)\n", ret
);
835 put_device(&partner
->dev
);
841 EXPORT_SYMBOL_GPL(typec_register_partner
);
844 * typec_unregister_partner - Unregister a USB Type-C Partner
845 * @partner: The partner to be unregistered
847 * Unregister device created with typec_register_partner().
849 void typec_unregister_partner(struct typec_partner
*partner
)
851 if (!IS_ERR_OR_NULL(partner
))
852 device_unregister(&partner
->dev
);
854 EXPORT_SYMBOL_GPL(typec_unregister_partner
);
856 /* ------------------------------------------------------------------------- */
857 /* Type-C Cable Plugs */
859 static void typec_plug_release(struct device
*dev
)
861 struct typec_plug
*plug
= to_typec_plug(dev
);
863 ida_destroy(&plug
->mode_ids
);
867 static struct attribute
*typec_plug_attrs
[] = {
868 &dev_attr_number_of_alternate_modes
.attr
,
872 static umode_t
typec_plug_attr_is_visible(struct kobject
*kobj
, struct attribute
*attr
, int n
)
874 struct typec_plug
*plug
= to_typec_plug(kobj_to_dev(kobj
));
876 if (attr
== &dev_attr_number_of_alternate_modes
.attr
) {
877 if (plug
->num_altmodes
< 0)
884 static const struct attribute_group typec_plug_group
= {
885 .is_visible
= typec_plug_attr_is_visible
,
886 .attrs
= typec_plug_attrs
889 static const struct attribute_group
*typec_plug_groups
[] = {
894 static const struct device_type typec_plug_dev_type
= {
895 .name
= "typec_plug",
896 .groups
= typec_plug_groups
,
897 .release
= typec_plug_release
,
901 * typec_plug_set_num_altmodes - Set the number of available plug altmodes
902 * @plug: The plug to be updated.
903 * @num_altmodes: The number of altmodes we want to specify as available.
905 * This routine is used to report the number of alternate modes supported by the
906 * plug. This value is *not* enforced in alternate mode registration routines.
908 * @plug.num_altmodes is set to -1 on plug registration, denoting that
909 * a valid value has not been set for it yet.
911 * Returns 0 on success or negative error number on failure.
913 int typec_plug_set_num_altmodes(struct typec_plug
*plug
, int num_altmodes
)
917 if (num_altmodes
< 0)
920 plug
->num_altmodes
= num_altmodes
;
921 ret
= sysfs_update_group(&plug
->dev
.kobj
, &typec_plug_group
);
925 sysfs_notify(&plug
->dev
.kobj
, NULL
, "number_of_alternate_modes");
929 EXPORT_SYMBOL_GPL(typec_plug_set_num_altmodes
);
932 * typec_plug_register_altmode - Register USB Type-C Cable Plug Alternate Mode
933 * @plug: USB Type-C Cable Plug that supports the alternate mode
934 * @desc: Description of the alternate mode
936 * This routine is used to register each alternate mode individually that @plug
937 * has listed in response to Discover SVIDs command. The modes for a SVID that
938 * the plug lists in response to Discover Modes command need to be listed in an
941 * Returns handle to the alternate mode on success or ERR_PTR on failure.
943 struct typec_altmode
*
944 typec_plug_register_altmode(struct typec_plug
*plug
,
945 const struct typec_altmode_desc
*desc
)
947 return typec_register_altmode(&plug
->dev
, desc
);
949 EXPORT_SYMBOL_GPL(typec_plug_register_altmode
);
952 * typec_register_plug - Register a USB Type-C Cable Plug
953 * @cable: USB Type-C Cable with the plug
954 * @desc: Description of the cable plug
956 * Registers a device for USB Type-C Cable Plug described in @desc. A USB Type-C
957 * Cable Plug represents a plug with electronics in it that can response to USB
958 * Power Delivery SOP Prime or SOP Double Prime packages.
960 * Returns handle to the cable plug on success or ERR_PTR on failure.
962 struct typec_plug
*typec_register_plug(struct typec_cable
*cable
,
963 struct typec_plug_desc
*desc
)
965 struct typec_plug
*plug
;
969 plug
= kzalloc(sizeof(*plug
), GFP_KERNEL
);
971 return ERR_PTR(-ENOMEM
);
973 sprintf(name
, "plug%d", desc
->index
);
975 ida_init(&plug
->mode_ids
);
976 plug
->num_altmodes
= -1;
977 plug
->index
= desc
->index
;
978 plug
->dev
.class = typec_class
;
979 plug
->dev
.parent
= &cable
->dev
;
980 plug
->dev
.type
= &typec_plug_dev_type
;
981 dev_set_name(&plug
->dev
, "%s-%s", dev_name(cable
->dev
.parent
), name
);
983 ret
= device_register(&plug
->dev
);
985 dev_err(&cable
->dev
, "failed to register plug (%d)\n", ret
);
986 put_device(&plug
->dev
);
992 EXPORT_SYMBOL_GPL(typec_register_plug
);
995 * typec_unregister_plug - Unregister a USB Type-C Cable Plug
996 * @plug: The cable plug to be unregistered
998 * Unregister device created with typec_register_plug().
1000 void typec_unregister_plug(struct typec_plug
*plug
)
1002 if (!IS_ERR_OR_NULL(plug
))
1003 device_unregister(&plug
->dev
);
1005 EXPORT_SYMBOL_GPL(typec_unregister_plug
);
1009 static const char * const typec_plug_types
[] = {
1010 [USB_PLUG_NONE
] = "unknown",
1011 [USB_PLUG_TYPE_A
] = "type-a",
1012 [USB_PLUG_TYPE_B
] = "type-b",
1013 [USB_PLUG_TYPE_C
] = "type-c",
1014 [USB_PLUG_CAPTIVE
] = "captive",
1017 static ssize_t
plug_type_show(struct device
*dev
,
1018 struct device_attribute
*attr
, char *buf
)
1020 struct typec_cable
*cable
= to_typec_cable(dev
);
1022 return sprintf(buf
, "%s\n", typec_plug_types
[cable
->type
]);
1024 static DEVICE_ATTR_RO(plug_type
);
1026 static struct attribute
*typec_cable_attrs
[] = {
1027 &dev_attr_type
.attr
,
1028 &dev_attr_plug_type
.attr
,
1031 ATTRIBUTE_GROUPS(typec_cable
);
1033 static void typec_cable_release(struct device
*dev
)
1035 struct typec_cable
*cable
= to_typec_cable(dev
);
1040 static const struct device_type typec_cable_dev_type
= {
1041 .name
= "typec_cable",
1042 .groups
= typec_cable_groups
,
1043 .release
= typec_cable_release
,
1046 static int cable_match(struct device
*dev
, void *data
)
1048 return is_typec_cable(dev
);
1052 * typec_cable_get - Get a reference to the USB Type-C cable
1053 * @port: The USB Type-C Port the cable is connected to
1055 * The caller must decrement the reference count with typec_cable_put() after
1058 struct typec_cable
*typec_cable_get(struct typec_port
*port
)
1062 dev
= device_find_child(&port
->dev
, NULL
, cable_match
);
1066 return to_typec_cable(dev
);
1068 EXPORT_SYMBOL_GPL(typec_cable_get
);
1071 * typec_cable_put - Decrement the reference count on USB Type-C cable
1072 * @cable: The USB Type-C cable
1074 void typec_cable_put(struct typec_cable
*cable
)
1076 put_device(&cable
->dev
);
1078 EXPORT_SYMBOL_GPL(typec_cable_put
);
1081 * typec_cable_is_active - Check is the USB Type-C cable active or passive
1082 * @cable: The USB Type-C Cable
1084 * Return 1 if the cable is active or 0 if it's passive.
1086 int typec_cable_is_active(struct typec_cable
*cable
)
1088 return cable
->active
;
1090 EXPORT_SYMBOL_GPL(typec_cable_is_active
);
1093 * typec_cable_set_identity - Report result from Discover Identity command
1094 * @cable: The cable updated identity values
1096 * This routine is used to report that the result of Discover Identity USB power
1097 * delivery command has become available.
1099 int typec_cable_set_identity(struct typec_cable
*cable
)
1101 if (!cable
->identity
)
1104 typec_report_identity(&cable
->dev
);
1107 EXPORT_SYMBOL_GPL(typec_cable_set_identity
);
1110 * typec_register_cable - Register a USB Type-C Cable
1111 * @port: The USB Type-C Port the cable is connected to
1112 * @desc: Description of the cable
1114 * Registers a device for USB Type-C Cable described in @desc. The cable will be
1115 * parent for the optional cable plug devises.
1117 * Returns handle to the cable on success or ERR_PTR on failure.
1119 struct typec_cable
*typec_register_cable(struct typec_port
*port
,
1120 struct typec_cable_desc
*desc
)
1122 struct typec_cable
*cable
;
1125 cable
= kzalloc(sizeof(*cable
), GFP_KERNEL
);
1127 return ERR_PTR(-ENOMEM
);
1129 cable
->type
= desc
->type
;
1130 cable
->active
= desc
->active
;
1132 if (desc
->identity
) {
1134 * Creating directory for the identity only if the driver is
1135 * able to provide data to it.
1137 cable
->dev
.groups
= usb_pd_id_groups
;
1138 cable
->identity
= desc
->identity
;
1141 cable
->dev
.class = typec_class
;
1142 cable
->dev
.parent
= &port
->dev
;
1143 cable
->dev
.type
= &typec_cable_dev_type
;
1144 dev_set_name(&cable
->dev
, "%s-cable", dev_name(&port
->dev
));
1146 ret
= device_register(&cable
->dev
);
1148 dev_err(&port
->dev
, "failed to register cable (%d)\n", ret
);
1149 put_device(&cable
->dev
);
1150 return ERR_PTR(ret
);
1155 EXPORT_SYMBOL_GPL(typec_register_cable
);
1158 * typec_unregister_cable - Unregister a USB Type-C Cable
1159 * @cable: The cable to be unregistered
1161 * Unregister device created with typec_register_cable().
1163 void typec_unregister_cable(struct typec_cable
*cable
)
1165 if (!IS_ERR_OR_NULL(cable
))
1166 device_unregister(&cable
->dev
);
1168 EXPORT_SYMBOL_GPL(typec_unregister_cable
);
1170 /* ------------------------------------------------------------------------- */
1171 /* USB Type-C ports */
1173 static const char * const typec_orientations
[] = {
1174 [TYPEC_ORIENTATION_NONE
] = "unknown",
1175 [TYPEC_ORIENTATION_NORMAL
] = "normal",
1176 [TYPEC_ORIENTATION_REVERSE
] = "reverse",
1179 static const char * const typec_roles
[] = {
1180 [TYPEC_SINK
] = "sink",
1181 [TYPEC_SOURCE
] = "source",
1184 static const char * const typec_data_roles
[] = {
1185 [TYPEC_DEVICE
] = "device",
1186 [TYPEC_HOST
] = "host",
1189 static const char * const typec_port_power_roles
[] = {
1190 [TYPEC_PORT_SRC
] = "source",
1191 [TYPEC_PORT_SNK
] = "sink",
1192 [TYPEC_PORT_DRP
] = "dual",
1195 static const char * const typec_port_data_roles
[] = {
1196 [TYPEC_PORT_DFP
] = "host",
1197 [TYPEC_PORT_UFP
] = "device",
1198 [TYPEC_PORT_DRD
] = "dual",
1201 static const char * const typec_port_types_drp
[] = {
1202 [TYPEC_PORT_SRC
] = "dual [source] sink",
1203 [TYPEC_PORT_SNK
] = "dual source [sink]",
1204 [TYPEC_PORT_DRP
] = "[dual] source sink",
1208 preferred_role_store(struct device
*dev
, struct device_attribute
*attr
,
1209 const char *buf
, size_t size
)
1211 struct typec_port
*port
= to_typec_port(dev
);
1215 if (port
->cap
->type
!= TYPEC_PORT_DRP
) {
1216 dev_dbg(dev
, "Preferred role only supported with DRP ports\n");
1220 if (!port
->ops
|| !port
->ops
->try_role
) {
1221 dev_dbg(dev
, "Setting preferred role not supported\n");
1225 role
= sysfs_match_string(typec_roles
, buf
);
1227 if (sysfs_streq(buf
, "none"))
1228 role
= TYPEC_NO_PREFERRED_ROLE
;
1233 ret
= port
->ops
->try_role(port
, role
);
1237 port
->prefer_role
= role
;
1242 preferred_role_show(struct device
*dev
, struct device_attribute
*attr
,
1245 struct typec_port
*port
= to_typec_port(dev
);
1247 if (port
->cap
->type
!= TYPEC_PORT_DRP
)
1250 if (port
->prefer_role
< 0)
1253 return sprintf(buf
, "%s\n", typec_roles
[port
->prefer_role
]);
1255 static DEVICE_ATTR_RW(preferred_role
);
1257 static ssize_t
data_role_store(struct device
*dev
,
1258 struct device_attribute
*attr
,
1259 const char *buf
, size_t size
)
1261 struct typec_port
*port
= to_typec_port(dev
);
1264 if (!port
->ops
|| !port
->ops
->dr_set
) {
1265 dev_dbg(dev
, "data role swapping not supported\n");
1269 ret
= sysfs_match_string(typec_data_roles
, buf
);
1273 mutex_lock(&port
->port_type_lock
);
1274 if (port
->cap
->data
!= TYPEC_PORT_DRD
) {
1276 goto unlock_and_ret
;
1279 ret
= port
->ops
->dr_set(port
, ret
);
1281 goto unlock_and_ret
;
1285 mutex_unlock(&port
->port_type_lock
);
1289 static ssize_t
data_role_show(struct device
*dev
,
1290 struct device_attribute
*attr
, char *buf
)
1292 struct typec_port
*port
= to_typec_port(dev
);
1294 if (port
->cap
->data
== TYPEC_PORT_DRD
)
1295 return sprintf(buf
, "%s\n", port
->data_role
== TYPEC_HOST
?
1296 "[host] device" : "host [device]");
1298 return sprintf(buf
, "[%s]\n", typec_data_roles
[port
->data_role
]);
1300 static DEVICE_ATTR_RW(data_role
);
1302 static ssize_t
power_role_store(struct device
*dev
,
1303 struct device_attribute
*attr
,
1304 const char *buf
, size_t size
)
1306 struct typec_port
*port
= to_typec_port(dev
);
1309 if (!port
->ops
|| !port
->ops
->pr_set
) {
1310 dev_dbg(dev
, "power role swapping not supported\n");
1314 if (port
->pwr_opmode
!= TYPEC_PWR_MODE_PD
) {
1315 dev_dbg(dev
, "partner unable to swap power role\n");
1319 ret
= sysfs_match_string(typec_roles
, buf
);
1323 mutex_lock(&port
->port_type_lock
);
1324 if (port
->port_type
!= TYPEC_PORT_DRP
) {
1325 dev_dbg(dev
, "port type fixed at \"%s\"",
1326 typec_port_power_roles
[port
->port_type
]);
1328 goto unlock_and_ret
;
1331 ret
= port
->ops
->pr_set(port
, ret
);
1333 goto unlock_and_ret
;
1337 mutex_unlock(&port
->port_type_lock
);
1341 static ssize_t
power_role_show(struct device
*dev
,
1342 struct device_attribute
*attr
, char *buf
)
1344 struct typec_port
*port
= to_typec_port(dev
);
1346 if (port
->cap
->type
== TYPEC_PORT_DRP
)
1347 return sprintf(buf
, "%s\n", port
->pwr_role
== TYPEC_SOURCE
?
1348 "[source] sink" : "source [sink]");
1350 return sprintf(buf
, "[%s]\n", typec_roles
[port
->pwr_role
]);
1352 static DEVICE_ATTR_RW(power_role
);
1355 port_type_store(struct device
*dev
, struct device_attribute
*attr
,
1356 const char *buf
, size_t size
)
1358 struct typec_port
*port
= to_typec_port(dev
);
1360 enum typec_port_type type
;
1362 if (port
->cap
->type
!= TYPEC_PORT_DRP
||
1363 !port
->ops
|| !port
->ops
->port_type_set
) {
1364 dev_dbg(dev
, "changing port type not supported\n");
1368 ret
= sysfs_match_string(typec_port_power_roles
, buf
);
1373 mutex_lock(&port
->port_type_lock
);
1375 if (port
->port_type
== type
) {
1377 goto unlock_and_ret
;
1380 ret
= port
->ops
->port_type_set(port
, type
);
1382 goto unlock_and_ret
;
1384 port
->port_type
= type
;
1388 mutex_unlock(&port
->port_type_lock
);
1393 port_type_show(struct device
*dev
, struct device_attribute
*attr
,
1396 struct typec_port
*port
= to_typec_port(dev
);
1398 if (port
->cap
->type
== TYPEC_PORT_DRP
)
1399 return sprintf(buf
, "%s\n",
1400 typec_port_types_drp
[port
->port_type
]);
1402 return sprintf(buf
, "[%s]\n", typec_port_power_roles
[port
->cap
->type
]);
1404 static DEVICE_ATTR_RW(port_type
);
1406 static const char * const typec_pwr_opmodes
[] = {
1407 [TYPEC_PWR_MODE_USB
] = "default",
1408 [TYPEC_PWR_MODE_1_5A
] = "1.5A",
1409 [TYPEC_PWR_MODE_3_0A
] = "3.0A",
1410 [TYPEC_PWR_MODE_PD
] = "usb_power_delivery",
1413 static ssize_t
power_operation_mode_show(struct device
*dev
,
1414 struct device_attribute
*attr
,
1417 struct typec_port
*port
= to_typec_port(dev
);
1419 return sprintf(buf
, "%s\n", typec_pwr_opmodes
[port
->pwr_opmode
]);
1421 static DEVICE_ATTR_RO(power_operation_mode
);
1423 static ssize_t
vconn_source_store(struct device
*dev
,
1424 struct device_attribute
*attr
,
1425 const char *buf
, size_t size
)
1427 struct typec_port
*port
= to_typec_port(dev
);
1431 if (!port
->cap
->pd_revision
) {
1432 dev_dbg(dev
, "VCONN swap depends on USB Power Delivery\n");
1436 if (!port
->ops
|| !port
->ops
->vconn_set
) {
1437 dev_dbg(dev
, "VCONN swapping not supported\n");
1441 ret
= kstrtobool(buf
, &source
);
1445 ret
= port
->ops
->vconn_set(port
, (enum typec_role
)source
);
1452 static ssize_t
vconn_source_show(struct device
*dev
,
1453 struct device_attribute
*attr
, char *buf
)
1455 struct typec_port
*port
= to_typec_port(dev
);
1457 return sprintf(buf
, "%s\n",
1458 port
->vconn_role
== TYPEC_SOURCE
? "yes" : "no");
1460 static DEVICE_ATTR_RW(vconn_source
);
1462 static ssize_t
supported_accessory_modes_show(struct device
*dev
,
1463 struct device_attribute
*attr
,
1466 struct typec_port
*port
= to_typec_port(dev
);
1470 for (i
= 0; i
< ARRAY_SIZE(port
->cap
->accessory
); i
++) {
1471 if (port
->cap
->accessory
[i
])
1472 ret
+= sprintf(buf
+ ret
, "%s ",
1473 typec_accessory_modes
[port
->cap
->accessory
[i
]]);
1477 return sprintf(buf
, "none\n");
1479 buf
[ret
- 1] = '\n';
1483 static DEVICE_ATTR_RO(supported_accessory_modes
);
1485 static ssize_t
usb_typec_revision_show(struct device
*dev
,
1486 struct device_attribute
*attr
,
1489 struct typec_port
*port
= to_typec_port(dev
);
1490 u16 rev
= port
->cap
->revision
;
1492 return sprintf(buf
, "%d.%d\n", (rev
>> 8) & 0xff, (rev
>> 4) & 0xf);
1494 static DEVICE_ATTR_RO(usb_typec_revision
);
1496 static ssize_t
usb_power_delivery_revision_show(struct device
*dev
,
1497 struct device_attribute
*attr
,
1500 struct typec_port
*p
= to_typec_port(dev
);
1502 return sprintf(buf
, "%d\n", (p
->cap
->pd_revision
>> 8) & 0xff);
1504 static DEVICE_ATTR_RO(usb_power_delivery_revision
);
1506 static ssize_t
orientation_show(struct device
*dev
,
1507 struct device_attribute
*attr
,
1510 struct typec_port
*port
= to_typec_port(dev
);
1512 return sprintf(buf
, "%s\n", typec_orientations
[port
->orientation
]);
1514 static DEVICE_ATTR_RO(orientation
);
1516 static struct attribute
*typec_attrs
[] = {
1517 &dev_attr_data_role
.attr
,
1518 &dev_attr_power_operation_mode
.attr
,
1519 &dev_attr_power_role
.attr
,
1520 &dev_attr_preferred_role
.attr
,
1521 &dev_attr_supported_accessory_modes
.attr
,
1522 &dev_attr_usb_power_delivery_revision
.attr
,
1523 &dev_attr_usb_typec_revision
.attr
,
1524 &dev_attr_vconn_source
.attr
,
1525 &dev_attr_port_type
.attr
,
1526 &dev_attr_orientation
.attr
,
1530 static umode_t
typec_attr_is_visible(struct kobject
*kobj
,
1531 struct attribute
*attr
, int n
)
1533 struct typec_port
*port
= to_typec_port(kobj_to_dev(kobj
));
1535 if (attr
== &dev_attr_data_role
.attr
) {
1536 if (port
->cap
->data
!= TYPEC_PORT_DRD
||
1537 !port
->ops
|| !port
->ops
->dr_set
)
1539 } else if (attr
== &dev_attr_power_role
.attr
) {
1540 if (port
->cap
->type
!= TYPEC_PORT_DRP
||
1541 !port
->ops
|| !port
->ops
->pr_set
)
1543 } else if (attr
== &dev_attr_vconn_source
.attr
) {
1544 if (!port
->cap
->pd_revision
||
1545 !port
->ops
|| !port
->ops
->vconn_set
)
1547 } else if (attr
== &dev_attr_preferred_role
.attr
) {
1548 if (port
->cap
->type
!= TYPEC_PORT_DRP
||
1549 !port
->ops
|| !port
->ops
->try_role
)
1551 } else if (attr
== &dev_attr_port_type
.attr
) {
1552 if (!port
->ops
|| !port
->ops
->port_type_set
)
1554 if (port
->cap
->type
!= TYPEC_PORT_DRP
)
1556 } else if (attr
== &dev_attr_orientation
.attr
) {
1557 if (port
->cap
->orientation_aware
)
1565 static const struct attribute_group typec_group
= {
1566 .is_visible
= typec_attr_is_visible
,
1567 .attrs
= typec_attrs
,
1570 static const struct attribute_group
*typec_groups
[] = {
1575 static int typec_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
1579 ret
= add_uevent_var(env
, "TYPEC_PORT=%s", dev_name(dev
));
1581 dev_err(dev
, "failed to add uevent TYPEC_PORT\n");
1586 static void typec_release(struct device
*dev
)
1588 struct typec_port
*port
= to_typec_port(dev
);
1590 ida_simple_remove(&typec_index_ida
, port
->id
);
1591 ida_destroy(&port
->mode_ids
);
1592 typec_switch_put(port
->sw
);
1593 typec_mux_put(port
->mux
);
1598 const struct device_type typec_port_dev_type
= {
1599 .name
= "typec_port",
1600 .groups
= typec_groups
,
1601 .uevent
= typec_uevent
,
1602 .release
= typec_release
,
1605 /* --------------------------------------- */
1606 /* Driver callbacks to report role updates */
1608 static int partner_match(struct device
*dev
, void *data
)
1610 return is_typec_partner(dev
);
1614 * typec_set_data_role - Report data role change
1615 * @port: The USB Type-C Port where the role was changed
1616 * @role: The new data role
1618 * This routine is used by the port drivers to report data role changes.
1620 void typec_set_data_role(struct typec_port
*port
, enum typec_data_role role
)
1622 struct device
*partner_dev
;
1624 if (port
->data_role
== role
)
1627 port
->data_role
= role
;
1628 sysfs_notify(&port
->dev
.kobj
, NULL
, "data_role");
1629 kobject_uevent(&port
->dev
.kobj
, KOBJ_CHANGE
);
1631 partner_dev
= device_find_child(&port
->dev
, NULL
, partner_match
);
1635 if (to_typec_partner(partner_dev
)->identity
)
1636 typec_product_type_notify(partner_dev
);
1638 put_device(partner_dev
);
1640 EXPORT_SYMBOL_GPL(typec_set_data_role
);
1643 * typec_set_pwr_role - Report power role change
1644 * @port: The USB Type-C Port where the role was changed
1645 * @role: The new data role
1647 * This routine is used by the port drivers to report power role changes.
1649 void typec_set_pwr_role(struct typec_port
*port
, enum typec_role role
)
1651 if (port
->pwr_role
== role
)
1654 port
->pwr_role
= role
;
1655 sysfs_notify(&port
->dev
.kobj
, NULL
, "power_role");
1656 kobject_uevent(&port
->dev
.kobj
, KOBJ_CHANGE
);
1658 EXPORT_SYMBOL_GPL(typec_set_pwr_role
);
1661 * typec_set_vconn_role - Report VCONN source change
1662 * @port: The USB Type-C Port which VCONN role changed
1663 * @role: Source when @port is sourcing VCONN, or Sink when it's not
1665 * This routine is used by the port drivers to report if the VCONN source is
1668 void typec_set_vconn_role(struct typec_port
*port
, enum typec_role role
)
1670 if (port
->vconn_role
== role
)
1673 port
->vconn_role
= role
;
1674 sysfs_notify(&port
->dev
.kobj
, NULL
, "vconn_source");
1675 kobject_uevent(&port
->dev
.kobj
, KOBJ_CHANGE
);
1677 EXPORT_SYMBOL_GPL(typec_set_vconn_role
);
1680 * typec_set_pwr_opmode - Report changed power operation mode
1681 * @port: The USB Type-C Port where the mode was changed
1682 * @opmode: New power operation mode
1684 * This routine is used by the port drivers to report changed power operation
1685 * mode in @port. The modes are USB (default), 1.5A, 3.0A as defined in USB
1686 * Type-C specification, and "USB Power Delivery" when the power levels are
1687 * negotiated with methods defined in USB Power Delivery specification.
1689 void typec_set_pwr_opmode(struct typec_port
*port
,
1690 enum typec_pwr_opmode opmode
)
1692 struct device
*partner_dev
;
1694 if (port
->pwr_opmode
== opmode
)
1697 port
->pwr_opmode
= opmode
;
1698 sysfs_notify(&port
->dev
.kobj
, NULL
, "power_operation_mode");
1699 kobject_uevent(&port
->dev
.kobj
, KOBJ_CHANGE
);
1701 partner_dev
= device_find_child(&port
->dev
, NULL
, partner_match
);
1703 struct typec_partner
*partner
= to_typec_partner(partner_dev
);
1705 if (opmode
== TYPEC_PWR_MODE_PD
&& !partner
->usb_pd
) {
1706 partner
->usb_pd
= 1;
1707 sysfs_notify(&partner_dev
->kobj
, NULL
,
1708 "supports_usb_power_delivery");
1710 put_device(partner_dev
);
1713 EXPORT_SYMBOL_GPL(typec_set_pwr_opmode
);
1716 * typec_find_pwr_opmode - Get the typec power operation mode capability
1717 * @name: power operation mode string
1719 * This routine is used to find the typec_pwr_opmode by its string @name.
1721 * Returns typec_pwr_opmode if success, otherwise negative error code.
1723 int typec_find_pwr_opmode(const char *name
)
1725 return match_string(typec_pwr_opmodes
,
1726 ARRAY_SIZE(typec_pwr_opmodes
), name
);
1728 EXPORT_SYMBOL_GPL(typec_find_pwr_opmode
);
1731 * typec_find_orientation - Convert orientation string to enum typec_orientation
1732 * @name: Orientation string
1734 * This routine is used to find the typec_orientation by its string name @name.
1736 * Returns the orientation value on success, otherwise negative error code.
1738 int typec_find_orientation(const char *name
)
1740 return match_string(typec_orientations
, ARRAY_SIZE(typec_orientations
),
1743 EXPORT_SYMBOL_GPL(typec_find_orientation
);
1746 * typec_find_port_power_role - Get the typec port power capability
1747 * @name: port power capability string
1749 * This routine is used to find the typec_port_type by its string name.
1751 * Returns typec_port_type if success, otherwise negative error code.
1753 int typec_find_port_power_role(const char *name
)
1755 return match_string(typec_port_power_roles
,
1756 ARRAY_SIZE(typec_port_power_roles
), name
);
1758 EXPORT_SYMBOL_GPL(typec_find_port_power_role
);
1761 * typec_find_power_role - Find the typec one specific power role
1762 * @name: power role string
1764 * This routine is used to find the typec_role by its string name.
1766 * Returns typec_role if success, otherwise negative error code.
1768 int typec_find_power_role(const char *name
)
1770 return match_string(typec_roles
, ARRAY_SIZE(typec_roles
), name
);
1772 EXPORT_SYMBOL_GPL(typec_find_power_role
);
1775 * typec_find_port_data_role - Get the typec port data capability
1776 * @name: port data capability string
1778 * This routine is used to find the typec_port_data by its string name.
1780 * Returns typec_port_data if success, otherwise negative error code.
1782 int typec_find_port_data_role(const char *name
)
1784 return match_string(typec_port_data_roles
,
1785 ARRAY_SIZE(typec_port_data_roles
), name
);
1787 EXPORT_SYMBOL_GPL(typec_find_port_data_role
);
1789 /* ------------------------------------------ */
1790 /* API for Multiplexer/DeMultiplexer Switches */
1793 * typec_set_orientation - Set USB Type-C cable plug orientation
1794 * @port: USB Type-C Port
1795 * @orientation: USB Type-C cable plug orientation
1797 * Set cable plug orientation for @port.
1799 int typec_set_orientation(struct typec_port
*port
,
1800 enum typec_orientation orientation
)
1804 ret
= typec_switch_set(port
->sw
, orientation
);
1808 port
->orientation
= orientation
;
1809 sysfs_notify(&port
->dev
.kobj
, NULL
, "orientation");
1810 kobject_uevent(&port
->dev
.kobj
, KOBJ_CHANGE
);
1814 EXPORT_SYMBOL_GPL(typec_set_orientation
);
1817 * typec_get_orientation - Get USB Type-C cable plug orientation
1818 * @port: USB Type-C Port
1820 * Get current cable plug orientation for @port.
1822 enum typec_orientation
typec_get_orientation(struct typec_port
*port
)
1824 return port
->orientation
;
1826 EXPORT_SYMBOL_GPL(typec_get_orientation
);
1829 * typec_set_mode - Set mode of operation for USB Type-C connector
1830 * @port: USB Type-C connector
1831 * @mode: Accessory Mode, USB Operation or Safe State
1833 * Configure @port for Accessory Mode @mode. This function will configure the
1834 * muxes needed for @mode.
1836 int typec_set_mode(struct typec_port
*port
, int mode
)
1838 struct typec_mux_state state
= { };
1842 return typec_mux_set(port
->mux
, &state
);
1844 EXPORT_SYMBOL_GPL(typec_set_mode
);
1846 /* --------------------------------------- */
1849 * typec_get_drvdata - Return private driver data pointer
1850 * @port: USB Type-C port
1852 void *typec_get_drvdata(struct typec_port
*port
)
1854 return dev_get_drvdata(&port
->dev
);
1856 EXPORT_SYMBOL_GPL(typec_get_drvdata
);
1859 * typec_port_register_altmode - Register USB Type-C Port Alternate Mode
1860 * @port: USB Type-C Port that supports the alternate mode
1861 * @desc: Description of the alternate mode
1863 * This routine is used to register an alternate mode that @port is capable of
1866 * Returns handle to the alternate mode on success or ERR_PTR on failure.
1868 struct typec_altmode
*
1869 typec_port_register_altmode(struct typec_port
*port
,
1870 const struct typec_altmode_desc
*desc
)
1872 struct typec_altmode
*adev
;
1873 struct typec_mux
*mux
;
1875 mux
= typec_mux_get(&port
->dev
, desc
);
1877 return ERR_CAST(mux
);
1879 adev
= typec_register_altmode(&port
->dev
, desc
);
1883 to_altmode(adev
)->mux
= mux
;
1887 EXPORT_SYMBOL_GPL(typec_port_register_altmode
);
1890 * typec_register_port - Register a USB Type-C Port
1891 * @parent: Parent device
1892 * @cap: Description of the port
1894 * Registers a device for USB Type-C Port described in @cap.
1896 * Returns handle to the port on success or ERR_PTR on failure.
1898 struct typec_port
*typec_register_port(struct device
*parent
,
1899 const struct typec_capability
*cap
)
1901 struct typec_port
*port
;
1905 port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
1907 return ERR_PTR(-ENOMEM
);
1909 id
= ida_simple_get(&typec_index_ida
, 0, 0, GFP_KERNEL
);
1915 switch (cap
->type
) {
1916 case TYPEC_PORT_SRC
:
1917 port
->pwr_role
= TYPEC_SOURCE
;
1918 port
->vconn_role
= TYPEC_SOURCE
;
1920 case TYPEC_PORT_SNK
:
1921 port
->pwr_role
= TYPEC_SINK
;
1922 port
->vconn_role
= TYPEC_SINK
;
1924 case TYPEC_PORT_DRP
:
1925 if (cap
->prefer_role
!= TYPEC_NO_PREFERRED_ROLE
)
1926 port
->pwr_role
= cap
->prefer_role
;
1928 port
->pwr_role
= TYPEC_SINK
;
1932 switch (cap
->data
) {
1933 case TYPEC_PORT_DFP
:
1934 port
->data_role
= TYPEC_HOST
;
1936 case TYPEC_PORT_UFP
:
1937 port
->data_role
= TYPEC_DEVICE
;
1939 case TYPEC_PORT_DRD
:
1940 if (cap
->prefer_role
== TYPEC_SOURCE
)
1941 port
->data_role
= TYPEC_HOST
;
1943 port
->data_role
= TYPEC_DEVICE
;
1947 ida_init(&port
->mode_ids
);
1948 mutex_init(&port
->port_type_lock
);
1951 port
->ops
= cap
->ops
;
1952 port
->port_type
= cap
->type
;
1953 port
->prefer_role
= cap
->prefer_role
;
1955 device_initialize(&port
->dev
);
1956 port
->dev
.class = typec_class
;
1957 port
->dev
.parent
= parent
;
1958 port
->dev
.fwnode
= cap
->fwnode
;
1959 port
->dev
.type
= &typec_port_dev_type
;
1960 dev_set_name(&port
->dev
, "port%d", id
);
1961 dev_set_drvdata(&port
->dev
, cap
->driver_data
);
1963 port
->cap
= kmemdup(cap
, sizeof(*cap
), GFP_KERNEL
);
1965 put_device(&port
->dev
);
1966 return ERR_PTR(-ENOMEM
);
1969 port
->sw
= typec_switch_get(&port
->dev
);
1970 if (IS_ERR(port
->sw
)) {
1971 ret
= PTR_ERR(port
->sw
);
1972 put_device(&port
->dev
);
1973 return ERR_PTR(ret
);
1976 port
->mux
= typec_mux_get(&port
->dev
, NULL
);
1977 if (IS_ERR(port
->mux
)) {
1978 ret
= PTR_ERR(port
->mux
);
1979 put_device(&port
->dev
);
1980 return ERR_PTR(ret
);
1983 ret
= device_add(&port
->dev
);
1985 dev_err(parent
, "failed to register port (%d)\n", ret
);
1986 put_device(&port
->dev
);
1987 return ERR_PTR(ret
);
1992 EXPORT_SYMBOL_GPL(typec_register_port
);
1995 * typec_unregister_port - Unregister a USB Type-C Port
1996 * @port: The port to be unregistered
1998 * Unregister device created with typec_register_port().
2000 void typec_unregister_port(struct typec_port
*port
)
2002 if (!IS_ERR_OR_NULL(port
))
2003 device_unregister(&port
->dev
);
2005 EXPORT_SYMBOL_GPL(typec_unregister_port
);
2007 static int __init
typec_init(void)
2011 ret
= bus_register(&typec_bus
);
2015 ret
= class_register(&typec_mux_class
);
2017 goto err_unregister_bus
;
2019 typec_class
= class_create(THIS_MODULE
, "typec");
2020 if (IS_ERR(typec_class
)) {
2021 ret
= PTR_ERR(typec_class
);
2022 goto err_unregister_mux_class
;
2027 err_unregister_mux_class
:
2028 class_unregister(&typec_mux_class
);
2031 bus_unregister(&typec_bus
);
2035 subsys_initcall(typec_init
);
2037 static void __exit
typec_exit(void)
2039 class_destroy(typec_class
);
2040 ida_destroy(&typec_index_ida
);
2041 bus_unregister(&typec_bus
);
2042 class_unregister(&typec_mux_class
);
2044 module_exit(typec_exit
);
2046 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
2047 MODULE_LICENSE("GPL v2");
2048 MODULE_DESCRIPTION("USB Type-C Connector Class");