diff mbox series

[v2,07/14] iio: sx9310: Use long instead of int for channel bitmaps

Message ID 20200728170317.v2.7.Iecaa50e469918a385b3e5dab375e442540ea2ad4@changeid (mailing list archive)
State New, archived
Headers show
Series sx9310 iio driver updates | expand

Commit Message

Daniel Campello July 28, 2020, 11:05 p.m. UTC
Uses for_each_set_bit() macro to loop over channel bitmaps.

Signed-off-by: Daniel Campello <campello@chromium.org>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
---

Changes in v2:
 - Changed prox_stat to chan_prox_stat bitmap.

 drivers/iio/proximity/sx9310.c | 38 ++++++++++++++++++----------------
 1 file changed, 20 insertions(+), 18 deletions(-)

Comments

Stephen Boyd July 29, 2020, 1:03 a.m. UTC | #1
Quoting Daniel Campello (2020-07-28 16:05:13)
> Uses for_each_set_bit() macro to loop over channel bitmaps.
> 
> Signed-off-by: Daniel Campello <campello@chromium.org>
> Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
> ---
> 
> Changes in v2:
>  - Changed prox_stat to chan_prox_stat bitmap.
> 
>  drivers/iio/proximity/sx9310.c | 38 ++++++++++++++++++----------------
>  1 file changed, 20 insertions(+), 18 deletions(-)
> 
> diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
> index 75e85dcd6b3572..1f04ab8507ec62 100644
> --- a/drivers/iio/proximity/sx9310.c
> +++ b/drivers/iio/proximity/sx9310.c
> @@ -130,14 +130,15 @@ struct sx9310_data {
>          * Last reading of the proximity status for each channel.
>          * We only send an event to user space when this changes.
>          */
> -       bool prox_stat[SX9310_NUM_CHANNELS];
> +       unsigned long chan_prox_stat;

This can be DECLARE_BITMAP(chan_prox_stat, SX9310_NUM_CHANNELS)

>         bool trigger_enabled;
>         /* 64-bit data + 64-bit timestamp buffer */
>         __be16 buffer[SX9310_NUM_CHANNELS + 4] __aligned(8);
>         /* Remember enabled channels and sample rate during suspend. */
>         unsigned int suspend_ctrl0;
>         struct completion completion;
> -       unsigned int chan_read, chan_event;
> +       unsigned long chan_read;
> +       unsigned long chan_event;

Same for these?

>         int channel_users[SX9310_NUM_CHANNELS];
>         unsigned int whoami;
>  };
> @@ -280,15 +281,16 @@ static const struct regmap_config sx9310_regmap_config = {
>  };
>  
>  static int sx9310_update_chan_en(struct sx9310_data *data,
> -                                unsigned int chan_read,
> -                                unsigned int chan_event)
> +                                unsigned long chan_read,
> +                                unsigned long chan_event)
>  {
>         int ret;
> +       unsigned long channels = chan_read | chan_event;
>  
> -       if ((data->chan_read | data->chan_event) != (chan_read | chan_event)) {
> +       if ((data->chan_read | data->chan_event) != channels) {
>                 ret = regmap_update_bits(data->regmap, SX9310_REG_PROX_CTRL0,
>                                          SX9310_REG_PROX_CTRL0_SENSOREN_MASK,
> -                                        chan_read | chan_event);
> +                                        channels);
>                 if (ret)
>                         return ret;
>         }
> @@ -531,6 +533,7 @@ static void sx9310_push_events(struct iio_dev *indio_dev)
>         unsigned int val, chan;
>         struct sx9310_data *data = iio_priv(indio_dev);
>         s64 timestamp = iio_get_time_ns(indio_dev);
> +       unsigned long prox_changed;
>  
>         /* Read proximity state on all channels */
>         ret = regmap_read(data->regmap, SX9310_REG_STAT0, &val);
> @@ -539,24 +542,23 @@ static void sx9310_push_events(struct iio_dev *indio_dev)
>                 return;
>         }
>  
> -       for (chan = 0; chan < SX9310_NUM_CHANNELS; chan++) {
> +       /*
> +        * Only iterate over channels with changes on proximity status that have
> +        * events enabled.
> +        */
> +       prox_changed = (data->chan_prox_stat ^ val) & data->chan_event;

I was expecting:


	bitmap_xor(&prox_changed, &data->chan_prox_stat, &val, SX9310_NUM_CHANNELS);
	bitmap_and(&prox_changed, &data->chan_event, SX9310_NUM_CHANNELS);
	
> +
> +       for_each_set_bit(chan, &prox_changed, SX9310_NUM_CHANNELS) {
>                 int dir;
>                 u64 ev;
> -               bool new_prox = val & BIT(chan);
> -
> -               if (!(data->chan_event & BIT(chan)))
> -                       continue;
> -               if (new_prox == data->prox_stat[chan])
> -                       /* No change on this channel. */
> -                       continue;
>  
> -               dir = new_prox ? IIO_EV_DIR_FALLING : IIO_EV_DIR_RISING;
> +               dir = val & BIT(chan) ? IIO_EV_DIR_FALLING : IIO_EV_DIR_RISING;
>                 ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, chan,
>                                           IIO_EV_TYPE_THRESH, dir);
>  
>                 iio_push_event(indio_dev, ev, timestamp);
> -               data->prox_stat[chan] = new_prox;
>         }
> +       data->chan_prox_stat = val;
>  }
>  
>  static irqreturn_t sx9310_irq_thread_handler(int irq, void *private)
> @@ -713,13 +715,13 @@ static irqreturn_t sx9310_trigger_handler(int irq, void *private)
>  static int sx9310_buffer_preenable(struct iio_dev *indio_dev)
>  {
>         struct sx9310_data *data = iio_priv(indio_dev);
> -       unsigned int channels = 0;
> +       unsigned long channels = 0;

Use DECLARE_BITMAP(channels, SX9310_NUM_CHANNELS)?

>         int bit, ret;
>  
>         mutex_lock(&data->mutex);
>         for_each_set_bit(bit, indio_dev->active_scan_mask,
>                          indio_dev->masklength)
> -               channels |= BIT(indio_dev->channels[bit].channel);
> +               __set_bit(indio_dev->channels[bit].channel, &channels);
>  
>         ret = sx9310_update_chan_en(data, channels, data->chan_event);
>         mutex_unlock(&data->mutex);
Andy Shevchenko July 29, 2020, 7 a.m. UTC | #2
On Wed, Jul 29, 2020 at 4:03 AM Stephen Boyd <swboyd@chromium.org> wrote:
> Quoting Daniel Campello (2020-07-28 16:05:13)
> > Uses for_each_set_bit() macro to loop over channel bitmaps.

...

> > +       unsigned long chan_prox_stat;
>
> This can be DECLARE_BITMAP(chan_prox_stat, SX9310_NUM_CHANNELS)

> > +       unsigned long chan_read;
> > +       unsigned long chan_event;
>
> Same for these?

...

> > +       prox_changed = (data->chan_prox_stat ^ val) & data->chan_event;
>
> I was expecting:
>
>
>         bitmap_xor(&prox_changed, &data->chan_prox_stat, &val, SX9310_NUM_CHANNELS);
>         bitmap_and(&prox_changed, &data->chan_event, SX9310_NUM_CHANNELS);

I agree with this. On a small number of channels (up to 32) it will be
reduced to simple operations, but will leave a possibility to have
more with easy redefine.

(though _and() above misses one argument AFAICT)

...

> > +       unsigned long channels = 0;
>
> Use DECLARE_BITMAP(channels, SX9310_NUM_CHANNELS)?
Daniel Campello July 31, 2020, 3:44 p.m. UTC | #3
On Wed, Jul 29, 2020 at 1:00 AM Andy Shevchenko
<andy.shevchenko@gmail.com> wrote:
>
> On Wed, Jul 29, 2020 at 4:03 AM Stephen Boyd <swboyd@chromium.org> wrote:
> > Quoting Daniel Campello (2020-07-28 16:05:13)
> > > Uses for_each_set_bit() macro to loop over channel bitmaps.
>
> ...
>
> > > +       unsigned long chan_prox_stat;
> >
> > This can be DECLARE_BITMAP(chan_prox_stat, SX9310_NUM_CHANNELS)
>
> > > +       unsigned long chan_read;
> > > +       unsigned long chan_event;
> >
> > Same for these?
All of these are eventually used by regmap_update_bits() which expects
unsigned int. I believe the extra complexity is not worth it given the
number of channels.
>
> ...
>
> > > +       prox_changed = (data->chan_prox_stat ^ val) & data->chan_event;
> >
> > I was expecting:
> >
> >
> >         bitmap_xor(&prox_changed, &data->chan_prox_stat, &val, SX9310_NUM_CHANNELS);
> >         bitmap_and(&prox_changed, &data->chan_event, SX9310_NUM_CHANNELS);
>
> I agree with this. On a small number of channels (up to 32) it will be
> reduced to simple operations, but will leave a possibility to have
> more with easy redefine.
>
> (though _and() above misses one argument AFAICT)
>
> ...
>
> > > +       unsigned long channels = 0;
> >
> > Use DECLARE_BITMAP(channels, SX9310_NUM_CHANNELS)?
>
>
> --
> With Best Regards,
> Andy Shevchenko
Andy Shevchenko July 31, 2020, 3:52 p.m. UTC | #4
On Fri, Jul 31, 2020 at 6:45 PM Daniel Campello <campello@chromium.org> wrote:
> On Wed, Jul 29, 2020 at 1:00 AM Andy Shevchenko
> <andy.shevchenko@gmail.com> wrote:
> >
> > On Wed, Jul 29, 2020 at 4:03 AM Stephen Boyd <swboyd@chromium.org> wrote:
> > > Quoting Daniel Campello (2020-07-28 16:05:13)
> > > > Uses for_each_set_bit() macro to loop over channel bitmaps.
> >
> > ...
> >
> > > > +       unsigned long chan_prox_stat;
> > >
> > > This can be DECLARE_BITMAP(chan_prox_stat, SX9310_NUM_CHANNELS)
> >
> > > > +       unsigned long chan_read;
> > > > +       unsigned long chan_event;
> > >
> > > Same for these?
> All of these are eventually used by regmap_update_bits() which expects
> unsigned int. I believe the extra complexity is not worth it given the
> number of channels.

Okay then. Good to have some build check for the limitation, so, what
about adding

static_assert(..._NUM_CHANNELS < BITS_PER_LONG);
after the _NUM_CHANNELS definition?
diff mbox series

Patch

diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
index 75e85dcd6b3572..1f04ab8507ec62 100644
--- a/drivers/iio/proximity/sx9310.c
+++ b/drivers/iio/proximity/sx9310.c
@@ -130,14 +130,15 @@  struct sx9310_data {
 	 * Last reading of the proximity status for each channel.
 	 * We only send an event to user space when this changes.
 	 */
-	bool prox_stat[SX9310_NUM_CHANNELS];
+	unsigned long chan_prox_stat;
 	bool trigger_enabled;
 	/* 64-bit data + 64-bit timestamp buffer */
 	__be16 buffer[SX9310_NUM_CHANNELS + 4] __aligned(8);
 	/* Remember enabled channels and sample rate during suspend. */
 	unsigned int suspend_ctrl0;
 	struct completion completion;
-	unsigned int chan_read, chan_event;
+	unsigned long chan_read;
+	unsigned long chan_event;
 	int channel_users[SX9310_NUM_CHANNELS];
 	unsigned int whoami;
 };
@@ -280,15 +281,16 @@  static const struct regmap_config sx9310_regmap_config = {
 };
 
 static int sx9310_update_chan_en(struct sx9310_data *data,
-				 unsigned int chan_read,
-				 unsigned int chan_event)
+				 unsigned long chan_read,
+				 unsigned long chan_event)
 {
 	int ret;
+	unsigned long channels = chan_read | chan_event;
 
-	if ((data->chan_read | data->chan_event) != (chan_read | chan_event)) {
+	if ((data->chan_read | data->chan_event) != channels) {
 		ret = regmap_update_bits(data->regmap, SX9310_REG_PROX_CTRL0,
 					 SX9310_REG_PROX_CTRL0_SENSOREN_MASK,
-					 chan_read | chan_event);
+					 channels);
 		if (ret)
 			return ret;
 	}
@@ -531,6 +533,7 @@  static void sx9310_push_events(struct iio_dev *indio_dev)
 	unsigned int val, chan;
 	struct sx9310_data *data = iio_priv(indio_dev);
 	s64 timestamp = iio_get_time_ns(indio_dev);
+	unsigned long prox_changed;
 
 	/* Read proximity state on all channels */
 	ret = regmap_read(data->regmap, SX9310_REG_STAT0, &val);
@@ -539,24 +542,23 @@  static void sx9310_push_events(struct iio_dev *indio_dev)
 		return;
 	}
 
