public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: David Lechner <dlechner@baylibre.com>
To: Jonathan Santos <Jonathan.Santos@analog.com>,
	linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org
Cc: Sergiu Cuciurean <sergiu.cuciurean@analog.com>,
	lars@metafoo.de, Michael.Hennerich@analog.com, jic23@kernel.org,
	robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org,
	marcelo.schmitt1@gmail.com
Subject: Re: [PATCH v1 12/15] iio: adc: ad7768-1: Add GPIO controller support
Date: Tue, 7 Jan 2025 17:48:04 -0600	[thread overview]
Message-ID: <c5890aee-78ac-45a7-b910-72a342f329b1@baylibre.com> (raw)
In-Reply-To: <e09454c98accd763d6d5e67f9f7262aabec91acd.1736201898.git.Jonathan.Santos@analog.com>

On 1/7/25 9:26 AM, Jonathan Santos wrote:
> From: Sergiu Cuciurean <sergiu.cuciurean@analog.com>
> 
> The AD7768-1 has the ability to control other local hardware (such as gain
> stages),to power down other blocks in the signal chain, or read local
> status signals over the SPI interface.
> 
> This change exports the AD7768-1's four gpios and makes them accessible
> at an upper layer.

We will also need a dt-bindings patch to add gpio-controller and #gpio-cells
properties.

> 
> Signed-off-by: Sergiu Cuciurean <sergiu.cuciurean@analog.com>
> ---
>  drivers/iio/adc/ad7768-1.c | 121 +++++++++++++++++++++++++++++++++++++
>  1 file changed, 121 insertions(+)
> 
> diff --git a/drivers/iio/adc/ad7768-1.c b/drivers/iio/adc/ad7768-1.c
> index 675af9ea856d..9741a6d47942 100644
> --- a/drivers/iio/adc/ad7768-1.c
> +++ b/drivers/iio/adc/ad7768-1.c
> @@ -10,6 +10,8 @@
>  #include <linux/delay.h>
>  #include <linux/device.h>
>  #include <linux/err.h>
> +#include <linux/gpio.h>
> +#include <linux/gpio/driver.h>
>  #include <linux/gpio/consumer.h>
>  #include <linux/kernel.h>
>  #include <linux/module.h>
> @@ -77,6 +79,19 @@
>  #define AD7768_CONV_MODE_MSK		GENMASK(2, 0)
>  #define AD7768_CONV_MODE(x)		FIELD_PREP(AD7768_CONV_MODE_MSK, x)
>  
> +/* AD7768_REG_GPIO_CONTROL */
> +#define AD7768_GPIO_CONTROL_MSK		GENMASK(3, 0)
> +#define AD7768_GPIO_UNIVERSAL_EN	BIT(7)

Bits should be ordered highest to lowest (from top to bottom) to be consistent
with other code.

