diff mbox series

[3/4] usb: typec: add typec orientation switch support via mux controller

Message ID 1621408490-23811-4-git-send-email-jun.li@nxp.com
State New
Headers show
Series typec switch via mux controller | expand

Commit Message

Jun Li May 19, 2021, 7:14 a.m. UTC
Some dedicated mux block can use existing mux controller as a
mux provider, typec port as a consumer to select channel for
orientation switch, this can be an alternate way to current
typec_switch interface.

Signed-off-by: Li Jun <jun.li@nxp.com>
---
 drivers/usb/typec/class.c     | 26 +++++++++++++++++++++++++-
 drivers/usb/typec/class.h     |  2 ++
 drivers/usb/typec/mux.c       | 34 ++++++++++++++++++++++++++++++++++
 include/linux/usb/typec_mux.h |  4 ++++
 4 files changed, 65 insertions(+), 1 deletion(-)

Comments

Jun Li May 21, 2021, 1:02 p.m. UTC | #1
> -----Original Message-----

> From: Heikki Krogerus <heikki.krogerus@linux.intel.com>

> Sent: Thursday, May 20, 2021 8:34 PM

> To: Jun Li <jun.li@nxp.com>

> Cc: robh+dt@kernel.org; shawnguo@kernel.org; gregkh@linuxfoundation.org;

> linux@roeck-us.net; linux-usb@vger.kernel.org; dl-linux-imx

> <linux-imx@nxp.com>; devicetree@vger.kernel.org;

> linux-arm-kernel@lists.infradead.org

> Subject: Re: [PATCH 3/4] usb: typec: add typec orientation switch support

> via mux controller

> 

> On Wed, May 19, 2021 at 03:14:49PM +0800, Li Jun wrote:

> > Some dedicated mux block can use existing mux controller as a mux

> > provider, typec port as a consumer to select channel for orientation

> > switch, this can be an alternate way to current typec_switch

> > interface.

> >

> > Signed-off-by: Li Jun <jun.li@nxp.com>

> > ---

> >  drivers/usb/typec/class.c     | 26 +++++++++++++++++++++++++-

> >  drivers/usb/typec/class.h     |  2 ++

> >  drivers/usb/typec/mux.c       | 34 ++++++++++++++++++++++++++++++++++

> >  include/linux/usb/typec_mux.h |  4 ++++

> >  4 files changed, 65 insertions(+), 1 deletion(-)

> >

> > diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c

> > index a29bf2c32233..1bb0275e6204 100644

> > --- a/drivers/usb/typec/class.c

> > +++ b/drivers/usb/typec/class.c

> > @@ -1601,6 +1601,7 @@ static void typec_release(struct device *dev)

> >  	ida_simple_remove(&typec_index_ida, port->id);

> >  	ida_destroy(&port->mode_ids);

> >  	typec_switch_put(port->sw);

> > +	typec_mux_control_switch_put(port->mux_control_switch);

> >  	typec_mux_put(port->mux);

> >  	kfree(port->cap);

> >  	kfree(port);

