public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Antoni Pokusinski <apokusinski01@gmail.com>
Cc: jic23@kernel.org, dlechner@baylibre.com, nuno.sa@analog.com,
	andy@kernel.org, marcelo.schmitt1@gmail.com,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 2/2] iio: mpl3115: add threshold events support
Date: Wed, 29 Oct 2025 10:24:49 +0200	[thread overview]
Message-ID: <aQHPUQ5bU7sFojul@smile.fi.intel.com> (raw)
In-Reply-To: <20251028213351.77368-3-apokusinski01@gmail.com>

On Tue, Oct 28, 2025 at 10:33:52PM +0100, Antoni Pokusinski wrote:
> Add support for pressure and temperature rising threshold events.

...

> @@ -322,7 +339,9 @@ static const struct iio_chan_spec mpl3115_channels[] = {
>  			.storagebits = 32,
>  			.shift = 12,
>  			.endianness = IIO_BE,
> -		}
> +		},
> +		.event_spec = mpl3115_temp_press_event,
> +		.num_event_specs = ARRAY_SIZE(mpl3115_temp_press_event),
>  	},
>  	{
>  		.type = IIO_TEMP,
> @@ -338,7 +357,9 @@ static const struct iio_chan_spec mpl3115_channels[] = {
>  			.storagebits = 16,
>  			.shift = 4,
>  			.endianness = IIO_BE,
> -		}
> +		},

Just a side note below, no action from you required on this!

Yeah, yet another reminder for the comma/not-a-comma choices made initially and
why it's important to follow the advice

> +		.event_spec = mpl3115_temp_press_event,
> +		.num_event_specs = ARRAY_SIZE(mpl3115_temp_press_event),
>  	},
>  	IIO_CHAN_SOFT_TIMESTAMP(2),
>  };

...

> -	if (!(ret & MPL3115_INT_SRC_DRDY))
> +	if (!(ret & (MPL3115_INT_SRC_DRDY | MPL3115_INT_SRC_PTH |
> +		     MPL3115_INT_SRC_TTH)))

Can we rather keep this split logical?

	if (!(ret & (MPL3115_INT_SRC_TTH | MPL3115_INT_SRC_PTH |
		     MPL3115_INT_SRC_DRDY)))

>  		return IRQ_NONE;

...

> -	u8 ctrl_reg1 = data->ctrl_reg1;
> -	u8 ctrl_reg4 = data->ctrl_reg4;
> +	u8 ctrl_reg1, ctrl_reg4;

> +	guard(mutex)(&data->lock);

Why this is moved? Before the access to the data->ctrl* was done without
locking. Is it an existing bug?

> +	ctrl_reg1 = data->ctrl_reg1;
> +	ctrl_reg4 = data->ctrl_reg4;
>  
>  	if (state) {
>  		ctrl_reg1 |= MPL3115_CTRL1_ACTIVE;
>  		ctrl_reg4 |= MPL3115_CTRL4_INT_EN_DRDY;
>  	} else {
> -		ctrl_reg1 &= ~MPL3115_CTRL1_ACTIVE;
>  		ctrl_reg4 &= ~MPL3115_CTRL4_INT_EN_DRDY;
> -	}
>  
> -	guard(mutex)(&data->lock);
> +		if (!ctrl_reg4)
> +			ctrl_reg1 &= ~MPL3115_CTRL1_ACTIVE;
> +	}
>  
>  	return mpl3115_config_interrupt(data, ctrl_reg1, ctrl_reg4);

...

> +static int mpl3115_write_event_config(struct iio_dev *indio_dev,
> +				      const struct iio_chan_spec *chan,
> +				      enum iio_event_type type,
> +				      enum iio_event_direction dir,
> +				      bool state)
> +{
> +	struct mpl3115_data *data = iio_priv(indio_dev);
> +	u8 int_en_mask;
> +	u8 ctrl_reg1, ctrl_reg4;
> +
> +	switch (chan->type) {
> +	case IIO_PRESSURE:
> +		int_en_mask = MPL3115_CTRL4_INT_EN_PTH;
> +		break;
> +	case IIO_TEMP:
> +		int_en_mask = MPL3115_CTRL4_INT_EN_TTH;
> +		break;
> +	default:
> +		return -EINVAL;
> +	}

> +	guard(mutex)(&data->lock);

Similar Q here, why do you protect data that was (still is?) not protected before?

> +	ctrl_reg1 = data->ctrl_reg1;
> +	ctrl_reg4 = data->ctrl_reg4;
> +
> +	if (state) {
> +		ctrl_reg1 |= MPL3115_CTRL1_ACTIVE;
> +		ctrl_reg4 |= int_en_mask;
> +	} else {
> +		ctrl_reg4 &= ~int_en_mask;
> +
> +		if (!ctrl_reg4)
> +			ctrl_reg1 &= ~MPL3115_CTRL1_ACTIVE;
> +	}
> +
> +	return mpl3115_config_interrupt(data, ctrl_reg1, ctrl_reg4);
> +}