> +
> +/* AD7768_REG_GPIO_WRITE */
> +#define AD7768_GPIO_WRITE_MSK		GENMASK(3, 0)
> +
> +/* AD7768_REG_GPIO_READ */
> +#define AD7768_GPIO_READ_MSK		GENMASK(3, 0)
> +
> +#define AD7768_GPIO_INPUT(x)		0x00
> +#define AD7768_GPIO_OUTPUT(x)		BIT(x)
> +
>  #define AD7768_RD_FLAG_MSK(x)		(BIT(6) | ((x) & 0x3F))
>  #define AD7768_WR_FLAG_MSK(x)		((x) & 0x3F)
>  
> @@ -190,6 +205,8 @@ struct ad7768_state {
>  	struct regulator *vref;
>  	struct mutex lock;
>  	struct clk *mclk;
> +	struct gpio_chip gpiochip;
> +	unsigned int gpio_avail_map;
>  	unsigned int mclk_freq;
>  	unsigned int samp_freq;
>  	unsigned int common_mode_voltage;
> @@ -338,6 +355,106 @@ static int ad7768_set_dig_fil(struct ad7768_state *st,
>  	return 0;
>  }
>  
> +static int ad7768_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
> +{
> +	struct ad7768_state *st = gpiochip_get_data(chip);
> +
> +	guard(mutex)(&st->lock);

As mentioned in another patch, this should be iio_device_claim_direct_mode()
instead of st->lock since we can't access registers during a buffered read
because the chip is in conversion mode. Same applies to other functions below.

> +	return ad7768_spi_reg_write_masked(st,
> +					  AD7768_REG_GPIO_CONTROL,
> +					  BIT(offset),
> +					  AD7768_GPIO_INPUT(offset));
> +}
> +
> +static int ad7768_gpio_direction_output(struct gpio_chip *chip,
> +					unsigned int offset, int value)
> +{
> +	struct ad7768_state *st = gpiochip_get_data(chip);
> +
> +	guard(mutex)(&st->lock);
> +	return ad7768_spi_reg_write_masked(st,
> +					  AD7768_REG_GPIO_CONTROL,
> +					  BIT(offset),
> +					  AD7768_GPIO_OUTPUT(offset));
> +}
> +
> +static int ad7768_gpio_get(struct gpio_chip *chip, unsigned int offset)
> +{
> +	struct ad7768_state *st = gpiochip_get_data(chip);
> +	unsigned int val;
> +	int ret;
> +
> +	guard(mutex)(&st->lock);
> +	ret = ad7768_spi_reg_read(st, AD7768_REG_GPIO_CONTROL, &val, 1);
> +	if (ret < 0)
> +		return ret;
> +
> +	if (val & BIT(offset))
> +		ret = ad7768_spi_reg_read(st, AD7768_REG_GPIO_WRITE, &val, 1);
> +	else
> +		ret = ad7768_spi_reg_read(st, AD7768_REG_GPIO_READ, &val, 1);

It doesn't work to just always read AD7768_REG_GPIO_READ?

> +	if (ret < 0)
> +		return ret;
> +
> +	return !!(val & BIT(offset));
> +}
> +
> +static void ad7768_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
> +{
> +	struct ad7768_state *st = gpiochip_get_data(chip);
> +	unsigned int val;
> +	int ret;
> +
> +	guard(mutex)(&st->lock);
> +	ret = ad7768_spi_reg_read(st, AD7768_REG_GPIO_CONTROL, &val, 1);
> +	if (ret < 0)
> +		return;
> +
> +	if (val & BIT(offset))
> +		ad7768_spi_reg_write_masked(st,
> +					    AD7768_REG_GPIO_WRITE,
> +					    BIT(offset),
> +					    (value << offset));
> +}
> +
> +static int ad7768_gpio_request(struct gpio_chip *chip, unsigned int offset)
> +{
> +	struct ad7768_state *st = gpiochip_get_data(chip);
> +
> +	if (!(st->gpio_avail_map & BIT(offset)))
> +		return -ENODEV;
> +
> +	st->gpio_avail_map &= ~BIT(offset);
> +
> +	return 0;
> +}
> +
> +static int ad7768_gpio_init(struct ad7768_state *st)
> +{
> +	int ret;
> +
> +	ret = ad7768_spi_reg_write(st,
> +				   AD7768_REG_GPIO_CONTROL,

nit: this can fit on the previous line

> +				   AD7768_GPIO_UNIVERSAL_EN);
> +	if (ret < 0)
> +		return ret;
> +
> +	st->gpio_avail_map = AD7768_GPIO_CONTROL_MSK;
> +	st->gpiochip.label = "ad7768_1_gpios";
> +	st->gpiochip.base = -1;
> +	st->gpiochip.ngpio = 4;
> +	st->gpiochip.parent = &st->spi->dev;
> +	st->gpiochip.can_sleep = true;
> +	st->gpiochip.direction_input = ad7768_gpio_direction_input;
> +	st->gpiochip.direction_output = ad7768_gpio_direction_output;
> +	st->gpiochip.get = ad7768_gpio_get;
> +	st->gpiochip.set = ad7768_gpio_set;
> +	st->gpiochip.request = ad7768_gpio_request;
> +	st->gpiochip.owner = THIS_MODULE;
> +
> +	return gpiochip_add_data(&st->gpiochip, st);
> +}
> +
>  static int ad7768_set_freq(struct ad7768_state *st,
>  			   unsigned int freq)
>  {
> @@ -538,6 +655,10 @@ static int ad7768_setup(struct ad7768_state *st)
>  	if (IS_ERR(st->gpio_sync_in))
>  		return PTR_ERR(st->gpio_sync_in);
>  

Since the GPIO pins can also be wired up as mode input pins, this should not be
always enabled. We can use the `gpio-controller` DT property as a flag to
conditionally enable GPIO support when it is actually wired up for that purpose.

> +	ret = ad7768_gpio_init(st);
> +	if (ret < 0)
> +		return ret;
> +
>  	/* Set the default sampling frequency to 32000 kSPS */
>  	return ad7768_set_freq(st, 32000);
>  }


  reply	other threads:[~2025-01-07 23:48 UTC|newest]

Thread overview: 77+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-07 15:23 [PATCH v1 00/15] iio: adc: ad7768-1: Add features, improvements, and fixes Jonathan Santos
2025-01-07 15:24 ` [PATCH v1 01/15] dt-bindings: iio: adc: ad7768-1: add synchronization over SPI property Jonathan Santos
2025-01-07 23:35   ` David Lechner
2025-01-11 22:34     ` Jonathan Santos
2025-01-12 12:12       ` Jonathan Cameron
2025-01-14  0:18         ` Jonathan Santos
2025-01-14 16:05           ` David Lechner
2025-01-18 11:58             ` Jonathan Cameron
2025-01-12 17:14       ` David Lechner
2025-01-10 21:51   ` Marcelo Schmitt
2025-01-12 12:05     ` Jonathan Cameron
2025-01-14  0:41       ` Jonathan Santos
2025-01-18 12:00         ` Jonathan Cameron
2025-01-07 15:24 ` [PATCH v1 02/15] Documentation: ABI: add wideband filter type to sysfs-bus-iio Jonathan Santos
2025-01-07 23:38   ` David Lechner
2025-01-11 22:50     ` Jonathan Santos
2025-01-12 12:16       ` Jonathan Cameron
2025-01-14  0:44         ` Jonathan Santos
2025-01-12 17:39       ` David Lechner
2025-01-12 17:41         ` David Lechner
2025-01-07 15:24 ` [PATCH v1 03/15] Documentation: ABI: testing: ad7768-1: Add device specific ABI documentation Jonathan Santos
2025-01-07 23:38   ` David Lechner
2025-01-11 23:22     ` Jonathan Santos
2025-01-12 12:20       ` Jonathan Cameron
2025-01-12 13:10         ` Jonathan Cameron
2025-01-14  1:06           ` Jonathan Santos
2025-01-07 15:25 ` [PATCH v1 04/15] iio: adc: ad7768-1: Fix conversion result sign Jonathan Santos
2025-01-07 23:39   ` David Lechner
2025-01-10 21:52   ` Marcelo Schmitt
2025-01-12  0:00     ` Jonathan Santos
2025-01-12 12:22   ` Jonathan Cameron
2025-01-07 15:25 ` [PATCH v1 05/15] iio: adc: ad7768-1: set MOSI idle state to high Jonathan Santos
2025-01-07 23:40   ` David Lechner
2025-01-10 21:56     ` Marcelo Schmitt
2025-01-12 12:30       ` Jonathan Cameron
2025-01-13 12:19         ` Marcelo Schmitt
2025-01-18 12:09           ` Jonathan Cameron
2025-01-18 13:17             ` Marcelo Schmitt
2025-01-07 15:25 ` [PATCH v1 06/15] iio: adc: ad7768-1: Update reg_read function Jonathan Santos
2025-01-10 21:57   ` Marcelo Schmitt
2025-01-07 15:25 ` [PATCH v1 07/15] iio: adc: ad7768-1: Add reset gpio Jonathan Santos
2025-01-07 23:40   ` David Lechner
2025-01-12 12:35     ` Jonathan Cameron
2025-01-07 15:25 ` [PATCH v1 08/15] iio: adc: ad7768-1: use guard(mutex) to simplify code Jonathan Santos
2025-01-07 23:42   ` David Lechner
2025-01-12  0:26     ` Jonathan Santos
2025-01-07 15:26 ` [PATCH v1 09/15] iio: adc: ad7768-1: Move buffer allocation to a separate function Jonathan Santos
2025-01-10 22:01   ` Marcelo Schmitt
2025-01-12 12:39     ` Jonathan Cameron
2025-01-12 12:40   ` Jonathan Cameron
2025-01-14  1:14     ` Jonathan Santos
2025-01-07 15:26 ` [PATCH v1 10/15] iio: adc: ad7768-1: Add support for variable VCM Jonathan Santos
2025-01-07 23:46   ` David Lechner
2025-01-12  2:37     ` Jonathan Santos
2025-01-12 12:45       ` Jonathan Cameron
2025-01-07 15:26 ` [PATCH v1 11/15] iio: adc: ad7768-1: Add reg_write_masked function Jonathan Santos
2025-01-07 23:46   ` David Lechner
2025-01-10 22:34   ` Marcelo Schmitt
2025-01-12  2:42     ` Jonathan Santos
2025-01-07 15:26 ` [PATCH v1 12/15] iio: adc: ad7768-1: Add GPIO controller support Jonathan Santos
2025-01-07 23:48   ` David Lechner [this message]
2025-01-07 15:26 ` [PATCH v1 13/15] iio: adc: ad7768-1: add multiple scan types to support 16-bits mode Jonathan Santos
2025-01-07 23:49   ` David Lechner
2025-01-12  3:21     ` Jonathan Santos
2025-01-12 12:50       ` Jonathan Cameron
2025-01-12 17:51         ` David Lechner
2025-01-07 15:27 ` [PATCH v1 14/15] iio: adc: ad7768-1: add support for Synchronization over SPI Jonathan Santos
2025-01-07 23:50   ` David Lechner
2025-01-12 12:59     ` Jonathan Cameron
2025-01-14  1:27       ` Jonathan Santos
2025-01-07 15:27 ` [PATCH v1 15/15] iio: adc: ad7768-1: add filter type and decimation rate attributes Jonathan Santos
2025-01-07 23:50   ` David Lechner
2025-01-12 13:04     ` Jonathan Cameron
2025-01-14  1:39       ` Jonathan Santos
2025-01-10 22:32   ` Marcelo Schmitt
2025-01-07 23:33 ` [PATCH v1 00/15] iio: adc: ad7768-1: Add features, improvements, and fixes David Lechner
2025-01-11 21:56   ` Jonathan Santos

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c5890aee-78ac-45a7-b910-72a342f329b1@baylibre.com \
    --to=dlechner@baylibre.com \
    --cc=Jonathan.Santos@analog.com \
    --cc=Michael.Hennerich@analog.com \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=jic23@kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=marcelo.schmitt1@gmail.com \
    --cc=robh@kernel.org \
    --cc=sergiu.cuciurean@analog.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox