On Fri, 23 Sep 2022 13:40:30 +0200 Mårten Lindahl <marten.lindahl@xxxxxxxx> wrote: > 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@xxxxxxxx> See inline. Otherwise looks good to me. Jonathan > --- > drivers/iio/light/vcnl4000.c | 54 ++++++++++++++++++++++++++++++++++-- > 1 file changed, 51 insertions(+), 3 deletions(-) > > diff --git a/drivers/iio/light/vcnl4000.c b/drivers/iio/light/vcnl4000.c > index 3db4e26731bb..b2ecf8af1aa5 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 */ Comment seems inverted. Bit being set is 'shut down' I would expand the name to VCNL4040_ALS_CONF_ALS_SHUTDOWN then drop the comment as the name is self explanatory > +#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,61 @@ 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, bool en) > +{ > + int ret; > + > + mutex_lock(&data->vcnl4000_lock); > + > + ret = i2c_smbus_read_word_data(data->client, VCNL4200_AL_CONF); > + if (ret < 0) > + goto out; > + > + if (en) > + ret &= ~VCNL4040_ALS_CONF_ALS_SD; > + else > + ret |= VCNL4040_ALS_CONF_ALS_SD; > + > + ret = i2c_smbus_write_word_data(data->client, VCNL4200_AL_CONF, ret); > + > +out: > + mutex_unlock(&data->vcnl4000_lock); > + > + return ret; > +} > + > +static ssize_t vcnl4000_write_ps_enable(struct vcnl4000_data *data, bool en) > +{ > + int ret; > + > + mutex_lock(&data->vcnl4000_lock); > + > + ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF1); > + if (ret < 0) > + goto out; > + > + if (en) > + ret &= ~VCNL4040_PS_CONF1_PS_SD; > + else > + ret |= VCNL4040_PS_CONF1_PS_SD; > + > + ret = i2c_smbus_write_word_data(data->client, VCNL4200_PS_CONF1, ret); > + > +out: > + mutex_unlock(&data->vcnl4000_lock); > + > + return ret; > +} > + > 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, on); > if (ret < 0) > return ret; > > - ret = i2c_smbus_write_word_data(data->client, VCNL4200_PS_CONF1, val); > + ret = vcnl4000_write_ps_enable(data, on); > if (ret < 0) > return ret; >