Received: by 10.192.165.148 with SMTP id m20csp4306136imm; Tue, 8 May 2018 06:29:22 -0700 (PDT) X-Google-Smtp-Source: AB8JxZppw7NpfCfA3q5iQAHFSeulZeBYzWGYnYawwIU7lx4kFQe9Vz6oKucvIlJsCN8gst8/hDDQ X-Received: by 2002:a17:902:758a:: with SMTP id j10-v6mr41559491pll.11.1525786162877; Tue, 08 May 2018 06:29:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525786162; cv=none; d=google.com; s=arc-20160816; b=QZrs/Ig9uEPb76Jz9QecsKxsNcGR63mL8FjvoA+o4h6Jx4snCP/1jvIWp+UcVatLhT jdo6A/osqmDfs/rjYY8/LW8cKnvU3wmAZLb9lAGvINxRv/pHgjyv4SZrD0OkVCEekbtB z9buLE9YR20Waig7pWf0wttvv5yezR+WM7pxTgaiyMj32F4b67iTvJuHL8VLwMiQI3xC OoP53RF74xfhy/ukP/mYctwDVkl/awTO9ToocVqP5Ynz8mmblgxjvQNXWDg7YCjiuFA1 LZg5HNS4TcrbKiAghSC6R/Exyt7JM7mNYcHD7sjGMIXg39Xs2CSz1nyaP/sTMG4wc8o7 mjtw== 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=HI9d10fTvygtx/hnUrehTnJP2FXBcK1M2aXhU9jJrE8=; b=oIglInnXUdzdwAHOFSNxc9CehtSAeLADjZzFaCPUH9qd6H5jhjLy64mKjrEo/aVsFj 6Iy17KFehUtGvPybmS7kcRlmi2slMCd0SMiNcI6zrVE2YrPKcGGRGFK7s8gwelFhbGOF eDAZrv/aKuGKoO+JxCrCVzptZgRsDBQUZvnWT/Ecl4xgxtKCYuw3J+22gZoAakue2Ls5 WyOWTkZsj3HwVOnlDTW/QLoZLvrt4dlt19+8aSwFtIxkZCSFt5cAZpz1vrmJwyhvl5A1 AJn1mR0L8h1wn1tT9FiMoG1NMU2ADtiHw/d1RnC9+N25qWxL4bRNZRB0gchQ/OMB38qi EvcQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=T1GI9IZ5; 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 g6-v6si20062099pgr.72.2018.05.08.06.29.08; Tue, 08 May 2018 06:29:22 -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=T1GI9IZ5; 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 S932267AbeEHN21 (ORCPT + 99 others); Tue, 8 May 2018 09:28:27 -0400 Received: from mail-yb0-f194.google.com ([209.85.213.194]:34388 "EHLO mail-yb0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932168AbeEHN2Z (ORCPT ); Tue, 8 May 2018 09:28:25 -0400 Received: by mail-yb0-f194.google.com with SMTP id b14-v6so11143510ybk.1; Tue, 08 May 2018 06:28:24 -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=HI9d10fTvygtx/hnUrehTnJP2FXBcK1M2aXhU9jJrE8=; b=T1GI9IZ5jFzJ0UHHFbPt1SVncE1t7J/NfUzbEH25iNrZbFofdYWVPf+jQOIGJjE7Qg ZivXrbfnbNJHSbYVDI0WENloLFKb2bamiAIf/XLJLycBUA6+yK+RSJsZJRGB/j6lD67P H3/GxsTNdrJuJBXUVOZw9UONVg3n+sp77rPrJXtm169vD+D5JGgcERV3gZYGkyzloZYC uTcHq5faqia27y/Kv/sjA4lgsNXcLqdf3hqGyG8cqDfm/YJ90TNaCLWk11J8EQ7q/BbA p1MtYmsPeKIoreSkup26t/TBsXtXS6VKP9ON/G04JMWxyFJqMd3iUu1+RC+h8lnBFGpj AvHA== 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=HI9d10fTvygtx/hnUrehTnJP2FXBcK1M2aXhU9jJrE8=; b=e7QPZ3aT3sABH+J2UlxwODZG5ZrIuZ+laPde/YqDbf9tWkcQIjs42dg6wOmQTqlJ1H wzae/RalDb1PajhHVbFOUW1JP3CWMTR/YFlbW+uoYnCi+dkAHJ+0PGMOvhxItBZg5PJS An5T2XUAX/tp4hgbFUPyDM2cLbmDfLeLtQdHeq/+KAvfERlDwu3dRXnjaUAJws+e3W/S xbO6jc6I635ZvYrN0hUKlRk/fxvTx8eSIZo230rEjs/UqRiKDL3x404tm1jQzH56VGoE LNxmCXaZzTbph7YZ/hRU0aD6SxBB6jlNGGbziAPOZI9Om+TkhIdh8BM+XQMUue30VGYx RpnA== X-Gm-Message-State: ALQs6tAnrDrv5QjbbOfYnGQ1k/KEPgEF7rO0FJF358cg+slRHTd8hqkL 80Xm1BBI4XlS7JG7G+Q0swQ= X-Received: by 2002:a25:41c6:: with SMTP id o189-v6mr25165175yba.125.1525786104349; Tue, 08 May 2018 06:28:24 -0700 (PDT) Received: from localhost ([72.188.97.40]) by smtp.gmail.com with ESMTPSA id x125-v6sm11261958ywf.106.2018.05.08.06.28.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 May 2018 06:28:24 -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 v2 7/7] gpio: pcie-idio-24: Utilize for_each_set_port_word macro Date: Tue, 8 May 2018 09:28:17 -0400 Message-Id: <57b9e48b8aa9d80c24c6e21bf3d0383deb95e1e9.1525785532.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 boilerplate code in get_multiple/set_multiple callbacks with for_each_set_port_word 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..a0c81724ce4d 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_port_word(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_port_word(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