[01/11] ASoC: ak5386: switch to using gpiod API

Message ID 20221116053817.2929810-1-dmitry.torokhov@gmail.com
State New
Headers
Series [01/11] ASoC: ak5386: switch to using gpiod API |

Commit Message

Dmitry Torokhov Nov. 16, 2022, 5:38 a.m. UTC
  Switch the driver from legacy gpio API that is deprecated to the newer
gpiod API that respects line polarities described in ACPI/DT.

Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
 sound/soc/codecs/ak5386.c | 30 ++++++++++++++----------------
 1 file changed, 14 insertions(+), 16 deletions(-)
  

Comments

Mark Brown Nov. 16, 2022, 10:36 a.m. UTC | #1
On Tue, Nov 15, 2022 at 09:38:07PM -0800, Dmitry Torokhov wrote:

> Switch the driver from legacy gpio API that is deprecated to the newer
> gpiod API that respects line polarities described in ACPI/DT.

> -	if (gpio_is_valid(priv->reset_gpio))
> -		gpio_set_value(priv->reset_gpio, 1);
> +	if (priv->reset_gpio)
> +		gpiod_set_value(priv->reset_gpio, 0);

How are we ensuring that people have described signals as active
low/high in existing DTs, and are we positive that the signal is
described as active low for all devices?  In particular if the
signal is described as a reset signal then it's active high even
if we want it low while the device is actually in use.
  
Dmitry Torokhov Nov. 16, 2022, 7:07 p.m. UTC | #2
On Wed, Nov 16, 2022 at 10:36:27AM +0000, Mark Brown wrote:
> On Tue, Nov 15, 2022 at 09:38:07PM -0800, Dmitry Torokhov wrote:
> 
> > Switch the driver from legacy gpio API that is deprecated to the newer
> > gpiod API that respects line polarities described in ACPI/DT.
> 
> > -	if (gpio_is_valid(priv->reset_gpio))
> > -		gpio_set_value(priv->reset_gpio, 1);
> > +	if (priv->reset_gpio)
> > +		gpiod_set_value(priv->reset_gpio, 0);
> 
> How are we ensuring that people have described signals as active
> low/high in existing DTs, and are we positive that the signal is
> described as active low for all devices?  In particular if the
> signal is described as a reset signal then it's active high even
> if we want it low while the device is actually in use.

I have been going through in-kernel DTSes and adjusting ones that are
incorrect. For external ones I think we should take a pragmatic approach
and say that if driver has last non-mechanical update in 2014 and there
are no users submitted to mainline since then (as this one), then it is
highly unlikely that devices currently using this component/codec will
be updated to the 6.2+ kernel even if they are still in service. And if
this does happen the breakage will be immediately obvious as we'll keep
the codec in reset state.

But if you really want to I can add quirk(s) to gpiolib forcing this
line to be treated as active-low regardless of what specified in DTS.
This kind of negates benefit of going to gpiod though.

Please let me know.
  
Mark Brown Nov. 17, 2022, 11:34 a.m. UTC | #3
On Wed, Nov 16, 2022 at 11:07:48AM -0800, Dmitry Torokhov wrote:
> On Wed, Nov 16, 2022 at 10:36:27AM +0000, Mark Brown wrote:

> > How are we ensuring that people have described signals as active
> > low/high in existing DTs, and are we positive that the signal is
> > described as active low for all devices?  In particular if the
> > signal is described as a reset signal then it's active high even
> > if we want it low while the device is actually in use.

> I have been going through in-kernel DTSes and adjusting ones that are
> incorrect. For external ones I think we should take a pragmatic approach
> and say that if driver has last non-mechanical update in 2014 and there
> are no users submitted to mainline since then (as this one), then it is
> highly unlikely that devices currently using this component/codec will
> be updated to the 6.2+ kernel even if they are still in service. And if
> this does happen the breakage will be immediately obvious as we'll keep
> the codec in reset state.

