Received: by 10.192.165.148 with SMTP id m20csp3222247imm; Mon, 7 May 2018 08:38:15 -0700 (PDT) X-Google-Smtp-Source: AB8JxZoPpL4BiMykcGor2UIqAYygnFuXaDcHZGYJJ5B/7pizNzVKO/SRIoY4WhsG8z+Wo9fWlY40 X-Received: by 2002:a6b:c083:: with SMTP id q125-v6mr11800249iof.42.1525707495477; Mon, 07 May 2018 08:38:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525707495; cv=none; d=google.com; s=arc-20160816; b=hewiATu87Zts/ysUEkeKud1S9+lLS8KdsqW3w/Kho4WnTf6/5HFP2Hxxu2T1gJu78d 1TJmscbo8gPhPKV7hLtH0S0kz97JGh+cJteX2AMaKOUPgOGGPk0dzLdsRA1jBs/FYTEE hE3qdquHw143pYmXc5odgo4d+oyYcyFcR3Mp5JAIAAKfoWzeqgfxb0aQIYFfHpUa2b2W kfGbWtYLSHY3InoduRCiJ06/NAN+lvg3gRLlS5C6y6M7KZSO43ApTSNdT30uxdTqeyWh msiEwC8CNMG3jtxudRJCB159shbyqGAYkSm7csUSaoJXVIFdki72n3cPQGf9+z5wnOm9 0nOw== 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=BUjJB9HrzBWsfQ61bErge8zObYCKdVlVgVzDbcwQbmn4G8mF/Ik4bU2AySRpjmi33a 3YlDbjY4/bImRwhqpDrfUJaYBnqwCmbvrcF2+Aa34eWSVs0aRQ5NY3ihnxrj5rRj2SSQ qaDUvahRNbHQ0GGe3V9FG1leHCpa4c+bppyrDD0OCngg/G6BNiAc+xBsWX4kGM6ZTLcg 5YyHlD5S9wT8OTbBbr0PQVowDx+TcrKMHpy5CFeBlsRE8l9mxYxnI3omkPslBBAxQRDT lpxFO6KZTr8PoKnEl7zz3/sbwZYVYQL/HyvBSroGMeUkNKpcKX5i4rTE7CpamRxnc06z yLUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Nt5QuwR1; 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 10-v6si6773375ion.87.2018.05.07.08.38.02; Mon, 07 May 2018 08:38:15 -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=Nt5QuwR1; 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 S1752658AbeEGPhD (ORCPT + 99 others); Mon, 7 May 2018 11:37:03 -0400 Received: from mail-yw0-f194.google.com ([209.85.161.194]:38573 "EHLO mail-yw0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752051AbeEGPhB (ORCPT ); Mon, 7 May 2018 11:37:01 -0400 Received: by mail-yw0-f194.google.com with SMTP id y123-v6so8729421ywa.5; Mon, 07 May 2018 08:37:00 -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=Nt5QuwR1zMJyy0dYLeUfJbOK4G8Gy63AeSeRT0ZXiI2N11VyQBsO8J9NTFQFmBKuf5 DC9JpFP9Sd1s4Vmh139jhkvPbYnTb8V60PUCTQ+TH2H0BhExAvmEYxirPc7gM+4iT73K IuU71/OsifG34hJNfComTioBk4gdnRY3JIbWAq39PTGQssbfO2Tr10Sg71p8ydZIPXaw l2dQ6+yATkEFt79OS4SXbDBT4SegV5HuZgAl763w/ZjmschzvpLFVlgRqiCd3W/SFrBz 90cUGGFvqO10KnfiE2TsJEXCzC2ZscqcBuapOXkf88VNbFN7cxdSacT++gvbFL2sEa6L MPcQ== 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=WkMKIdDyamHBgK2eOq1DC1PO8PBWC2H94U45vCWMCOWrsMzwjTEj5dG1G3iCQSy/lF yNzrrEWY82NIde8zR/uHs6IUiHTY4eFHArqVoW+vmf3jVvgQRv8JAh42dGSBGyMXMeaA O6+d9Wi3DGUevqG8zN5Vk66brDkOFkLQPNQyX5GVLSHAIoesagJ6AbK+zz/03N6tItb6 EORXDYGGo6hQHqzGqAJiM7hV1dsHms183MnAXbiysiQzzR3cqQldAL6kmk2wzOLTA2iu ak6zimXMq57DN6Qo8sLENYK2iF/whXzG14RVLfY6pv7gZS02XrANXvCM3pIYn1IaO+85 gSEA== X-Gm-Message-State: ALQs6tAyzOid/KS6MGE5amZq3tDKglDqJWU9Ik2GqedLmJv7sdQNs0iD Td/MPsnPF3gQ1XjFku84fApOHw== X-Received: by 2002:a81:a4cd:: with SMTP id b196-v6mr20419400ywh.137.1525707420339; Mon, 07 May 2018 08:37:00 -0700 (PDT) Received: from localhost ([72.188.97.40]) by smtp.gmail.com with ESMTPSA id o133-v6sm4442448ywd.12.2018.05.07.08.36.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 07 May 2018 08:37:00 -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 7/7] gpio: pcie-idio-24: Utilize for_each_set_port_word macro Date: Mon, 7 May 2018 11:36:54 -0400 Message-Id: <8467a738e42fcbbe3da0a23d5bccd61b47b561c6.1525704095.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