mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: "David Lechner (TI)" <dlechner@baylibre.com>
Cc: "Jonathan Cameron" <jic23@kernel.org>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>,
	"Linus Walleij" <linusw@kernel.org>,
	"Bartosz Golaszewski" <brgl@kernel.org>,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-gpio@vger.kernel.org, "Chris Hall" <c-hall@ti.com>,
	"Patrick Edwards" <pedwards@ti.com>,
	"Kurt Borja" <kuurtb@gmail.com>
Subject: Re: [PATCH] iio: adc: ti-ads112c14: add gpio support
Date: Mon, 28 Sep 2026 11:36:05 +0300	[thread overview]
Message-ID: <arom9T0azKX9jV5m@ashevche-desk.local> (raw)
In-Reply-To: <20260925-iio-adc-ti-ads112c14-gpio-v1-1-2a2b218ebf3a@baylibre.com>

On Fri, Sep 25, 2026 at 04:50:47PM -0500, David Lechner (TI) wrote:
> Add support for using the AIN4/GPIO0 to AIN7/GPIO3 pins as GPIOs when
> the gpio-controller property is present.
> 
> Pins that are already used for something else according to the
> devicetree are excluded from the valid GPIO mask. This includes analog
> inputs and excitation current outputs used by channels, REFP/REFN when
> an external reference is used, the /FAULT and /DRDY interrupts and the
> external clock input.
> 
> The per-pin register field macros are replaced with parameterized ones
> so that they can be used with the GPIO offset.

...

> +	DECLARE_BITMAP(gpio_reserved_mask, ADS112C14_NUM_GPIO);

Why not use valid_mask in GPIO chip directly?

...

> +static void ads112c14_reserve_gpio_for_ain(unsigned long *gpio_reserved_mask,
> +					   u32 ain)
> +{
> +	if (ain >= 4 && ain <= 7)
> +		set_bit(ain - 4, gpio_reserved_mask);

Why atomic op is needed?

> +}

...

> +static int ads112c14_gpio_init_valid_mask(struct gpio_chip *gc,
> +					  unsigned long *valid_mask,
> +					  unsigned int ngpios)
> +{
> +	struct iio_dev *indio_dev = gpiochip_get_data(gc);
> +	struct ads112c14_data *data = iio_priv(indio_dev);

> +	bitmap_fill(valid_mask, ngpios);

It's already filled by GPIOLIB, isn't it?

> +	bitmap_andnot(valid_mask, valid_mask, data->gpio_reserved_mask, ngpios);
> +
> +	return 0;
> +}

> +static int ads112c14_gpio_init(struct iio_dev *indio_dev)
> +{
> +	struct ads112c14_data *data = iio_priv(indio_dev);
> +	struct device *dev = indio_dev->dev.parent;
> +
> +	for (unsigned int i = 0; i < ADS112C14_NUM_GPIO; i++) {
> +		data->gpio_names[i] = devm_kasprintf(dev, GFP_KERNEL, "%s:GPIO%u",
> +						     dev_name(&indio_dev->dev), i);
> +		if (!data->gpio_names[i])
> +			return -ENOMEM;

Wondering if you can utilise devm_kasprintf_strarray().

> +	}
> +
> +	data->gc = (struct gpio_chip) {
> +		.owner = THIS_MODULE,
> +		.label = dev_name(dev),
> +		.parent = dev,
> +		.base = -1,
> +		.ngpio = ADS112C14_NUM_GPIO,
> +		.names = data->gpio_names,
> +		.can_sleep = true,
> +		.init_valid_mask = ads112c14_gpio_init_valid_mask,
> +		.get_direction = ads112c14_gpio_get_direction,
> +		.direction_input = ads112c14_gpio_direction_input,
> +		.direction_output = ads112c14_gpio_direction_output,
> +		.get = ads112c14_gpio_get,
> +		.set = ads112c14_gpio_set,
> +	};
> +
> +	return devm_gpiochip_add_data(dev, &data->gc, indio_dev);
> +}

...