> But if you really want to I can add quirk(s) to gpiolib forcing this
> line to be treated as active-low regardless of what specified in DTS.
> This kind of negates benefit of going to gpiod though.

That doesn't address the bit about checking that the device
describes the signal as active low in hardware - it's assuming
that the signal is described by the device as an active low
reset and not for example as a shutdown signal.

TBH I'm not thrilled about just randomly breaking ABI
compatibility for neatness reasons, it's really not helping
people take device tree ABI compatibility seriously.
  
Dmitry Torokhov Nov. 18, 2022, 6:31 a.m. UTC | #4
On Thu, Nov 17, 2022 at 11:34:06AM +0000, Mark Brown wrote:
> On Wed, Nov 16, 2022 at 11:07:48AM -0800, Dmitry Torokhov wrote:
> > On Wed, Nov 16, 2022 at 10:36:27AM +0000, Mark Brown wrote:
> 
> > > How are we ensuring that people have described signals as active
> > > low/high in existing DTs, and are we positive that the signal is
> > > described as active low for all devices?  In particular if the
> > > signal is described as a reset signal then it's active high even
> > > if we want it low while the device is actually in use.
> 
> > I have been going through in-kernel DTSes and adjusting ones that are
> > incorrect. For external ones I think we should take a pragmatic approach
> > and say that if driver has last non-mechanical update in 2014 and there
> > are no users submitted to mainline since then (as this one), then it is
> > highly unlikely that devices currently using this component/codec will
> > be updated to the 6.2+ kernel even if they are still in service. And if
> > this does happen the breakage will be immediately obvious as we'll keep
> > the codec in reset state.
> 
> > But if you really want to I can add quirk(s) to gpiolib forcing this
> > line to be treated as active-low regardless of what specified in DTS.
> > This kind of negates benefit of going to gpiod though.
> 
> That doesn't address the bit about checking that the device
> describes the signal as active low in hardware - it's assuming
> that the signal is described by the device as an active low
> reset and not for example as a shutdown signal.

Huh? If we add a quirk to gpiolib to treat the signal as active low
(i.e. preserve current driver behavior - I am talking about this
particular peripheral here, not treating everything as active low of
course).

> 
> TBH I'm not thrilled about just randomly breaking ABI
> compatibility for neatness reasons, it's really not helping
> people take device tree ABI compatibility seriously.

Yes, I freely admit I do not take device tree ABI compatibility
seriously. IMO, with the exception of a few peripherals, it is a
solution in search of a problem, and we declared stability of it too
early, before we came up with reasonable rules for how resources should
be described. I strongly believe that in vast majority of cases devices
with out-of-tree DTs will not be updated to upstream kernels as this
requires significant engineering effort and vendors usually not
interested in doing that.

Thanks.
  
Mark Brown Nov. 18, 2022, 1:12 p.m. UTC | #5
On Thu, Nov 17, 2022 at 10:31:49PM -0800, Dmitry Torokhov wrote:
> On Thu, Nov 17, 2022 at 11:34:06AM +0000, Mark Brown wrote:

> > That doesn't address the bit about checking that the device
> > describes the signal as active low in hardware - it's assuming
> > that the signal is described by the device as an active low
> > reset and not for example as a shutdown signal.

> Huh? If we add a quirk to gpiolib to treat the signal as active low
> (i.e. preserve current driver behavior - I am talking about this
> particular peripheral here, not treating everything as active low of
> course).

My comments were more generic ones about the whole series since
all the patches seemed to be doing the same thing with flipping
the polarity - some of the GPIOs were labelled as things like
reset which is active high if it's not nRESET or something even
though we want to pull it low while using the device.

> > TBH I'm not thrilled about just randomly breaking ABI
> > compatibility for neatness reasons, it's really not helping
> > people take device tree ABI compatibility seriously.

