diff mbox series

[1/4] spi: xcomm: add gpiochip support

Message ID 20240704-dev-spi-xcomm-gpiochip-v1-1-653db6fbef36@analog.com (mailing list archive)
State New
Headers show
Series spi: xcomm: support GPO pin | expand

Commit Message

Nuno Sa July 4, 2024, 1:49 p.m. UTC
From: Michael Hennerich <michael.hennerich@analog.com>

The hardware can expose one pin as a GPO. Hence, register a simple
gpiochip to support it.

Signed-off-by: Michael Hennerich <michael.hennerich@analog.com>
Co-developed-by: Nuno Sa <nuno.sa@analog.com>
Signed-off-by: Nuno Sa <nuno.sa@analog.com>
---
 drivers/spi/spi-xcomm.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 54 insertions(+), 1 deletion(-)

Comments

Mark Brown July 4, 2024, 1:53 p.m. UTC | #1
On Thu, Jul 04, 2024 at 03:49:12PM +0200, Nuno Sa wrote:

> +static int spi_xcomm_gpio_get_value(struct gpio_chip *chip, unsigned int offset)
> +{
> +	struct spi_xcomm *spi_xcomm = gpiochip_get_data(chip);
> +
> +	return spi_xcomm->gpio_val;
> +}

It seems like the hardware doesn't support input at all so should there
even be a get operation?  gpiolib appears to cope fine with omitting it.
Nuno Sá July 4, 2024, 2 p.m. UTC | #2
On Thu, 2024-07-04 at 14:53 +0100, Mark Brown wrote:
> On Thu, Jul 04, 2024 at 03:49:12PM +0200, Nuno Sa wrote:
> 
> > +static int spi_xcomm_gpio_get_value(struct gpio_chip *chip, unsigned int
> > offset)
> > +{
> > +	struct spi_xcomm *spi_xcomm = gpiochip_get_data(chip);
> > +
> > +	return spi_xcomm->gpio_val;
> > +}
> 
> It seems like the hardware doesn't support input at all so should there
> even be a get operation?  gpiolib appears to cope fine with omitting it.

Just following recommendations :)

https://elixir.bootlin.com/linux/v6.10-rc6/source/include/linux/gpio/driver.h#L336

- Nuno Sá
Mark Brown July 4, 2024, 3:45 p.m. UTC | #3
On Thu, Jul 04, 2024 at 04:00:15PM +0200, Nuno Sá wrote:
> On Thu, 2024-07-04 at 14:53 +0100, Mark Brown wrote:
> > On Thu, Jul 04, 2024 at 03:49:12PM +0200, Nuno Sa wrote:
> > 
> > > +static int spi_xcomm_gpio_get_value(struct gpio_chip *chip, unsigned int
> > > offset)
> > > +{
> > > +	struct spi_xcomm *spi_xcomm = gpiochip_get_data(chip);
> > > +
> > > +	return spi_xcomm->gpio_val;
> > > +}

> > It seems like the hardware doesn't support input at all so should there
> > even be a get operation?  gpiolib appears to cope fine with omitting it.

> Just following recommendations :)

> https://elixir.bootlin.com/linux/v6.10-rc6/source/include/linux/gpio/driver.h#L336

That comment is for get_direction(), not get().
Nuno Sá July 5, 2024, 9:13 a.m. UTC | #4
On Thu, 2024-07-04 at 16:45 +0100, Mark Brown wrote:
> On Thu, Jul 04, 2024 at 04:00:15PM +0200, Nuno Sá wrote:
> > On Thu, 2024-07-04 at 14:53 +0100, Mark Brown wrote:
> > > On Thu, Jul 04, 2024 at 03:49:12PM +0200, Nuno Sa wrote:
> > > 
> > > > +static int spi_xcomm_gpio_get_value(struct gpio_chip *chip, unsigned int
> > > > offset)
> > > > +{
> > > > +	struct spi_xcomm *spi_xcomm = gpiochip_get_data(chip);
> > > > +
> > > > +	return spi_xcomm->gpio_val;
> > > > +}
> 
> > > It seems like the hardware doesn't support input at all so should there
> > > even be a get operation?  gpiolib appears to cope fine with omitting it.
> 
> > Just following recommendations :)
> 
> > https://elixir.bootlin.com/linux/v6.10-rc6/source/include/linux/gpio/driver.h#L336
> 
> That comment is for get_direction(), not get().

