linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: William Breathitt Gray <vilhelm.gray@gmail.com>
To: Andy Shevchenko <andy.shevchenko@gmail.com>
Cc: Linus Walleij <linus.walleij@linaro.org>,
	"open list:GPIO SUBSYSTEM" <linux-gpio@vger.kernel.org>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	linux-iio@vger.kernel.org
Subject: Re: [PATCH v3 3/8] gpio: pci-idio-16: Implement get_multiple callback
Date: Wed, 21 Mar 2018 14:49:43 -0400	[thread overview]
Message-ID: <20180321184943.GA24015@sophia> (raw)
In-Reply-To: <CAHp75Vfhe4qV73+A+X98bo-cpV0du5pwDERwEc0kPLGB612vmw@mail.gmail.com>

On Wed, Mar 21, 2018 at 07:45:29PM +0200, Andy Shevchenko wrote:
>On Sat, Mar 17, 2018 at 5:50 PM, William Breathitt Gray
><vilhelm.gray@gmail.com> wrote:
>> The ACCES I/O PCI-IDIO-16 series of devices provides 16
>> optically-isolated digital inputs accessed via two 8-bit ports. Since
>> eight input lines are acquired on a single port input read, the
>> PCI-IDIO-16 GPIO driver may improve multiple input reads by utilizing a
>> get_multiple callback. This patch implements the
>> idio_16_gpio_get_multiple function which serves as the respective
>> get_multiple callback.
>
>> +static int idio_16_gpio_get_multiple(struct gpio_chip *chip,
>> +       unsigned long *mask, unsigned long *bits)
>> +{
>> +       struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
>> +       size_t i;
>> +       const unsigned int gpio_reg_size = 8;
>> +       unsigned int bits_offset;
>> +       size_t word_index;
>> +       unsigned int word_offset;
>> +       unsigned long word_mask;
>
>> +       const unsigned long port_mask = GENMASK(gpio_reg_size, 0);
>
>gpio_reg_size - 1?

Oops, looks like I made an off-by-one error here so I'll make sure to
fix that up.

>Though I would prefer not to have that variable at all, just use 8 or
>7 respectively.

This device is simple enough that throughout this function I could
inline gpio_reg_size and port_mask to 8 and 0xFF respectively, but I
would like to keep the code generic enough for reuse in other drivers.
In addition, I believe the variable names help keep the intention of the
code clear, so I'll stick with dedicated const variables for now if
there are no other objections.

>
>> +       unsigned long port_state;
>
>> +       u8 __iomem ports[] = {
>> +               idio16gpio->reg->out0_7, idio16gpio->reg->out8_15,
>
>> +               idio16gpio->reg->in0_7, idio16gpio->reg->in8_15
>
>I would leave comma even here.

Will do.

>
>> +       };
>
>> +       /* get bits are evaluated a gpio port register at a time */
>> +       for (i = 0; i < ARRAY_SIZE(ports); i++) {
>> +               /* gpio offset in bits array */
>> +               bits_offset = i * gpio_reg_size;
>> +
>> +               /* word index for bits array */
>> +               word_index = BIT_WORD(bits_offset);
>> +
>> +               /* gpio offset within current word of bits array */
>> +               word_offset = bits_offset % BITS_PER_LONG;
>> +
>> +               /* mask of get bits for current gpio within current word */
>> +               word_mask = mask[word_index] & (port_mask << word_offset);
>> +               if (!word_mask) {
>> +                       /* no get bits in this port so skip to next one */
>> +                       continue;
>> +               }
>> +
>> +               /* read bits from current gpio port */
>> +               port_state = ioread8(ports + i);
>> +
>> +               /* store acquired bits at respective bits array offset */
>> +               bits[word_index] |= port_state << word_offset;
>> +       }
>
>I would propose to do other way around, i.e.
>read all ports to the bitmap array and call bitmap_and() after.
>
>Further optimization can be something like introduction of generic
>
>bitmap_copy_uXX_off(unsigned long *dst, u8 src, unsigned int offset);
>
>It can be done using macros, though it's another story not quite
>related to the topic.

Port I/O is significantly more costly to perform than the bitmask
evaluations for each port. Despite the increased complexity of the loop
logic, I believe the latency improvements of skipping unnecessary I/O
port reads are worth the trouble.

I do like the idea of a bitmap_copy_uXX_off macro as that could be quite
useful in general. Even if not for this particular patchset, I would be
interested in seeing that functionality added to the bitmap API. Perhaps
I might implement it as a standlone patch when I have some free time.

William Breathitt Gray

>
>> +}
>
>-- 
>With Best Regards,
>Andy Shevchenko

  reply	other threads:[~2018-03-21 18:49 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-17 15:49 [PATCH v3 0/8] Implement get_multiple for ACCES and PC/104 drivers William Breathitt Gray
2018-03-17 15:49 ` [PATCH v3 1/8] iio: stx104: Implement get_multiple callback William Breathitt Gray
2018-03-17 18:22   ` Jonathan Cameron
2018-03-17 18:51   ` Andy Shevchenko
2018-03-17 19:15     ` William Breathitt Gray
2018-03-17 15:50 ` [PATCH v3 2/8] gpio: 104-idio-16: " William Breathitt Gray
2018-03-21 15:32   ` Andy Shevchenko
2018-03-21 16:04     ` William Breathitt Gray
2018-03-17 15:50 ` [PATCH v3 3/8] gpio: pci-idio-16: " William Breathitt Gray
2018-03-21 17:45   ` Andy Shevchenko
2018-03-21 18:49     ` William Breathitt Gray [this message]
2018-03-17 15:51 ` [PATCH v3 4/8] gpio: pcie-idio-24: Implement get_multiple/set_multiple callbacks William Breathitt Gray
2018-03-17 15:51 ` [PATCH v3 5/8] gpio: 104-dio-48e: Implement get_multiple callback William Breathitt Gray
2018-03-17 15:51 ` [PATCH v3 6/8] gpio: 104-idi-48: " William Breathitt Gray
2018-03-17 15:51 ` [PATCH v3 7/8] gpio: gpio-mm: " William Breathitt Gray
2018-03-17 15:52 ` [PATCH v3 8/8] gpio: ws16c48: " William Breathitt Gray

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=20180321184943.GA24015@sophia \
    --to=vilhelm.gray@gmail.com \
    --cc=andy.shevchenko@gmail.com \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@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).