Skip to content

Commit 8c49c9e

Browse files
Heikki Krogerusgregkh
authored andcommitted
usb: typec: Add typec_find_orientation()
Function that converts orientation string into orientation value. Signed-off-by: Heikki Krogerus <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 3e63cff commit 8c49c9e

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

drivers/usb/typec/class.c

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,12 @@ EXPORT_SYMBOL_GPL(typec_unregister_cable);
917917
/* ------------------------------------------------------------------------- */
918918
/* USB Type-C ports */
919919

920+
static const char * const typec_orientations[] = {
921+
[TYPEC_ORIENTATION_NONE] = "unknown",
922+
[TYPEC_ORIENTATION_NORMAL] = "normal",
923+
[TYPEC_ORIENTATION_REVERSE] = "reverse",
924+
};
925+
920926
static const char * const typec_roles[] = {
921927
[TYPEC_SINK] = "sink",
922928
[TYPEC_SOURCE] = "source",
@@ -1248,18 +1254,9 @@ static ssize_t orientation_show(struct device *dev,
12481254
struct device_attribute *attr,
12491255
char *buf)
12501256
{
1251-
struct typec_port *p = to_typec_port(dev);
1252-
enum typec_orientation orientation = typec_get_orientation(p);
1253-
1254-
switch (orientation) {
1255-
case TYPEC_ORIENTATION_NORMAL:
1256-
return sprintf(buf, "%s\n", "normal");
1257-
case TYPEC_ORIENTATION_REVERSE:
1258-
return sprintf(buf, "%s\n", "reverse");
1259-
case TYPEC_ORIENTATION_NONE:
1260-
default:
1261-
return sprintf(buf, "%s\n", "unknown");
1262-
}
1257+
struct typec_port *port = to_typec_port(dev);
1258+
1259+
return sprintf(buf, "%s\n", typec_orientations[port->orientation]);
12631260
}
12641261
static DEVICE_ATTR_RO(orientation);
12651262

@@ -1451,6 +1448,21 @@ void typec_set_pwr_opmode(struct typec_port *port,
14511448
}
14521449
EXPORT_SYMBOL_GPL(typec_set_pwr_opmode);
14531450

1451+
/**
1452+
* typec_find_orientation - Convert orientation string to enum typec_orientation
1453+
* @name: Orientation string
1454+
*
1455+
* This routine is used to find the typec_orientation by its string name @name.
1456+
*
1457+
* Returns the orientation value on success, otherwise negative error code.
1458+
*/
1459+
int typec_find_orientation(const char *name)
1460+
{
1461+
return match_string(typec_orientations, ARRAY_SIZE(typec_orientations),
1462+
name);
1463+
}
1464+
EXPORT_SYMBOL_GPL(typec_find_orientation);
1465+
14541466
/**
14551467
* typec_find_port_power_role - Get the typec port power capability
14561468
* @name: port power capability string

include/linux/usb/typec.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ int typec_set_mode(struct typec_port *port, int mode);
254254

255255
void *typec_get_drvdata(struct typec_port *port);
256256

257+
int typec_find_orientation(const char *name);
257258
int typec_find_port_power_role(const char *name);
258259
int typec_find_power_role(const char *name);
259260
int typec_find_port_data_role(const char *name);

0 commit comments

Comments
 (0)