diff mbox series

spi: sun6i: Add support for GPIO chip select lines

Message ID 20200506014135.2941967-1-alistair@alistair23.me (mailing list archive)
State New, archived
Headers show
Series spi: sun6i: Add support for GPIO chip select lines | expand

Commit Message

Alistair Francis May 6, 2020, 1:41 a.m. UTC
Add a setup function that can be used to support using generic GPIO
lines for the chip select.

Signed-off-by: Alistair Francis <alistair@alistair23.me>
---
 drivers/spi/spi-sun6i.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

Comments

Geert Uytterhoeven May 6, 2020, 8:27 a.m. UTC | #1
Hi Alistair,

On Wed, May 6, 2020 at 3:41 AM Alistair Francis <alistair@alistair23.me> wrote:
> Add a setup function that can be used to support using generic GPIO
> lines for the chip select.
>
> Signed-off-by: Alistair Francis <alistair@alistair23.me>
> ---
>  drivers/spi/spi-sun6i.c | 27 +++++++++++++++++++++++++++
>  1 file changed, 27 insertions(+)
>
> diff --git a/drivers/spi/spi-sun6i.c b/drivers/spi/spi-sun6i.c
> index ec7967be9e2f..fd4e19434942 100644
> --- a/drivers/spi/spi-sun6i.c
> +++ b/drivers/spi/spi-sun6i.c
> @@ -10,6 +10,7 @@
>  #include <linux/clk.h>
>  #include <linux/delay.h>
>  #include <linux/device.h>
> +#include <linux/gpio.h>
>  #include <linux/interrupt.h>
>  #include <linux/io.h>
>  #include <linux/module.h>
> @@ -171,6 +172,31 @@ static inline void sun6i_spi_fill_fifo(struct sun6i_spi *sspi, int len)
>         }
>  }
>
> +static int sun6i_spi_setup(struct spi_device *spi)
> +{
> +       int ret;
> +
> +       /* sanity check for native cs */
> +       if (spi->mode & SPI_NO_CS)
> +               return 0;
> +       if (gpio_is_valid(spi->cs_gpio)) {
> +               /* with gpio-cs set the GPIO to the correct level
> +                * and as output (in case the dt has the gpio not configured
> +                * as output but native cs)
> +                */
> +               ret = gpio_direction_output(spi->cs_gpio,
> +                                           (spi->mode & SPI_CS_HIGH) ? 0 : 1);
> +               if (ret)
> +                       dev_err(&spi->dev,
> +                               "could not set gpio %i as output: %i\n",
> +                               spi->cs_gpio, ret);
> +
> +               return ret;
> +       }
> +
> +       return 0;
> +}
> +
>  static void sun6i_spi_set_cs(struct spi_device *spi, bool enable)
>  {
>         struct sun6i_spi *sspi = spi_master_get_devdata(spi->master);
> @@ -470,6 +496,7 @@ static int sun6i_spi_probe(struct platform_device *pdev)
>
>         master->max_speed_hz = 100 * 1000 * 1000;
>         master->min_speed_hz = 3 * 1000;
> +       master->setup = sun6i_spi_setup;
>         master->set_cs = sun6i_spi_set_cs;
>         master->transfer_one = sun6i_spi_transfer_one;
>         master->num_chipselect = 4;

Can't you just set

    master->use_gpio_descriptors = true;

instead and be done with it?
Then drivers/spi/spi.c:spi_get_gpio_descs() will configure the GPIO line
as output for you.

Gr{oetje,eeting}s,

                        Geert
Alistair Francis May 11, 2020, 4:52 a.m. UTC | #2
On Wed, May 6, 2020, at 1:27 AM, Geert Uytterhoeven wrote:
> Hi Alistair,
> 
> On Wed, May 6, 2020 at 3:41 AM Alistair Francis <alistair@alistair23.me> wrote:
> > Add a setup function that can be used to support using generic GPIO
> > lines for the chip select.
> >
> > Signed-off-by: Alistair Francis <alistair@alistair23.me>
> > ---
> > drivers/spi/spi-sun6i.c | 27 +++++++++++++++++++++++++++
> > 1 file changed, 27 insertions(+)
> >
> > diff --git a/drivers/spi/spi-sun6i.c b/drivers/spi/spi-sun6i.c
> > index ec7967be9e2f..fd4e19434942 100644
> > --- a/drivers/spi/spi-sun6i.c
> > +++ b/drivers/spi/spi-sun6i.c
> > @@ -10,6 +10,7 @@
> > #include <linux/clk.h>
> > #include <linux/delay.h>
> > #include <linux/device.h>
> > +#include <linux/gpio.h>
> > #include <linux/interrupt.h>
> > #include <linux/io.h>
> > #include <linux/module.h>
> > @@ -171,6 +172,31 @@ static inline void sun6i_spi_fill_fifo(struct sun6i_spi *sspi, int len)
> > }
> > }
> >
> > +static int sun6i_spi_setup(struct spi_device *spi)
> > +{
> > + int ret;
> > +
> > + /* sanity check for native cs */
> > + if (spi->mode & SPI_NO_CS)
> > + return 0;
> > + if (gpio_is_valid(spi->cs_gpio)) {
> > + /* with gpio-cs set the GPIO to the correct level
> > + * and as output (in case the dt has the gpio not configured
> > + * as output but native cs)
> > + */
> > + ret = gpio_direction_output(spi->cs_gpio,
> > + (spi->mode & SPI_CS_HIGH) ? 0 : 1);
> > + if (ret)
> > + dev_err(&spi->dev,
> > + "could not set gpio %i as output: %i\n",
> > + spi->cs_gpio, ret);
> > +
> > + return ret;
> > + }
> > +
> > + return 0;
> > +}
> > +
> > static void sun6i_spi_set_cs(struct spi_device *spi, bool enable)
> > {
> > struct sun6i_spi *sspi = spi_master_get_devdata(spi->master);
> > @@ -470,6 +496,7 @@ static int sun6i_spi_probe(struct platform_device *pdev)
> >
> > master->max_speed_hz = 100 * 1000 * 1000;
> > master->min_speed_hz = 3 * 1000;
> > + master->setup = sun6i_spi_setup;
> > master->set_cs = sun6i_spi_set_cs;
> > master->transfer_one = sun6i_spi_transfer_one;
> > master->num_chipselect = 4;
> 
> Can't you just set
> 
>  master->use_gpio_descriptors = true;
> 
> instead and be done with it?
> Then drivers/spi/spi.c:spi_get_gpio_descs() will configure the GPIO line
> as output for you.

Yep, it looks like that works. Sending a v2.

Alistair

> 
> Gr{oetje,eeting}s,
> 
>  Geert
> 
> -- 
> Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org
> 
> In personal conversations with technical people, I call myself a hacker. But
> when I'm talking to journalists I just say "programmer" or something like that.
>  -- Linus Torvalds
>
diff mbox series

Patch

diff --git a/drivers/spi/spi-sun6i.c b/drivers/spi/spi-sun6i.c
index ec7967be9e2f..fd4e19434942 100644
--- a/drivers/spi/spi-sun6i.c
+++ b/drivers/spi/spi-sun6i.c
@@ -10,6 +10,7 @@ 
 #include <linux/clk.h>
 #include <linux/delay.h>
 #include <linux/device.h>
+#include <linux/gpio.h>
 #include <linux/interrupt.h>
 #include <linux/io.h>
 #include <linux/module.h>
@@ -171,6 +172,31 @@  static inline void sun6i_spi_fill_fifo(struct sun6i_spi *sspi, int len)
 	}
 }
 
