Linux IIO development
 help / color / mirror / Atom feed
From: Marten Lindahl <martenli@axis.com>
To: Paul Cercueil <paul@crapouillou.net>
Cc: "Mårten Lindahl" <Marten.Lindahl@axis.com>,
	"Jonathan Cameron" <jic23@kernel.org>,
	"Lars-Peter Clausen" <lars@metafoo.de>,
	"Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>,
	"linux-iio@vger.kernel.org" <linux-iio@vger.kernel.org>,
	kernel <kernel@axis.com>
Subject: Re: [PATCH 1/3] iio: light: vcnl4000: Preserve conf bits when toggle power
Date: Thu, 22 Sep 2022 14:18:31 +0200	[thread overview]
Message-ID: <YyxSlxwN9WlH0a+/@axis.com> (raw)
In-Reply-To: <2V4JIR.7354DS8U39Q53@crapouillou.net>

On Wed, Sep 21, 2022 at 12:23:26AM +0200, Paul Cercueil wrote:
> Hi Mårten,
> 
> Le mar., sept. 20 2022 at 20:09:56 +0200, Mårten Lindahl 
> <marten.lindahl@axis.com> a écrit :
> > As the vcnl4040 and vcnl4200 chip uses runtime power management for
> > turning the ambient light and proximity sensors on/off, it overwrites
> > the entire register each time. In ALS_CONF register bit fields ALS_IT,
> > ALS_PERS, ALS_INT_EN are overwritten. In PS_CONF1 register bit fields
> > PS_DUTY, PS_PERS, PS_IT, PS_HD, and PS_INT are overwritten.
> > 
> > Add functions for preserving the affected bit fields when changing 
> > power
> > state.
> > 
> > Signed-off-by: Mårten Lindahl <marten.lindahl@axis.com>
> > ---
> >  drivers/iio/light/vcnl4000.c | 53 
> > ++++++++++++++++++++++++++++++++++--
> >  1 file changed, 51 insertions(+), 2 deletions(-)
> > 
> > diff --git a/drivers/iio/light/vcnl4000.c 
> > b/drivers/iio/light/vcnl4000.c
> > index 3db4e26731bb..0b226c684957 100644
> > --- a/drivers/iio/light/vcnl4000.c
> > +++ b/drivers/iio/light/vcnl4000.c
> > @@ -74,6 +74,9 @@
> >  #define VCNL4000_PROX_EN	BIT(1) /* start proximity measurement */
> >  #define VCNL4000_SELF_TIMED_EN	BIT(0) /* start self-timed 
> > measurement */
> > 
> > +#define VCNL4040_ALS_CONF_ALS_SD	BIT(0) /* Enable ambient light 
> > sensor */
> > +#define VCNL4040_PS_CONF1_PS_SD	BIT(0) /* Enable proximity sensor */
> > +
> >  /* Bit masks for interrupt registers. */
> >  #define VCNL4010_INT_THR_SEL	BIT(0) /* Select threshold interrupt 
> > source */
> >  #define VCNL4010_INT_THR_EN	BIT(1) /* Threshold interrupt type */
> > @@ -188,16 +191,62 @@ static int vcnl4000_init(struct vcnl4000_data 
> > *data)
> >  	return data->chip_spec->set_power_state(data, true);
> >  };
> > 
> > +static ssize_t vcnl4000_write_als_enable(struct vcnl4000_data *data, 
> > int val)
> > +{
> > +	int ret;
> > +
> > +	switch (data->id) {
> > +	case VCNL4040:
> > +	case VCNL4200:

Hi Paul!
Thanks for looking at this!
> 
> The switch isn't needed, since vcnl4200_set_power_state() is only 
> called for compatible chips.

Yes, your're right. I will remove the switch.
> 
> > +		ret = i2c_smbus_read_word_data(data->client, VCNL4200_AL_CONF);
> > +		if (ret < 0)
> > +			return ret;
> > +
> > +		if (val)
> > +			ret &= ~VCNL4040_ALS_CONF_ALS_SD;
> > +		else
> > +			ret |= VCNL4040_ALS_CONF_ALS_SD;
> > +
> > +		return i2c_smbus_write_word_data(data->client, VCNL4200_AL_CONF,
> > +						 ret);
> 
> Are you sure a race cannot happen here?
> 
> I would assume it cannot, but that's still a bit fishy.

Ok, I see what you mean. I cannot tell for sure, but I can guard the
read and write with the mutex.

> 
> This driver would benefit from a regmap conversion, but it's probably a 
> bit too much to ask...

I can try to do this in a separate patch after these ones if that's ok?

> 
> > +	default:
> > +		return -EINVAL;
> > +	}
> > +}
> > +
> > +static ssize_t vcnl4000_write_ps_enable(struct vcnl4000_data *data, 
> > int val)
> > +{
> > +	int ret;
> > +
> > +	switch (data->id) {
> > +	case VCNL4040:
> > +	case VCNL4200:
> > +		ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF1);
> > +		if (ret < 0)
> > +			return ret;
> > +
> > +		if (val)
> > +			ret &= ~VCNL4040_PS_CONF1_PS_SD;
> > +		else
> > +			ret |= VCNL4040_PS_CONF1_PS_SD;
> > +
> > +		return i2c_smbus_write_word_data(data->client,
> > +						 VCNL4200_PS_CONF1, ret);
> > +	default:
> > +		return -EINVAL;
> > +	}
> > +}
> > +
> >  static int vcnl4200_set_power_state(struct vcnl4000_data *data, bool 
> > on)
> >  {
> >  	u16 val = on ? 0 /* power on */ : 1 /* shut down */;
> >  	int ret;
> > 
> > -	ret = i2c_smbus_write_word_data(data->client, VCNL4200_AL_CONF, 
> > val);
> > +	ret = vcnl4000_write_als_enable(data, !val);
> 
> You set "val" to 0 if "on", and to 1 if "!on", then pass "!val", this 
> is very confusing. You could just pass "on" here and below.

I agree! I will change it to only use the boolean 'on'.

Kind reagards
Mårten
> 
> Cheers,
> -Paul
> 
> >  	if (ret < 0)
> >  		return ret;
> > 
> > -	ret = i2c_smbus_write_word_data(data->client, VCNL4200_PS_CONF1, 
> > val);
> > +	ret = vcnl4000_write_ps_enable(data, !val);
> >  	if (ret < 0)
> >  		return ret;
> > 
> > --
> > 2.30.2
> > 
> 
> 

  reply	other threads:[~2022-09-22 12:18 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-20 18:09 [PATCH 0/3] Add basic attributes for vcnl4040 Mårten Lindahl
2022-09-20 18:09 ` [PATCH 1/3] iio: light: vcnl4000: Preserve conf bits when toggle power Mårten Lindahl
2022-09-20 22:23   ` Paul Cercueil
2022-09-22 12:18     ` Marten Lindahl [this message]
2022-09-20 18:09 ` [PATCH 2/3] iio: light: vcnl4000: Add enable attributes for vcnl4040 Mårten Lindahl
2022-09-20 22:01   ` Paul Cercueil
2022-09-22 13:04     ` Marten Lindahl
2022-09-22 14:10       ` Paul Cercueil
2022-09-22 18:39         ` Marten Lindahl
2022-09-20 18:09 ` [PATCH 3/3] iio: light: vcnl4000: Add ps_it " Mårten Lindahl
2022-09-20 22:12   ` Paul Cercueil
2022-09-22 13:31     ` Marten Lindahl

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=YyxSlxwN9WlH0a+/@axis.com \
    --to=martenli@axis.com \
    --cc=Marten.Lindahl@axis.com \
    --cc=jic23@kernel.org \
    --cc=kernel@axis.com \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=paul@crapouillou.net \
    --cc=u.kleine-koenig@pengutronix.de \
    /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