...

> +static int mpl3115_read_thresh(struct iio_dev *indio_dev,
> +			       const struct iio_chan_spec *chan,
> +			       enum iio_event_type type,
> +			       enum iio_event_direction dir,
> +			       enum iio_event_info info,
> +			       int *val, int *val2)
> +{
> +	struct mpl3115_data *data = iio_priv(indio_dev);
> +	int ret, press_pa;
> +	__be16 tmp;
> +
> +	if (info != IIO_EV_INFO_VALUE)
> +		return -EINVAL;
> +
> +	switch (chan->type) {
> +	case IIO_PRESSURE:
> +		ret = i2c_smbus_read_i2c_block_data(data->client,
> +						    MPL3115_PRESS_TGT, 2,

sizeof() ?

> +						    (u8 *) &tmp);

Here and elsewhere, drop the space between casting and operand.

> +		if (ret < 0)
> +			return ret;
> +
> +		/**

It's not a kernel-doc.

> +		 * Target value for the pressure is
> +		 * 16-bit unsigned value in 2 Pa units
> +		 */
> +		press_pa = be16_to_cpu(tmp) << 1;
> +		*val = press_pa / KILO;
> +		*val2 = (press_pa % KILO) * MILLI;
> +
> +		return IIO_VAL_INT_PLUS_MICRO;
> +	case IIO_TEMP:
> +		ret = i2c_smbus_read_byte_data(data->client, MPL3115_TEMP_TGT);
> +		if (ret < 0)
> +			return ret;
> +
> +		/* Target value for the temperature is 8-bit 2's complement */
> +		*val = sign_extend32(ret, 7);
> +
> +		return IIO_VAL_INT;
> +	default:
> +		return -EINVAL;
> +	}
> +}

...

> +static int mpl3115_write_thresh(struct iio_dev *indio_dev,
> +				const struct iio_chan_spec *chan,
> +				enum iio_event_type type,
> +				enum iio_event_direction dir,
> +				enum iio_event_info info,
> +				int val, int val2)
> +{
> +	struct mpl3115_data *data = iio_priv(indio_dev);
> +	u8 tmp[2];

Use proper __be16 type.

> +	if (info != IIO_EV_INFO_VALUE)
> +		return -EINVAL;
> +
> +	switch (chan->type) {
> +	case IIO_PRESSURE:
> +		val = (val * KILO + val2 / MILLI) >> 1;

> +		if (val < 0 || val > 0xffff)
> +			return -EINVAL;

U16_MAX?

> +		tmp[0] = FIELD_GET(GENMASK(15, 8), val);
> +		tmp[1] = FIELD_GET(GENMASK(7, 0), val);
> +
> +		return i2c_smbus_write_i2c_block_data(data->client,
> +						      MPL3115_PRESS_TGT, 2, tmp);

sizeof()

> +	case IIO_TEMP:
> +		if (val < -128 || val > 127)
> +			return -EINVAL;

S8_MIN, S8_MAX ?

> +		return i2c_smbus_write_byte_data(data->client,
> +						 MPL3115_TEMP_TGT, val);
> +	default:
> +		return -EINVAL;
> +	}
> +}

-- 
With Best Regards,
Andy Shevchenko



  reply	other threads:[~2025-10-29  8:24 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-28 21:33 [PATCH 0/2] iio: mpl3115: support for events Antoni Pokusinski
2025-10-28 21:33 ` [PATCH 1/2] iio: mpl3115: add ctrl_reg4 to mpl3115_data Antoni Pokusinski
2025-10-29 17:20   ` Marcelo Schmitt
2025-10-28 21:33 ` [PATCH 2/2] iio: mpl3115: add threshold events support Antoni Pokusinski
2025-10-29  8:24   ` Andy Shevchenko [this message]
2025-10-29 22:46     ` Antoni Pokusinski
2025-10-30  9:18       ` Andy Shevchenko
2025-10-29 17:13   ` Marcelo Schmitt

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=aQHPUQ5bU7sFojul@smile.fi.intel.com \
    --to=andriy.shevchenko@intel.com \
    --cc=andy@kernel.org \
    --cc=apokusinski01@gmail.com \
    --cc=dlechner@baylibre.com \
    --cc=jic23@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=marcelo.schmitt1@gmail.com \
    --cc=nuno.sa@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