-	for (chan = 0; chan < SX9310_NUM_CHANNELS; chan++) {
+	/*
+	 * Only iterate over channels with changes on proximity status that have
+	 * events enabled.
+	 */
+	prox_changed = (data->chan_prox_stat ^ val) & data->chan_event;
+
+	for_each_set_bit(chan, &prox_changed, SX9310_NUM_CHANNELS) {
 		int dir;
 		u64 ev;
-		bool new_prox = val & BIT(chan);
-
-		if (!(data->chan_event & BIT(chan)))
-			continue;
-		if (new_prox == data->prox_stat[chan])
-			/* No change on this channel. */
-			continue;
 
-		dir = new_prox ? IIO_EV_DIR_FALLING : IIO_EV_DIR_RISING;
+		dir = val & BIT(chan) ? IIO_EV_DIR_FALLING : IIO_EV_DIR_RISING;
 		ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, chan,
 					  IIO_EV_TYPE_THRESH, dir);
 
 		iio_push_event(indio_dev, ev, timestamp);
-		data->prox_stat[chan] = new_prox;
 	}
+	data->chan_prox_stat = val;
 }
 
 static irqreturn_t sx9310_irq_thread_handler(int irq, void *private)
@@ -713,13 +715,13 @@  static irqreturn_t sx9310_trigger_handler(int irq, void *private)
 static int sx9310_buffer_preenable(struct iio_dev *indio_dev)
 {
 	struct sx9310_data *data = iio_priv(indio_dev);
-	unsigned int channels = 0;
+	unsigned long channels = 0;
 	int bit, ret;
 
 	mutex_lock(&data->mutex);
 	for_each_set_bit(bit, indio_dev->active_scan_mask,
 			 indio_dev->masklength)
-		channels |= BIT(indio_dev->channels[bit].channel);
+		__set_bit(indio_dev->channels[bit].channel, &channels);
 
 	ret = sx9310_update_chan_en(data, channels, data->chan_event);
 	mutex_unlock(&data->mutex);