Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp3955159pxb; Tue, 10 Nov 2020 04:36:23 -0800 (PST) X-Google-Smtp-Source: ABdhPJxRpOxD0yBiaGQcqynCvCEBHqVyqXgizKqHZp1usA7dDMVwFTbnfJzAkj66oDrnts1ME/gm X-Received: by 2002:a05:6402:1153:: with SMTP id g19mr864338edw.312.1605011783727; Tue, 10 Nov 2020 04:36:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605011783; cv=none; d=google.com; s=arc-20160816; b=JG+EquXy0+mR0r42RIDoYKKCY59cZIrWwcXscMIXMH2+Zcu5vuvzvoSvyalXQ56s1D sTd6JZWXx207icCJLa5uptNC96X/0CGZhr8LbP+FSerep3+HEVHznytprVO32ukPbbFO UGBLoh6AxYM+TLZkXi7oT9vsiSwgCzxphFvzSSXwr4GTF/l9pF5hKlwFFt0fC/dNNF0a 2yprvSfSpYBf/X6dMYlBsGY2R/T3HalbmPv7NiDpmUqZoMgpyjARWS0aLdBmsPvVUL/m vr4jyc8IipOqufU3SeiLcsSWnqDAd+xapA2HitWHgX0HgZPoGn/ndfh0uNLj0jX9XvDn qF4A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=xXkyfLrBZRJrErHtr2bqBIYeiQvyVwQuf/DgPi3APEY=; b=D90R3PqTbeTQKRh234XV1llSdLMq9RQi2OzNESicQKEKD96+hmzgqLsLb+ko/Bm1Yq kxYFHMWkPUCYEJhbkWE28p1Zhp/W9TgVmv7zLpemMdB5jcs5tmf+KDBGTjqqswsCYZ2X JZvOnviaVwD+Qj36EYCNl7IPsLiN5ESesr/QPDMKIfdLtbR7bFd5CIEVuCK9qJfHXmfJ vhHu1rYBkzQKHQVEX3lm0TWsxTU5fzyPuoF2Q3Oiw7dQ9mupt0xh8hOkrOieTsx/sesN ai6Ei+XG1GiOJitTjNyoY1x5h5/uOPpyEIPXOyeylIb/vyCDeG5HJvJNRcB6y0xEaNZI hqVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b="U/Y/yQOE"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m14si9249760ejr.448.2020.11.10.04.35.59; Tue, 10 Nov 2020 04:36:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b="U/Y/yQOE"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731414AbgKJMe2 (ORCPT + 99 others); Tue, 10 Nov 2020 07:34:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42486 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730540AbgKJMeU (ORCPT ); Tue, 10 Nov 2020 07:34:20 -0500 Received: from mail-wm1-x344.google.com (mail-wm1-x344.google.com [IPv6:2a00:1450:4864:20::344]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F22C5C0613CF for ; Tue, 10 Nov 2020 04:34:19 -0800 (PST) Received: by mail-wm1-x344.google.com with SMTP id h2so2848792wmm.0 for ; Tue, 10 Nov 2020 04:34:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=xXkyfLrBZRJrErHtr2bqBIYeiQvyVwQuf/DgPi3APEY=; b=U/Y/yQOEbgnTM3Jkw110ASaMkq28gpx/BQ87lY59I6lLOL/usnPyX0oZwamiq+YwIi WnkARFstsDlwmY6TAKKwF3k2l1srtFRk5HXfLdVr+R4Uk8D0JxCUJu7B7ikUsBJIRCUR PnJ0SF2ypLhcng6QrmoqnFg6DJE6AaC2/ntwTOWjjNaxhVn+DJeqAsbSMcatZvtaCkoj DaRfnXT+taNy/VZXtnhOv/cCfc/drIW8QXNny+u3WwDa1SdgYJ4SwuiVV2qGSUIXqcTf 84DSUVPCaJW465W1QscHsEuPtYd9kEkqzTI2mRpkqeWLx3OfN0apONuNA8O0irCstaVm kBcA== 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:mime-version:content-transfer-encoding; bh=xXkyfLrBZRJrErHtr2bqBIYeiQvyVwQuf/DgPi3APEY=; b=TpRoqArbcfBUmjVUcDXjiMet9F8ykJ4VLjsIEGwxIDzpgVQIMgIg/w/5NUjtSAGqrQ kUiXTWleFlwF/HUYm4vxsZF0ugGd2+zlGrBzkeDYnaV8LGCnOPsl2Mbu65TgumK7FRaf cltcHvDXhcCphG2PtezjBNaXHj+il9B+qW18s55NAT97pziaTtY1fpxAtVZAhMgBZHun pIFgzNxSvx5bRh0XHRvEQb5uGkD1yKKeLKAioHgQlKGE/Q7zWeOpFIch+smLUlwJHEqD 40vxMrjYz5zU7WO26qOH/nr1vtQoWJOvfFJL1yGVJc0Su2UM17nCKtUIgbESoC6Ngdxz QBlg== X-Gm-Message-State: AOAM533yJq0sAoX5CLMlfPNG3L8F3dhRexQvU+O1OnpXveXJ9r94SAGt wlFPMvO1SE7lt48XNWn2VgQltQ== X-Received: by 2002:a05:600c:4147:: with SMTP id h7mr4318969wmm.146.1605011658685; Tue, 10 Nov 2020 04:34:18 -0800 (PST) Received: from debian-brgl.home (amarseille-656-1-4-167.w90-8.abo.wanadoo.fr. [90.8.158.167]) by smtp.gmail.com with ESMTPSA id s8sm12942217wrn.33.2020.11.10.04.34.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Nov 2020 04:34:18 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Andy Shevchenko , Jan Kiszka , David Laight Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 6/7] gpio: exar: switch to using regmap Date: Tue, 10 Nov 2020 13:34:05 +0100 Message-Id: <20201110123406.3261-7-brgl@bgdev.pl> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20201110123406.3261-1-brgl@bgdev.pl> References: <20201110123406.3261-1-brgl@bgdev.pl> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Bartosz Golaszewski We can simplify the code in gpio-exar by using regmap. This allows us to drop the mutex (regmap provides its own locking) and we can also reuse regmap's bit operations instead of implementing our own update function. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/Kconfig | 1 + drivers/gpio/gpio-exar.c | 90 +++++++++++++++++----------------------- 2 files changed, 38 insertions(+), 53 deletions(-) diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig index 5d4de5cd6759..253a61ec9645 100644 --- a/drivers/gpio/Kconfig +++ b/drivers/gpio/Kconfig @@ -255,6 +255,7 @@ config GPIO_EP93XX config GPIO_EXAR tristate "Support for GPIO pins on XR17V352/354/358" depends on SERIAL_8250_EXAR + select REGMAP_MMIO help Selecting this option will enable handling of GPIO pins present on Exar XR17V352/354/358 chips. diff --git a/drivers/gpio/gpio-exar.c b/drivers/gpio/gpio-exar.c index 28b0b4b5fa35..a2d324c513f8 100644 --- a/drivers/gpio/gpio-exar.c +++ b/drivers/gpio/gpio-exar.c @@ -14,6 +14,7 @@ #include #include #include +#include #define EXAR_OFFSET_MPIOLVL_LO 0x90 #define EXAR_OFFSET_MPIOSEL_LO 0x93 @@ -26,9 +27,8 @@ static DEFINE_IDA(ida_index); struct exar_gpio_chip { struct gpio_chip gpio_chip; - struct mutex lock; + struct regmap *regs; int index; - void __iomem *regs; char name[20]; unsigned int first_pin; }; @@ -53,51 +53,13 @@ exar_offset_to_bit(struct exar_gpio_chip *exar_gpio, unsigned int offset) return (offset + exar_gpio->first_pin) % 8; } -static void exar_update(struct gpio_chip *chip, unsigned int reg, int val, - unsigned int offset) -{ - struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip); - int temp; - - mutex_lock(&exar_gpio->lock); - temp = readb(exar_gpio->regs + reg); - temp &= ~BIT(offset); - if (val) - temp |= BIT(offset); - writeb(temp, exar_gpio->regs + reg); - mutex_unlock(&exar_gpio->lock); -} - -static int exar_set_direction(struct gpio_chip *chip, int direction, - unsigned int offset) -{ - struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip); - unsigned int addr = exar_offset_to_sel_addr(exar_gpio, offset); - unsigned int bit = exar_offset_to_bit(exar_gpio, offset); - - exar_update(chip, addr, direction, bit); - return 0; -} - -static int exar_get(struct gpio_chip *chip, unsigned int reg) -{ - struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip); - int value; - - mutex_lock(&exar_gpio->lock); - value = readb(exar_gpio->regs + reg); - mutex_unlock(&exar_gpio->lock); - - return value; -} - static int exar_get_direction(struct gpio_chip *chip, unsigned int offset) { struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip); unsigned int addr = exar_offset_to_sel_addr(exar_gpio, offset); unsigned int bit = exar_offset_to_bit(exar_gpio, offset); - if (exar_get(chip, addr) & BIT(bit)) + if (regmap_test_bits(exar_gpio->regs, addr, BIT(bit))) return GPIO_LINE_DIRECTION_IN; return GPIO_LINE_DIRECTION_OUT; @@ -109,7 +71,7 @@ static int exar_get_value(struct gpio_chip *chip, unsigned int offset) unsigned int addr = exar_offset_to_lvl_addr(exar_gpio, offset); unsigned int bit = exar_offset_to_bit(exar_gpio, offset); - return !!(exar_get(chip, addr) & BIT(bit)); + return !!(regmap_test_bits(exar_gpio->regs, addr, BIT(bit))); } static void exar_set_value(struct gpio_chip *chip, unsigned int offset, @@ -119,21 +81,42 @@ static void exar_set_value(struct gpio_chip *chip, unsigned int offset, unsigned int addr = exar_offset_to_lvl_addr(exar_gpio, offset); unsigned int bit = exar_offset_to_bit(exar_gpio, offset); - exar_update(chip, addr, value, bit); + if (value) + regmap_set_bits(exar_gpio->regs, addr, BIT(bit)); + else + regmap_clear_bits(exar_gpio->regs, addr, BIT(bit)); } static int exar_direction_output(struct gpio_chip *chip, unsigned int offset, int value) { + struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip); + unsigned int addr = exar_offset_to_sel_addr(exar_gpio, offset); + unsigned int bit = exar_offset_to_bit(exar_gpio, offset); + exar_set_value(chip, offset, value); - return exar_set_direction(chip, 0, offset); + regmap_clear_bits(exar_gpio->regs, addr, BIT(bit)); + + return 0; } static int exar_direction_input(struct gpio_chip *chip, unsigned int offset) { - return exar_set_direction(chip, 1, offset); + struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip); + unsigned int addr = exar_offset_to_sel_addr(exar_gpio, offset); + unsigned int bit = exar_offset_to_bit(exar_gpio, offset); + + regmap_set_bits(exar_gpio->regs, addr, BIT(bit)); + + return 0; } +static const struct regmap_config exar_regmap_config = { + .name = "exar-gpio", + .reg_bits = 8, + .val_bits = 8, +}; + static int gpio_exar_probe(struct platform_device *pdev) { struct device *dev = &pdev->dev; @@ -163,13 +146,17 @@ static int gpio_exar_probe(struct platform_device *pdev) if (!exar_gpio) return -ENOMEM; - mutex_init(&exar_gpio->lock); + /* + * We don't need to check the return values of mmio regmap operations (unless + * the regmap has a clock attached which is not the case here). + */ + exar_gpio->regs = devm_regmap_init_mmio(dev, p, &exar_regmap_config); + if (IS_ERR(exar_gpio->regs)) + return PTR_ERR(exar_gpio->regs); index = ida_alloc(&ida_index, GFP_KERNEL); - if (index < 0) { - ret = index; - goto err_mutex_destroy; - } + if (index < 0) + return index; sprintf(exar_gpio->name, "exar_gpio%d", index); exar_gpio->gpio_chip.label = exar_gpio->name; @@ -195,8 +182,6 @@ static int gpio_exar_probe(struct platform_device *pdev) err_destroy: ida_free(&ida_index, index); -err_mutex_destroy: - mutex_destroy(&exar_gpio->lock); return ret; } @@ -205,7 +190,6 @@ static int gpio_exar_remove(struct platform_device *pdev) struct exar_gpio_chip *exar_gpio = platform_get_drvdata(pdev); ida_free(&ida_index, exar_gpio->index); - mutex_destroy(&exar_gpio->lock); return 0; } -- 2.29.1