From patchwork Mon Aug 6 15:12:33 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 143519 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp3406797ljj; Mon, 6 Aug 2018 08:12:40 -0700 (PDT) X-Google-Smtp-Source: AAOMgpc9eiMuBM7Ays0xSkFSN4ssnx/GmiVXgbEoscAL9DQW26LsD8G4J4wRml2BKpaEnySrZof2 X-Received: by 2002:a62:d8c:: with SMTP id 12-v6mr17661834pfn.202.1533568360261; Mon, 06 Aug 2018 08:12:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533568360; cv=none; d=google.com; s=arc-20160816; b=0i38X7Ju+3J1F+GXaxMecMFc9d7TpjtQU4NVPhXLNvptsubAGoE7cDw+trlBZLBSMH DdQCaAtApAQwZuEcjPlR6jNQo7eEnot7c8FpWJU/QPijmrAVtZI6Kv6/p26JMmaaumYh 9rDnD2UyNFdioWUCCbm+E6JTuesQ4R/k3NwqmvohR0YxIxkv0s4PGT/bWRsQ9vQIL+Zd 6M7AOi184HkSr2dmGYJIVnBLS8Izo6SMqXF1H9OTgAeWSrYgOeF2lCeZQlkHi2rQCgrp bTU33OxjpJkp5MWI7zNenh3qoJLkZhVqhWRm8oHKx0N3hllo/mYcI0wR5ICK3oZZ1L+X IDzg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=15B2iT+sM4dN2rulgDY+BUahW8t0qy3MMotjY0RLqB0=; b=JdZabKowgbzDT6YQ+LWY1FcWYTLl2UjESmpmfrF1XWdS0GK04ODJ6xcbjQZXFbTSfy mL2qvlbHzQJ8Y663l4JRKA5I9o4Wj+ZYWM4/ka1cxfBeUDubtQ8h9CbllCjppNbLvsIr rYsmVnIwWMTdBxMl7mRCLjxGgl6no+lMQPlZYmaetzM+8fYxxt9tqST2NQuNAPJcm3es wnidMzowLQkkJrBOgvYBc/VEsRVVVvyF6sS4vR2LDceR/nWtlfcgWLG+TmlSgy/oHsa4 RTW6U3Ag+ff76yQe4tSdZ3dnDRI9Vr2OYoLLaQ9ldaKUwQMTiNoP45eY80Rmv+p4H3id Af5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=W45K1QTs; spf=pass (google.com: best guess record for domain of linux-gpio-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-gpio-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d11-v6si10319807pla.442.2018.08.06.08.12.40; Mon, 06 Aug 2018 08:12:40 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-gpio-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=W45K1QTs; spf=pass (google.com: best guess record for domain of linux-gpio-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-gpio-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732385AbeHFRWL (ORCPT + 5 others); Mon, 6 Aug 2018 13:22:11 -0400 Received: from mail-wm0-f68.google.com ([74.125.82.68]:35449 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732382AbeHFRWL (ORCPT ); Mon, 6 Aug 2018 13:22:11 -0400 Received: by mail-wm0-f68.google.com with SMTP id o18-v6so14822263wmc.0 for ; Mon, 06 Aug 2018 08:12:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=aD+9ZzPxRkmJjjGUKuvGDxd5lZ7g0DRV3RTmROi4C3k=; b=W45K1QTsiBmtFk9XHZQQRFwo7HlkPj+yj56qDeTcZ7jmUcs12FVuX1pG8+ZNEkEbaJ /7McC6aFIsKDaFL8g7ajuT8Q5yofFAdDZLTJf5uc6kGBvuKWB6doY3ahHQFpwM5EDPQI fq325P9L2275Vt6ynw2N6x95uxuYiqhdYU2KE= 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; bh=aD+9ZzPxRkmJjjGUKuvGDxd5lZ7g0DRV3RTmROi4C3k=; b=Sm0BUFbIZcSHyQjvCrzpkqmZ6KwVtRYtZfEKOA5g28IvXDAeqcHXgV8KlOFdV0x9fy eYiaZh4tgFZPuaeKZ3TVINwET/bhDxsQk86ygyCGCr/6Le+U6XfPMTXyfkGlVxGYwikM S/j+JiOkFrgzC4Q41PMMhq6S0+tfMoLfXsP1qyij43igUi/MGCsLU72+7LKE0p62DPVs +VKPGE7MzaQ1zNnOI8aw050Lf4kASF3HSrjlOrpta5TDPlcKiR9kWDgq+5tXkxRWq+vR Fj6CzNMJygHUsrD8Wd6+VqMr7q98e8XPgukGPX+k/RDhVhTvIM7QFMz0ioiBOSB086a2 U9nA== X-Gm-Message-State: AOUpUlHlz8rfLjCTLl6URl9F+2mnaQkyA93d6xuk5B2wdq7NUwjzGXd8 3rkZNNt/Q+7yubeK2+79VLedWxZ7BQE= X-Received: by 2002:a1c:5e48:: with SMTP id s69-v6mr11604922wmb.19.1533568357507; Mon, 06 Aug 2018 08:12:37 -0700 (PDT) Received: from localhost.localdomain (catv-89-135-96-219.catv.broadband.hu. [89.135.96.219]) by smtp.gmail.com with ESMTPSA id s1-v6sm7958121wrt.82.2018.08.06.08.12.35 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 06 Aug 2018 08:12:36 -0700 (PDT) From: Linus Walleij To: linux-gpio@vger.kernel.org Cc: Linus Walleij , linux-snps-arc@lists.infradead.org, Christian Ruppert Subject: [PATCH 2/2] gpio: tb10x: Use GENERIC_GPIO Date: Mon, 6 Aug 2018 17:12:33 +0200 Message-Id: <20180806151233.2408-1-linus.walleij@linaro.org> X-Mailer: git-send-email 2.17.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Instead of open coding logic for reading and writing GPIO lines, use the generic GPIO library. Also switch to using the spinlock from the generic GPIO to protect the registers. Cc: linux-snps-arc@lists.infradead.org Cc: Christian Ruppert Signed-off-by: Linus Walleij --- drivers/gpio/Kconfig | 1 + drivers/gpio/gpio-tb10x.c | 96 ++++++++++++--------------------------- 2 files changed, 31 insertions(+), 66 deletions(-) -- 2.17.0 -- To unsubscribe from this list: send the line "unsubscribe linux-gpio" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Acked-by: Christian Ruppert diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig index 7429b30e61b0..d351548d0257 100644 --- a/drivers/gpio/Kconfig +++ b/drivers/gpio/Kconfig @@ -480,6 +480,7 @@ config GPIO_SYSCON config GPIO_TB10X bool + select GPIO_GENERIC select GENERIC_IRQ_CHIP select OF_GPIO diff --git a/drivers/gpio/gpio-tb10x.c b/drivers/gpio/gpio-tb10x.c index 422b0ac5a9de..d5e5d19f4c0a 100644 --- a/drivers/gpio/gpio-tb10x.c +++ b/drivers/gpio/gpio-tb10x.c @@ -45,14 +45,12 @@ /** - * @spinlock: used for atomic read/modify/write of registers * @base: register base address * @domain: IRQ domain of GPIO generated interrupts managed by this controller * @irq: Interrupt line of parent interrupt controller * @gc: gpio_chip structure associated to this GPIO controller */ struct tb10x_gpio { - spinlock_t spinlock; void __iomem *base; struct irq_domain *domain; int irq; @@ -76,60 +74,14 @@ static inline void tb10x_set_bits(struct tb10x_gpio *gpio, unsigned int offs, u32 r; unsigned long flags; - spin_lock_irqsave(&gpio->spinlock, flags); + spin_lock_irqsave(&gpio->gc.bgpio_lock, flags); r = tb10x_reg_read(gpio, offs); r = (r & ~mask) | (val & mask); tb10x_reg_write(gpio, offs, r); - spin_unlock_irqrestore(&gpio->spinlock, flags); -} - -static int tb10x_gpio_direction_in(struct gpio_chip *chip, unsigned offset) -{ - struct tb10x_gpio *tb10x_gpio = gpiochip_get_data(chip); - int mask = BIT(offset); - int val = TB10X_GPIO_DIR_IN << offset; - - tb10x_set_bits(tb10x_gpio, OFFSET_TO_REG_DDR, mask, val); - - return 0; -} - -static int tb10x_gpio_get(struct gpio_chip *chip, unsigned offset) -{ - struct tb10x_gpio *tb10x_gpio = gpiochip_get_data(chip); - int val; - - val = tb10x_reg_read(tb10x_gpio, OFFSET_TO_REG_DATA); - - if (val & BIT(offset)) - return 1; - else - return 0; -} - -static void tb10x_gpio_set(struct gpio_chip *chip, unsigned offset, int value) -{ - struct tb10x_gpio *tb10x_gpio = gpiochip_get_data(chip); - int mask = BIT(offset); - int val = value << offset; - - tb10x_set_bits(tb10x_gpio, OFFSET_TO_REG_DATA, mask, val); -} - -static int tb10x_gpio_direction_out(struct gpio_chip *chip, - unsigned offset, int value) -{ - struct tb10x_gpio *tb10x_gpio = gpiochip_get_data(chip); - int mask = BIT(offset); - int val = TB10X_GPIO_DIR_OUT << offset; - - tb10x_gpio_set(chip, offset, value); - tb10x_set_bits(tb10x_gpio, OFFSET_TO_REG_DDR, mask, val); - - return 0; + spin_unlock_irqrestore(&gpio->gc.bgpio_lock, flags); } static int tb10x_gpio_to_irq(struct gpio_chip *chip, unsigned offset) @@ -184,8 +136,6 @@ static int tb10x_gpio_probe(struct platform_device *pdev) if (tb10x_gpio == NULL) return -ENOMEM; - spin_lock_init(&tb10x_gpio->spinlock); - mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); tb10x_gpio->base = devm_ioremap_resource(dev, mem); if (IS_ERR(tb10x_gpio->base)) @@ -196,20 +146,34 @@ static int tb10x_gpio_probe(struct platform_device *pdev) if (!tb10x_gpio->gc.label) return -ENOMEM; - tb10x_gpio->gc.parent = &pdev->dev; - tb10x_gpio->gc.owner = THIS_MODULE; - tb10x_gpio->gc.direction_input = tb10x_gpio_direction_in; - tb10x_gpio->gc.get = tb10x_gpio_get; - tb10x_gpio->gc.direction_output = tb10x_gpio_direction_out; - tb10x_gpio->gc.set = tb10x_gpio_set; - tb10x_gpio->gc.request = gpiochip_generic_request; - tb10x_gpio->gc.free = gpiochip_generic_free; - tb10x_gpio->gc.base = -1; - tb10x_gpio->gc.ngpio = ngpio; - tb10x_gpio->gc.can_sleep = false; - - - ret = devm_gpiochip_add_data(&pdev->dev, &tb10x_gpio->gc, tb10x_gpio); + /* + * Initialize generic GPIO with one single register for reading and setting + * the lines, no special set or clear registers and a data direction register + * wher 1 means "output". + */ + ret = bgpio_init(&tb10x_gpio->gc, dev, 4, + tb10x_gpio->base + OFFSET_TO_REG_DATA, + NULL, + NULL, + tb10x_gpio->base + OFFSET_TO_REG_DDR, + NULL, + 0); + if (ret) { + dev_err(dev, "unable to init generic GPIO\n"); + return ret; + } + tb10x_gpio->gc.base = -1; + tb10x_gpio->gc.parent = dev; + tb10x_gpio->gc.owner = THIS_MODULE; + /* + * ngpio is set by bgpio_init() but we override it, this .request() + * callback also overrides the one set up by generic GPIO. + */ + tb10x_gpio->gc.ngpio = ngpio; + tb10x_gpio->gc.request = gpiochip_generic_request; + tb10x_gpio->gc.free = gpiochip_generic_free; + + ret = devm_gpiochip_add_data(dev, &tb10x_gpio->gc, tb10x_gpio); if (ret < 0) { dev_err(dev, "Could not add gpiochip.\n"); return ret;