>  static int ads112c14_parse_channels(struct iio_dev *indio_dev,
> -				    bool *need_avdd_ref, bool *need_ext_ref)
> +				    bool *need_avdd_ref, bool *need_ext_ref,
> +				    unsigned long *gpio_reserved_mask)
>  {

> +			ads112c14_reserve_gpio_for_ain(gpio_reserved_mask, pair[0]);

> +			ads112c14_reserve_gpio_for_ain(gpio_reserved_mask, pair[0]);
> +			ads112c14_reserve_gpio_for_ain(gpio_reserved_mask, pair[1]);
>  		} else {

> +			ads112c14_reserve_gpio_for_ain(gpio_reserved_mask, pair[0]);
> +			if (measurement->iadc_count > 1)
> +				ads112c14_reserve_gpio_for_ain(gpio_reserved_mask, pair[1]);

Can't this be done afterwards at init_valid_mask() stage?

...

>  static int ads112c14_probe(struct i2c_client *client)
>  {
> +	DECLARE_BITMAP(gpio_reserved_mask, ADS112C14_NUM_GPIO);

Hmm... But you have one already in the struct, why another one here?


...

> +	/* FAULT shares a pin with GPIO2. */
> +	if (fwnode_property_match_string(dev_fwnode(dev), "interrupt-names", "fault") >= 0)
> +		set_bit(2, gpio_reserved_mask);

Non-atomic?

...

>  	if (fwnode_property_match_string(dev_fwnode(dev), "interrupt-names", "drdy") >= 0) {
>  		data->drdy_irq = fwnode_irq_get_byname(dev_fwnode(dev), "drdy");
>  		if (data->drdy_irq < 0)
>  			return dev_err_probe(dev, data->drdy_irq,
>  					     "failed to get drdy interrupt\n");
>  
> +		/* DRDY shares a pin with GPIO3. */
> +		set_bit(3, gpio_reserved_mask);

Ditto.


>  		if (clk)
>  			return dev_err_probe(dev, -EINVAL,
>  					     "cannot use both DRDY and CLK - they share the same pin\n");

...

>  	if (clk) {
> +		/* CLK shares a pin with GPIO3. */
> +		set_bit(3, gpio_reserved_mask);

Ditto.

>  		ret = regmap_update_bits(data->regmap, ADS112C14_REG_GPIO_CFG,
> -					 ADS112C14_GPIO_CFG_GPIO3_CFG,
> -					 FIELD_PREP(ADS112C14_GPIO_CFG_GPIO3_CFG,
> +					 ADS112C14_GPIO_CFG_GPIO_CFG(3),
> +					 FIELD_PREP(ADS112C14_GPIO_CFG_GPIO_CFG(3),
>  						    ADS112C14_GPIO_CFG_GPIO_CFG_INPUT));
>  		if (ret)
>  			return ret;

...

> +	if (device_property_read_bool(dev, "gpio-controller")) {
> +		bitmap_copy(data->gpio_reserved_mask, gpio_reserved_mask,
> +			    ADS112C14_NUM_GPIO);
> +
> +		ret = ads112c14_gpio_init(indio_dev);
> +		if (ret)
> +			return ret;
> +	}

This all smells like it should be refactored to nicely have it in one place in init_valid_mask().

-- 
With Best Regards,
Andy Shevchenko



      parent reply	other threads:[~2026-09-28  8:36 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-25 21:50 David Lechner (TI)
2026-09-26  0:46 ` Jonathan Cameron
2026-09-28  8:36 ` Andy Shevchenko [this message]

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=arom9T0azKX9jV5m@ashevche-desk.local \
    --to=andriy.shevchenko@intel.com \
    --cc=andy@kernel.org \
    --cc=brgl@kernel.org \
    --cc=c-hall@ti.com \
    --cc=dlechner@baylibre.com \
    --cc=jic23@kernel.org \
    --cc=kuurtb@gmail.com \
    --cc=linusw@kernel.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    --cc=pedwards@ti.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

all inboxes | Powered by JetHome®