+static int sun6i_spi_setup(struct spi_device *spi)
+{
+	int ret;
+
+	/* sanity check for native cs */
+	if (spi->mode & SPI_NO_CS)
+		return 0;
+	if (gpio_is_valid(spi->cs_gpio)) {
+		/* with gpio-cs set the GPIO to the correct level
+		 * and as output (in case the dt has the gpio not configured
+		 * as output but native cs)
+		 */
+		ret = gpio_direction_output(spi->cs_gpio,
+					    (spi->mode & SPI_CS_HIGH) ? 0 : 1);
+		if (ret)
+			dev_err(&spi->dev,
+				"could not set gpio %i as output: %i\n",
+				spi->cs_gpio, ret);
+
+		return ret;
+	}
+
+	return 0;
+}
+
 static void sun6i_spi_set_cs(struct spi_device *spi, bool enable)
 {
 	struct sun6i_spi *sspi = spi_master_get_devdata(spi->master);
@@ -470,6 +496,7 @@  static int sun6i_spi_probe(struct platform_device *pdev)
 
 	master->max_speed_hz = 100 * 1000 * 1000;
 	master->min_speed_hz = 3 * 1000;
+	master->setup = sun6i_spi_setup;
 	master->set_cs = sun6i_spi_set_cs;
 	master->transfer_one = sun6i_spi_transfer_one;
 	master->num_chipselect = 4;