Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp806924imm; Tue, 15 May 2018 09:26:05 -0700 (PDT) X-Google-Smtp-Source: AB8JxZoTeG04TrKHYzG838prszPz/y5bgLx1B0rio4L0hmkg3tydjFnH6y3+K8yAlf2rtFZOSlLZ X-Received: by 2002:a62:d9d9:: with SMTP id b86-v6mr15889908pfl.41.1526401565842; Tue, 15 May 2018 09:26:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526401565; cv=none; d=google.com; s=arc-20160816; b=R1kz8vRrjXZ3TjmIUPfHySCU4RSkdbeB56DL3VHmTpgsDdZUlzxV6OK+Z9d4LPs0CS M1rVWXtMHqcUvfSX7ExaXO1Kt8By/1NHBh4qgIl/t279Fr+eGdqB+hm+G5S00v8APbqZ pNa8B/Qke4ynPAMSU8911ATlyKpQQkCA4jRGh55WxAhD2zw2xdavK5dkgx4URLrprqJ/ znH3AOB742A1zjjwt7gQfqrxtXanCbKkq4MEj5OgzZKFk9eGMg/WLo0MNjD+rPku4v6D 1RFM0AENtv4bUg2QL8pen4j/ckktyXe3BepoGdxJuG6swgM5A3Hdx8vq1D7EWjZ9q9MR om5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=3eDCtibkXcA48k6t/AjHEMBum5bdD8G3sSUcXpNzOBs=; b=lWZH8LGJvmsUyx7jyWbxwy6hHGMqCEzffkBOeBYqk1g/neM0pVZELq7yTkSXFVFhCT Uz6RtiX++eRCH/CSMwXOdzhIoV1qEi+4x4XAWL9zJyrg/gw+4ynp56mc9azjGyAs8hzi 0DVkGkNd8RwNpRQ2NcfHEd1jEben1J1TuBieYHM8cZ39vCvBwidQB/jIo19DrZj4Kjxu qS3AQP88QivN0czXHRxozwK1GHH9Fl7nyVMiEOr2f+7A7nEoFBHk0D6hrBE5zUGYtzmY mK09gVUZ+56F4xWmOitPO4T3KvSOCJurjTGHz1YU+Q6CyhMu7D1dPKR+D4ezro8Hb030 31Rg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=hqr5GStQ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a23-v6si300542pls.571.2018.05.15.09.25.47; Tue, 15 May 2018 09:26:05 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=hqr5GStQ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754269AbeEOQYB (ORCPT + 99 others); Tue, 15 May 2018 12:24:01 -0400 Received: from mail-yb0-f194.google.com ([209.85.213.194]:40618 "EHLO mail-yb0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754250AbeEOQX5 (ORCPT ); Tue, 15 May 2018 12:23:57 -0400 Received: by mail-yb0-f194.google.com with SMTP id o80-v6so255959ybc.7; Tue, 15 May 2018 09:23:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=3eDCtibkXcA48k6t/AjHEMBum5bdD8G3sSUcXpNzOBs=; b=hqr5GStQY7naopL/6aJZ1hbpezuWc8V2PMXD5Wx3W5pTeZxaXC3vfav67Ip/VvUYav 97FHibFBQ5RpaEUUoMckVLrHfSE78iWbY304H7/nelurM5vEjDF5kZZWZRd1/UsmfaDj q4+TghLd6r/jls31K1ypXi2cRqfr9RvM0F/Nd51hYEc51gxuDv4v0Ok5sLv+PyQAEEgl k+WfmWl1H8rO2B53P8E6yP6KZUi9OxzBQuv/x84WzPcQTQ3mnB9QbjMv4eABWZCCFLnH nEJqso2LB2m/wKEJ9r7pnzMCRxMeqmf8qfBVNtPiztDumzKW5Lx7G2DO1MpiVIHpMjme TnSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=3eDCtibkXcA48k6t/AjHEMBum5bdD8G3sSUcXpNzOBs=; b=QyL47B3dW8vNvYQKQxCOlDLYT58zFgQsYbYbvOjWULgiLJLYF8c7s8O6zmCYX/cI2m 8SezdYTZAZ1QYwwLX0oaymex4227uHJoNpx7s+JlUZlfte9AOfPcz9JU/4Bt16+j5MnD kIzvVdJPwkrT6/uX+Su25qQbKVswLU28OZMLqxBZYdwlX72+xotDTNoiXtJhEmFXQnW2 g/DicFnFj+a+zVSsYq0UmiR4BG0SDofWdJcq1JmrLQMAlrISgeV4ziBgLgBFbAJqY6L1 oMNuQDV1+S5YBgwYAscvRhgAa9a7lVK13br4MWm++i+LlFROZwEiygXQdERJFTeVxpJs m1YQ== X-Gm-Message-State: ALKqPwe7H8vmjvsjNaSAZXURyk+QvjNxuKd1BCrMZR/n7NoOwHtZ7Kz7 eW5o4uY/90j3Xnl5lC1rA/I= X-Received: by 2002:a25:d04e:: with SMTP id h75-v6mr9010148ybg.173.1526401437128; Tue, 15 May 2018 09:23:57 -0700 (PDT) Received: from localhost ([72.188.97.40]) by smtp.gmail.com with ESMTPSA id a4-v6sm146814ywa.87.2018.05.15.09.23.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 15 May 2018 09:23:56 -0700 (PDT) From: William Breathitt Gray To: linus.walleij@linaro.org Cc: linux-gpio@vger.kernel.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, William Breathitt Gray Subject: [PATCH v4 8/8] gpio: pcie-idio-24: Utilize for_each_set_clump macro Date: Tue, 15 May 2018 12:23:51 -0400 Message-Id: <7266e618f199840e54624edcefb7f2e6b320b0f7.1526400945.git.vilhelm.gray@gmail.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Replace verbose implementation in get_multiple/set_multiple callbacks with for_each_set_clump macro to simplify code and improve clarity. Signed-off-by: William Breathitt Gray --- drivers/gpio/gpio-pcie-idio-24.c | 102 +++++++++++-------------------- 1 file changed, 36 insertions(+), 66 deletions(-) diff --git a/drivers/gpio/gpio-pcie-idio-24.c b/drivers/gpio/gpio-pcie-idio-24.c index f953541e7890..b4d300338a05 100644 --- a/drivers/gpio/gpio-pcie-idio-24.c +++ b/drivers/gpio/gpio-pcie-idio-24.c @@ -199,41 +199,21 @@ static int idio_24_gpio_get_multiple(struct gpio_chip *chip, { struct idio_24_gpio *const idio24gpio = 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 - 1, 0); - unsigned long port_state; + size_t word; + unsigned int offset; void __iomem *ports[] = { &idio24gpio->reg->out0_7, &idio24gpio->reg->out8_15, &idio24gpio->reg->out16_23, &idio24gpio->reg->in0_7, &idio24gpio->reg->in8_15, &idio24gpio->reg->in16_23, }; + const size_t num_ports = ARRAY_SIZE(ports) + 1; + unsigned long port_state; const unsigned long out_mode_mask = BIT(1); /* clear bits array to a clean slate */ bitmap_zero(bits, chip->ngpio); - /* get bits are evaluated a gpio port register at a time */ - for (i = 0; i < ARRAY_SIZE(ports) + 1; 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; - } - + for_each_set_clump(i, word, offset, mask, num_ports, 8) { /* read bits from current gpio port (port 6 is TTL GPIO) */ if (i < 6) port_state = ioread8(ports[i]); @@ -243,7 +223,7 @@ static int idio_24_gpio_get_multiple(struct gpio_chip *chip, port_state = ioread8(&idio24gpio->reg->ttl_in0_7); /* store acquired bits at respective bits array offset */ - bits[word_index] |= port_state << word_offset; + bits[word] |= port_state << offset; } return 0; @@ -295,58 +275,48 @@ static void idio_24_gpio_set_multiple(struct gpio_chip *chip, { struct idio_24_gpio *const idio24gpio = gpiochip_get_data(chip); size_t i; - unsigned long bits_offset; - unsigned long gpio_mask; - const unsigned int gpio_reg_size = 8; - const unsigned long port_mask = GENMASK(gpio_reg_size, 0); - unsigned long flags; - unsigned int out_state; + size_t word; + unsigned int offset; void __iomem *ports[] = { &idio24gpio->reg->out0_7, &idio24gpio->reg->out8_15, &idio24gpio->reg->out16_23 }; + const size_t num_ports = ARRAY_SIZE(ports) + 1; + unsigned int iomask; + unsigned int bitmask; + unsigned long flags; const unsigned long out_mode_mask = BIT(1); - const unsigned int ttl_offset = 48; - const size_t ttl_i = BIT_WORD(ttl_offset); - const unsigned int word_offset = ttl_offset % BITS_PER_LONG; - const unsigned long ttl_mask = (mask[ttl_i] >> word_offset) & port_mask; - const unsigned long ttl_bits = (bits[ttl_i] >> word_offset) & ttl_mask; - - /* set bits are processed 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; - - /* check if any set bits for current port */ - gpio_mask = (*mask >> bits_offset) & port_mask; - if (!gpio_mask) { - /* no set bits for this port so move on to next port */ + unsigned int out_state; + + for_each_set_clump(i, word, offset, mask, num_ports, 8) { + iomask = mask[word] >> offset; + bitmask = iomask & (bits[word] >> offset); + + raw_spin_lock_irqsave(&idio24gpio->lock, flags); + + /* read bits from current gpio port (port 6 is TTL GPIO) */ + if (i < 6) { + out_state = ioread8(ports[i]) & ~iomask; + } else if (ioread8(&idio24gpio->reg->ctl) & out_mode_mask) { + out_state = ioread8(&idio24gpio->reg->ttl_out0_7); + } else { + /* skip TTL GPIO if set for input */ + raw_spin_unlock_irqrestore(&idio24gpio->lock, flags); continue; } - raw_spin_lock_irqsave(&idio24gpio->lock, flags); + /* set requested bit states */ + out_state &= ~iomask; + out_state |= bitmask; - /* process output lines */ - out_state = ioread8(ports[i]) & ~gpio_mask; - out_state |= (*bits >> bits_offset) & gpio_mask; - iowrite8(out_state, ports[i]); + /* write bits for current gpio port (port 6 is TTL GPIO) */ + if (i < 6) + iowrite8(out_state, ports[i]); + else + iowrite8(out_state, &idio24gpio->reg->ttl_out0_7); raw_spin_unlock_irqrestore(&idio24gpio->lock, flags); } - - /* check if setting TTL lines and if they are in output mode */ - if (!ttl_mask || !(ioread8(&idio24gpio->reg->ctl) & out_mode_mask)) - return; - - /* handle TTL output */ - raw_spin_lock_irqsave(&idio24gpio->lock, flags); - - /* process output lines */ - out_state = ioread8(&idio24gpio->reg->ttl_out0_7) & ~ttl_mask; - out_state |= ttl_bits; - iowrite8(out_state, &idio24gpio->reg->ttl_out0_7); - - raw_spin_unlock_irqrestore(&idio24gpio->lock, flags); } static void idio_24_irq_ack(struct irq_data *data) -- 2.17.0