> > @@ -1816,6 +1817,13 @@ int typec_set_orientation(struct typec_port *port,

> >  	if (ret)

> >  		return ret;

> >

> > +	if (!port->sw) {

> > +		ret = typec_mux_control_switch_set(port->mux_control_switch,

> > +				port->mux_control_switch_states[orientation]);

> > +		if (ret)

> > +			return ret;

> > +	}

> > +

> >  	port->orientation = orientation;

> >  	sysfs_notify(&port->dev.kobj, NULL, "orientation");

> >  	kobject_uevent(&port->dev.kobj, KOBJ_CHANGE); @@ -1991,7 +1999,7 @@

> > struct typec_port *typec_register_port(struct device *parent,

> >  				       const struct typec_capability *cap)  {

> >  	struct typec_port *port;

> > -	int ret;

> > +	int ret = 0;

> >  	int id;

> >

> >  	port = kzalloc(sizeof(*port), GFP_KERNEL); @@ -2068,6 +2076,22 @@

> > struct typec_port *typec_register_port(struct device *parent,

> >  		return ERR_PTR(ret);

> >  	}

> >

> > +	if (!port->sw) {

> > +		/* Try to get typec switch via general mux controller */

> > +		port->mux_control_switch =

> typec_mux_control_switch_get(&port->dev);

> > +		if (IS_ERR(port->mux_control_switch))

> > +			ret = PTR_ERR(port->mux_control_switch);

> > +		else if (port->mux_control_switch)

> > +			ret = device_property_read_u32_array(&port->dev,

> > +					"mux-control-switch-states",

> > +					port->mux_control_switch_states,

> > +					3);

> > +		if (ret) {

> > +			put_device(&port->dev);

> > +			return ERR_PTR(ret);

> > +		}

> > +	}

> 

> Why not just do that inside fwnode_typec_switch_get() and handle the whole

> thing in drivers/usb/typec/mux.c (or in its own file if you prefer)?

> 

> You'll just need to register a "wrapper" Type-C switch object for the OF

> mux controller, but that should not be a problem. That way you don't need

> to export any new functions, touch this file or anything else.

> 


Okay, so stick to current typec_switch is preferred, actually I hesitated
on this, I know that approach will have a unified interface, but with
the cost of creating it only for wrap.

My v2 will go with the direction you suggested.

Thanks
Li Jun

> 

> thanks,

> 

> --

> heikki
diff mbox series

Patch

diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
index a29bf2c32233..1bb0275e6204 100644
--- a/drivers/usb/typec/class.c
+++ b/drivers/usb/typec/class.c
@@ -1601,6 +1601,7 @@  static void typec_release(struct device *dev)
 	ida_simple_remove(&typec_index_ida, port->id);
 	ida_destroy(&port->mode_ids);
 	typec_switch_put(port->sw);
+	typec_mux_control_switch_put(port->mux_control_switch);
 	typec_mux_put(port->mux);
 	kfree(port->cap);
 	kfree(port);
@@ -1816,6 +1817,13 @@  int typec_set_orientation(struct typec_port *port,
 	if (ret)
 		return ret;
 
+	if (!port->sw) {
+		ret = typec_mux_control_switch_set(port->mux_control_switch,
+				port->mux_control_switch_states[orientation]);
+		if (ret)
+			return ret;
+	}
+
 	port->orientation = orientation;
 	sysfs_notify(&port->dev.kobj, NULL, "orientation");
 	kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
@@ -1991,7 +1999,7 @@  struct typec_port *typec_register_port(struct device *parent,
 				       const struct typec_capability *cap)
 {
 	struct typec_port *port;
-	int ret;
+	int ret = 0;
 	int id;
 
 	port = kzalloc(sizeof(*port), GFP_KERNEL);
@@ -2068,6 +2076,22 @@  struct typec_port *typec_register_port(struct device *parent,
 		return ERR_PTR(ret);
 	}
 
+	if (!port->sw) {
+		/* Try to get typec switch via general mux controller */
+		port->mux_control_switch = typec_mux_control_switch_get(&port->dev);
+		if (IS_ERR(port->mux_control_switch))
+			ret = PTR_ERR(port->mux_control_switch);
+		else if (port->mux_control_switch)
+			ret = device_property_read_u32_array(&port->dev,
+					"mux-control-switch-states",
+					port->mux_control_switch_states,
+					3);
+		if (ret) {
+			put_device(&port->dev);
+			return ERR_PTR(ret);
+		}
+	}
+
 	port->mux = typec_mux_get(&port->dev, NULL);
 	if (IS_ERR(port->mux)) {
 		ret = PTR_ERR(port->mux);
diff --git a/drivers/usb/typec/class.h b/drivers/usb/typec/class.h
index aef03eb7e152..15dad2621c83 100644
--- a/drivers/usb/typec/class.h
+++ b/drivers/usb/typec/class.h
@@ -50,6 +50,8 @@  struct typec_port {
 
 	enum typec_orientation		orientation;
 	struct typec_switch		*sw;
+	struct mux_control		*mux_control_switch;
+	int				mux_control_switch_states[3];
 	struct typec_mux		*mux;
 
 	const struct typec_capability	*cap;
diff --git a/drivers/usb/typec/mux.c b/drivers/usb/typec/mux.c
index 9da22ae3006c..6c5c4f07286d 100644
--- a/drivers/usb/typec/mux.c
+++ b/drivers/usb/typec/mux.c
@@ -11,6 +11,7 @@ 
 #include <linux/list.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
+#include <linux/mux/consumer.h>
 #include <linux/property.h>
 #include <linux/slab.h>
 
@@ -176,6 +177,39 @@  void *typec_switch_get_drvdata(struct typec_switch *sw)
 }
 EXPORT_SYMBOL_GPL(typec_switch_get_drvdata);
 
+struct mux_control *typec_mux_control_switch_get(struct device *dev)
+{
+	if (!device_property_present(dev, "mux-control-names"))
+		return NULL;
+
+	return mux_control_get(dev, "typec-orientation-switch");
+}
+EXPORT_SYMBOL_GPL(typec_mux_control_switch_get);
+
+int typec_mux_control_switch_set(struct mux_control *mc_sw, int state)
+{
+	int ret;
+
+	if (!mc_sw)
+		return 0;
+
+	ret = mux_control_deselect(mc_sw);
+	if (ret)
+		return ret;
+
+	return mux_control_select(mc_sw, state);
+}
+EXPORT_SYMBOL_GPL(typec_mux_control_switch_set);
+
+void typec_mux_control_switch_put(struct mux_control *mc_sw)
+{
+	if (!mc_sw)
+		return;
+
+	return mux_control_put(mc_sw);
+}
+EXPORT_SYMBOL_GPL(typec_mux_control_switch_put);
+
 /* ------------------------------------------------------------------------- */
 
 static int mux_fwnode_match(struct device *dev, const void *fwnode)
diff --git a/include/linux/usb/typec_mux.h b/include/linux/usb/typec_mux.h
index a9d9957933dc..e0933e205b80 100644
--- a/include/linux/usb/typec_mux.h
+++ b/include/linux/usb/typec_mux.h
@@ -27,6 +27,10 @@  void typec_switch_put(struct typec_switch *sw);
 int typec_switch_set(struct typec_switch *sw,
 		     enum typec_orientation orientation);
 
+struct mux_control *typec_mux_control_switch_get(struct device *dev);
+int typec_mux_control_switch_set(struct mux_control *mc_sw, int state);
+void typec_mux_control_switch_put(struct mux_control *mc_sw);
+
 static inline struct typec_switch *typec_switch_get(struct device *dev)
 {
 	return fwnode_typec_switch_get(dev_fwnode(dev));