Oh right, sorry. For some reason I assumed get_direction()... Well, get() was mainly
to not get an error when reading the GPIO value from userspace (eg:
/sys/clagg/gpio). 

That said, we're just caching the value in the driver in case the i2c transfer does
not fail so I guess yes, we can make this even simpler and remove get() and
'gpio_val'. Userspace apps can very well cache the value themselves.

- Nuno Sá
diff mbox series

Patch

diff --git a/drivers/spi/spi-xcomm.c b/drivers/spi/spi-xcomm.c
index 63354dd3110f..358e0b84ee60 100644
--- a/drivers/spi/spi-xcomm.c
+++ b/drivers/spi/spi-xcomm.c
@@ -10,6 +10,7 @@ 
 #include <linux/module.h>
 #include <linux/delay.h>
 #include <linux/i2c.h>
+#include <linux/gpio/driver.h>
 #include <linux/spi/spi.h>
 #include <asm/unaligned.h>
 
@@ -26,12 +27,16 @@ 
 
 #define SPI_XCOMM_CMD_UPDATE_CONFIG	0x03
 #define SPI_XCOMM_CMD_WRITE		0x04
+#define SPI_XCOMM_CMD_GPIO_SET		0x05
 
 #define SPI_XCOMM_CLOCK 48000000
 
 struct spi_xcomm {
 	struct i2c_client *i2c;
 
+	struct gpio_chip gc;
+	int gpio_val;
+
 	uint16_t settings;
 	uint16_t chipselect;
 
@@ -40,6 +45,54 @@  struct spi_xcomm {
 	uint8_t buf[63];
 };
 
+static void spi_xcomm_gpio_set_value(struct gpio_chip *chip,
+				     unsigned int offset, int val)
+{
+	struct spi_xcomm *spi_xcomm = gpiochip_get_data(chip);
+	unsigned char buf[2];
+	int ret;
+
+	buf[0] = SPI_XCOMM_CMD_GPIO_SET;
+	buf[1] = !!val;
+	ret = i2c_master_send(spi_xcomm->i2c, buf, 2);
+	if (ret < 0)
+		return;
+
+	spi_xcomm->gpio_val = !!val;
+}
+
+static int spi_xcomm_gpio_get_value(struct gpio_chip *chip, unsigned int offset)
+{
+	struct spi_xcomm *spi_xcomm = gpiochip_get_data(chip);
+
+	return spi_xcomm->gpio_val;
+}
+
+static int spi_xcomm_gpio_get_direction(struct gpio_chip *chip,
+					unsigned int offset)
+{
+	return GPIO_LINE_DIRECTION_OUT;
+}
+
+static int spi_xcomm_gpio_add(struct spi_xcomm *spi_xcomm)
+{
+	struct device *dev = &spi_xcomm->i2c->dev;
+
+	if (!IS_ENABLED(CONFIG_GPIOLIB))
+		return 0;
+
+	spi_xcomm->gc.get_direction = spi_xcomm_gpio_get_direction;
+	spi_xcomm->gc.get = spi_xcomm_gpio_get_value;
+	spi_xcomm->gc.set = spi_xcomm_gpio_set_value;
+	spi_xcomm->gc.can_sleep = 1;
+	spi_xcomm->gc.base = -1;
+	spi_xcomm->gc.ngpio = 1;
+	spi_xcomm->gc.label = spi_xcomm->i2c->name;
+	spi_xcomm->gc.owner = THIS_MODULE;
+
+	return devm_gpiochip_add_data(dev, &spi_xcomm->gc, spi_xcomm);
+}
+
 static int spi_xcomm_sync_config(struct spi_xcomm *spi_xcomm, unsigned int len)
 {
 	uint16_t settings;
@@ -227,7 +280,7 @@  static int spi_xcomm_probe(struct i2c_client *i2c)
 	if (ret < 0)
 		spi_controller_put(host);
 
-	return ret;
+	return spi_xcomm_gpio_add(spi_xcomm);
 }
 
 static const struct i2c_device_id spi_xcomm_ids[] = {