> Yes, I freely admit I do not take device tree ABI compatibility
> seriously. IMO, with the exception of a few peripherals, it is a
> solution in search of a problem, and we declared stability of it too
> early, before we came up with reasonable rules for how resources should
> be described. I strongly believe that in vast majority of cases devices
> with out-of-tree DTs will not be updated to upstream kernels as this
> requires significant engineering effort and vendors usually not
> interested in doing that.

There are practical systems which ship DTs as part of the
firmware, and frankly things like this do contribute to the
issue.  The systems that just ship their DTs are obviously a lot
less visible, but that's the whole goal here.  It's most common
with more server type systems using EDK2 for the firmware, ACPI
isn't always a good fit for them.
  

Patch

diff --git a/sound/soc/codecs/ak5386.c b/sound/soc/codecs/ak5386.c
index 0c5e00679c7d..0562890b5dc7 100644
--- a/sound/soc/codecs/ak5386.c
+++ b/sound/soc/codecs/ak5386.c
@@ -6,11 +6,12 @@ 
  * (c) 2013 Daniel Mack <zonque@gmail.com>
  */
 
+
+#include <linux/err.h>
+#include <linux/gpio/consumer.h>
 #include <linux/module.h>
 #include <linux/slab.h>
 #include <linux/of.h>
-#include <linux/of_gpio.h>
-#include <linux/of_device.h>
 #include <linux/regulator/consumer.h>
 #include <sound/soc.h>
 #include <sound/pcm.h>
@@ -21,7 +22,7 @@  static const char * const supply_names[] = {
 };
 
 struct ak5386_priv {
-	int reset_gpio;
+	struct gpio_desc *reset_gpio;
 	struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)];
 };
 
@@ -111,8 +112,8 @@  static int ak5386_hw_params(struct snd_pcm_substream *substream,
 	 * the AK5386 in power-down mode (PDN pin = ā€œLā€).
 	 */
 
-	if (gpio_is_valid(priv->reset_gpio))
-		gpio_set_value(priv->reset_gpio, 1);
+	if (priv->reset_gpio)
+		gpiod_set_value(priv->reset_gpio, 0);
 
 	return 0;
 }
@@ -123,8 +124,8 @@  static int ak5386_hw_free(struct snd_pcm_substream *substream,
 	struct snd_soc_component *component = dai->component;
 	struct ak5386_priv *priv = snd_soc_component_get_drvdata(component);
 
-	if (gpio_is_valid(priv->reset_gpio))
-		gpio_set_value(priv->reset_gpio, 0);
+	if (priv->reset_gpio)
+		gpiod_set_value(priv->reset_gpio, 1);
 
 	return 0;
 }
@@ -168,7 +169,6 @@  static int ak5386_probe(struct platform_device *pdev)
 	if (!priv)
 		return -ENOMEM;
 
-	priv->reset_gpio = -EINVAL;
 	dev_set_drvdata(dev, priv);
 
 	for (i = 0; i < ARRAY_SIZE(supply_names); i++)
@@ -179,15 +179,13 @@  static int ak5386_probe(struct platform_device *pdev)
 	if (ret < 0)
 		return ret;
 
-	if (of_match_device(of_match_ptr(ak5386_dt_ids), dev))
-		priv->reset_gpio = of_get_named_gpio(dev->of_node,
-						      "reset-gpio", 0);
+	priv->reset_gpio = devm_gpiod_get_optional(dev, "reset",
+						   GPIOD_OUT_HIGH);
+	ret = PTR_ERR_OR_ZERO(priv->reset_gpio);
+	if (ret)
+		return ret;
 
-	if (gpio_is_valid(priv->reset_gpio))
-		if (devm_gpio_request_one(dev, priv->reset_gpio,
-					  GPIOF_OUT_INIT_LOW,
-					  "AK5386 Reset"))
-			priv->reset_gpio = -EINVAL;
+	gpiod_set_consumer_name(priv->reset_gpio, "AK5386 Reset");
 
 	return devm_snd_soc_register_component(dev, &soc_component_ak5386,
 				      &ak5386_dai, 1);