linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Rojhalat Ibrahim <imr@rtschenk.de>
To: Alexandre Courbot <gnurou@gmail.com>
Cc: "linux-gpio@vger.kernel.org" <linux-gpio@vger.kernel.org>,
	Linus Walleij <linus.walleij@linaro.org>,
	Alexandre Courbot <acourbot@nvidia.com>
Subject: Re: [PATCH] gpio-generic: add bgpio_set_multiple functions
Date: Wed, 14 Jan 2015 14:18:39 +0100	[thread overview]
Message-ID: <1434420.1kjsRXqHkx@pcimr> (raw)
In-Reply-To: <CAAVeFu+qctx7q_rQ5h4+msHwfkGGjoxf0m+wEPwoNby32E3PWA@mail.gmail.com>

On Wednesday 14 January 2015 11:43:54 Alexandre Courbot wrote:
> On Tue, Jan 13, 2015 at 7:37 PM, Rojhalat Ibrahim <imr@rtschenk.de> wrote:
> > Add set_multiple functions to the generic driver for memory-mapped GPIO
> > controllers to improve performance when setting multiple outputs
> > simultaneously.
> 
> Great idea ; this driver is an obvious candidate to support this.
> 
> >
> > Signed-off-by: Rojhalat Ibrahim <imr@rtschenk.de>
> > ---
> >  drivers/gpio/gpio-generic.c |   79 ++++++++++++++++++++++++++++++++++++++++++++
> >  1 file changed, 79 insertions(+)
> >
> > diff --git a/drivers/gpio/gpio-generic.c b/drivers/gpio/gpio-generic.c
> > index 16f6115..cb6d0b7 100644
> > --- a/drivers/gpio/gpio-generic.c
> > +++ b/drivers/gpio/gpio-generic.c
> > @@ -160,6 +160,31 @@ static void bgpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
> >         spin_unlock_irqrestore(&bgc->lock, flags);
> >  }
> >
> > +static void bgpio_set_multiple(struct gpio_chip *gc, unsigned long *mask,
> > +                              unsigned long *bits)
> > +{
> > +       struct bgpio_chip *bgc = to_bgpio_chip(gc);
> > +       unsigned long flags;
> > +       int i;
> > +
> > +       spin_lock_irqsave(&bgc->lock, flags);
> > +
> > +       for (i = 0; i < bgc->bits; i++) {
> > +               if (*mask == 0)
> > +                       break;
> > +               if (__test_and_clear_bit(i, mask)) {
> > +                       if (test_bit(i, bits))
> > +                               bgc->data |= bgc->pin2mask(bgc, i);
> > +                       else
> > +                               bgc->data &= ~bgc->pin2mask(bgc, i);
> > +               }
> > +       }
> > +
> > +       bgc->write_reg(bgc->reg_dat, bgc->data);
> > +
> > +       spin_unlock_irqrestore(&bgc->lock, flags);
> > +}
> > +
> >  static void bgpio_set_with_clear(struct gpio_chip *gc, unsigned int gpio,
> >                                  int val)
> >  {
> > @@ -172,6 +197,32 @@ static void bgpio_set_with_clear(struct gpio_chip *gc, unsigned int gpio,
> >                 bgc->write_reg(bgc->reg_clr, mask);
> >  }
> >
> > +static void bgpio_set_multiple_with_clear(struct gpio_chip *gc,
> > +                                         unsigned long *mask,
> > +                                         unsigned long *bits)
> > +{
> > +       struct bgpio_chip *bgc = to_bgpio_chip(gc);
> > +       unsigned long set_mask = 0;
> > +       unsigned long clear_mask = 0;
> > +       int i;
> > +
> > +       for (i = 0; i < bgc->bits; i++) {
> > +               if (*mask == 0)
> > +                       break;
> > +               if (__test_and_clear_bit(i, mask)) {
> > +                       if (test_bit(i, bits))
> > +                               set_mask |= bgc->pin2mask(bgc, i);
> > +                       else
> > +                               clear_mask |= bgc->pin2mask(bgc, i);
> > +               }
> > +       }
> > +
> > +       if (set_mask)
> > +               bgc->write_reg(bgc->reg_set, set_mask);
> > +       if (clear_mask)
> > +               bgc->write_reg(bgc->reg_clr, clear_mask);
> > +}
> 
> Isn't this function missing spinlock protection?
> 

I followed the lead of the bgpio_set_with_clear function which also does not
use a spinlock. With dedicated set and clear registers it shouldn't be
necessary.

> > +
> >  static void bgpio_set_set(struct gpio_chip *gc, unsigned int gpio, int val)
> >  {
> >         struct bgpio_chip *bgc = to_bgpio_chip(gc);
> > @@ -190,6 +241,31 @@ static void bgpio_set_set(struct gpio_chip *gc, unsigned int gpio, int val)
> >         spin_unlock_irqrestore(&bgc->lock, flags);
> >  }
> >
> > +static void bgpio_set_multiple_set(struct gpio_chip *gc, unsigned long *mask,
> > +                                  unsigned long *bits)
> > +{
> > +       struct bgpio_chip *bgc = to_bgpio_chip(gc);
> > +       unsigned long flags;
> > +       int i;
> > +
> > +       spin_lock_irqsave(&bgc->lock, flags);
> > +
> > +       for (i = 0; i < bgc->bits; i++) {
> > +               if (*mask == 0)
> > +                       break;
> > +               if (__test_and_clear_bit(i, mask)) {
> > +                       if (test_bit(i, bits))
> > +                               bgc->data |= bgc->pin2mask(bgc, i);
> > +                       else
> > +                               bgc->data &= ~bgc->pin2mask(bgc, i);
> > +               }
> > +       }
> > +
> > +       bgc->write_reg(bgc->reg_set, bgc->data);
> > +
> > +       spin_unlock_irqrestore(&bgc->lock, flags);
> > +}
> 
> Couldn't it be possible to factorize a great deal of these 3 functions?
> 
> The only difference between bgpio_set_multiple() and
> bgpio_set_multiple_set() is the register that is written. In
> bgpio_set_multiple_set(), you only handle the set and cleared bits in
> different variables.
> 
> How about a private function that looks like this:
> 
> static void __bgpio_multiple_get_masks(struct bgpio_chip *bgc,
>                                        unsigned long *mask, unsigned long *bits,
>                                        unsigned long *set_mask,
>                                        unsigned long *clear_mask)
> {
>        int i;
> 
>        *set_mask = 0;
>        *clear_mask = 0;
> 
>        for (i = 0; i < bgc->bits; i++) {
>                if (*mask == 0)
>                        break;
>                if (__test_and_clear_bit(i, mask)) {
>                        if (test_bit(i, bits))
>                                *set_mask |= bgc->pin2mask(bgc, i);
>                        else
>                                *clear_mask |= bgc->pin2mask(bgc, i);
>                }
>        }
> }
> 
> Then, you could have:
> 
> static void bgpio_set_multiple_with_clear(struct gpio_chip *gc,
>                                          unsigned long *mask,
>                                          unsigned long *bits)
> {
>        struct bgpio_chip *bgc = to_bgpio_chip(gc);
>        unsigned long flags;
>        unsigned long set_mask, clear_mask;
> 
>        spin_lock_irqsave(&bgc->lock, flags);
> 
>        __bgpio_multiple_get_masks(bgc, mask, bits, &set_mask, &clear_mask);
> 
>        if (set_mask)
>                bgc->write_reg(bgc->reg_set, set_mask);
>        if (clear_mask)
>                bgc->write_reg(bgc->reg_clr, clear_mask);
> 
>        spin_unlock_irqrestore(&bgc->lock, flags);
> }
> 
> and:
> 
> static void bgpio_set_multiple(struct gpio_chip *gc, unsigned long *mask,
>                               unsigned long *bits)
> {
>        struct bgpio_chip *bgc = to_bgpio_chip(gc);
>        unsigned long flags;
>        unsigned long set_mask, clear_mask;
> 
>        spin_lock_irqsave(&bgc->lock, flags);
> 
>        __bgpio_multiple_get_masks(bgc, mask, bits, &set_mask, &clear_mask);
> 
>        bgc->data |= set_mask;
>        bgc->data &= ~clear_mask;
> 
>        bgc->write_reg(bgc->reg_dat, bgc->data);
> 
>        spin_unlock_irqrestore(&bgc->lock, flags);
> }
> 
> ... and something similar for __bgpio_multiple_get_masks. This would
> probably result in a smaller patch on top or reducing duplicate code.

You are right, of course. I'll post a revised version.


      reply	other threads:[~2015-01-14 13:18 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-13 10:37 [PATCH] gpio-generic: add bgpio_set_multiple functions Rojhalat Ibrahim
2015-01-14  2:43 ` Alexandre Courbot
2015-01-14 13:18   ` Rojhalat Ibrahim [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=1434420.1kjsRXqHkx@pcimr \
    --to=imr@rtschenk.de \
    --cc=acourbot@nvidia.com \
    --cc=gnurou@gmail.com \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    /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;
as well as URLs for NNTP newsgroup(s).