From patchwork Sun Jan 24 06:02:32 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Youngmin Nam X-Patchwork-Id: 8098261 Return-Path: X-Original-To: patchwork-linux-samsung-soc@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 67DCE9F6DA for ; Sun, 24 Jan 2016 06:04:08 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 7BD1D20394 for ; Sun, 24 Jan 2016 06:04:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 5117920380 for ; Sun, 24 Jan 2016 06:04:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751032AbcAXGEF (ORCPT ); Sun, 24 Jan 2016 01:04:05 -0500 Received: from mail-pa0-f65.google.com ([209.85.220.65]:36008 "EHLO mail-pa0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750892AbcAXGEE (ORCPT ); Sun, 24 Jan 2016 01:04:04 -0500 Received: by mail-pa0-f65.google.com with SMTP id a20so5100606pag.3 for ; Sat, 23 Jan 2016 22:04:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=CLdZugW+4nNIYTixtXjjnsOhn1bHsNBepv6ZYtM38lk=; b=K29MCDdigCkXBnTc6BES7H02v015BRaSna6WFbS9zvaWTUqxttlc7OkhCg16Wgn9fJ W1IqmM/Gu8hc56GN0UdkW9Td+4NTnSKWeTZ6DRs8O0BG+fSK6f0nBu/3r2LmOwfIt6g1 UJvEpEh5hY2SgyPkrbMbexh+vooQEhcaEyNilLc21xZgvliRChN0rj/y0jMBFp0f4tle B54ToPLt/2QgNfwpJw56Y941Ufi9PcJVL1tLNYnqKA1GUWOG3/2Wkrb1dzW3MCa065tM L/A9qqbf1HvNZVkmuRiHDXKTlHhpAAbWl/dZtcvpjZbmiHP+ElRiCLVj/Q+go7ag9Vy+ teZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=CLdZugW+4nNIYTixtXjjnsOhn1bHsNBepv6ZYtM38lk=; b=HV+iZqHYe2dUpXBiKf9OrW7CScDznbsR9mfl6VTMmfRIw8nIqEW427ZNKhXiMpi2yY NTTHhTm1bZs+AIzRpzp5aor3zpSZ02tOWqLKGJ1uMI78QERP/xb4Yqdw0p3KihC+Xjzs 76Le4IEFYKi2++bG4DbVO93XiZ5pKDM33joq21rEHkh8hPeAZUsimwfWhfNPMPnf0Btn iaz7Mul01an4FAOdI8OLj9WJUBakglQUlwa3lPlu9kljUf7vwpAf6wejku6F5qpgGlGc p6iEM4fPY31wHTCnHN96FG5FQHBlIAGUJzdtXQzUnbT6IzGYoBJW+JpCSckPTzUaNBYT 6Dgg== X-Gm-Message-State: AG10YORy0QiBlRqFRQXSshbUhi1ltwILz3thd0j25qEYB1LYqA+g6QgF3hE7/2mqySN2Mg== X-Received: by 10.66.235.202 with SMTP id uo10mr16482371pac.63.1453615443244; Sat, 23 Jan 2016 22:04:03 -0800 (PST) Received: from youngmin ([101.235.121.100]) by smtp.gmail.com with ESMTPSA id 63sm19747321pfo.67.2016.01.23.22.04.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 23 Jan 2016 22:04:02 -0800 (PST) Received: by youngmin (Postfix, from userid 1000) id 468CF1601A3; Sun, 24 Jan 2016 15:03:59 +0900 (KST) From: Youngmin Nam To: tomasz.figa@gmail.com, k.kozlowski@samsung.com, linus.walleij@linaro.org, kgene@kernel.org, thomas.ab@samsung.com Cc: linux-samsung-soc@vger.kernel.org, Youngmin Nam Subject: [PATCH v2] pinctrl: samsung: Fixes samsung_gpio_direction_in/output releated with spinlock Date: Sun, 24 Jan 2016 15:02:32 +0900 Message-Id: <1453615352-9748-1-git-send-email-ym0914@gmail.com> X-Mailer: git-send-email 2.7.0.25.gfc10eb5 Sender: linux-samsung-soc-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-samsung-soc@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Previously, samsung_gpio_drection_in/output function were not covered with one spinlock. For example, samsung_gpio_direction_output function consists of two functions. 1. samsung_gpio_set 2. samsung_gpio_set_direction When 2 CPUs try to control the same gpio pin heavily, (situation like i2c control with gpio emulation) This situation can cause below problem. CPU 0 | CPU1 | samsung_gpio_direction_output | samsung_gpio_set(pin A as 1) | samsung_gpio_direction_output | samsung_gpio_set(pin A as 0) samsung_gpio_set_direction | | samsung_gpio_set_direction The initial value of pin A will be set as 0 while we wanted to set pin A as 1. This patch modifies samsung_gpio_direction_in/output function to be done in one spinlock to fix race condition. Additionally, the new samsung_gpio_set_value was added to implement gpio set callback(samsung_gpio_set) with spinlock using this function. Signed-off-by: Youngmin Nam Acked-by: Tomasz Figa --- drivers/pinctrl/samsung/pinctrl-samsung.c | 48 ++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/drivers/pinctrl/samsung/pinctrl-samsung.c b/drivers/pinctrl/samsung/pinctrl-samsung.c index 48294e7..c5828b8 100644 --- a/drivers/pinctrl/samsung/pinctrl-samsung.c +++ b/drivers/pinctrl/samsung/pinctrl-samsung.c @@ -519,25 +519,35 @@ static const struct pinconf_ops samsung_pinconf_ops = { .pin_config_group_set = samsung_pinconf_group_set, }; -/* gpiolib gpio_set callback function */ -static void samsung_gpio_set(struct gpio_chip *gc, unsigned offset, int value) +/* + * The samsung_gpio_set_vlaue() should be called with "bank->slock" held + * to avoid race condition. + */ +static void samsung_gpio_set_value(struct gpio_chip *gc, + unsigned offset, int value) { struct samsung_pin_bank *bank = gc_to_pin_bank(gc); const struct samsung_pin_bank_type *type = bank->type; - unsigned long flags; void __iomem *reg; u32 data; reg = bank->drvdata->virt_base + bank->pctl_offset; - spin_lock_irqsave(&bank->slock, flags); - data = readl(reg + type->reg_offset[PINCFG_TYPE_DAT]); data &= ~(1 << offset); if (value) data |= 1 << offset; writel(data, reg + type->reg_offset[PINCFG_TYPE_DAT]); +} + +/* gpiolib gpio_set callback function */ +static void samsung_gpio_set(struct gpio_chip *gc, unsigned offset, int value) +{ + struct samsung_pin_bank *bank = gc_to_pin_bank(gc); + unsigned long flags; + spin_lock_irqsave(&bank->slock, flags); + samsung_gpio_set_value(gc, offset, value); spin_unlock_irqrestore(&bank->slock, flags); } @@ -558,6 +568,8 @@ static int samsung_gpio_get(struct gpio_chip *gc, unsigned offset) } /* + * The samsung_gpio_set_direction() should be called with "bank->slock" held + * to avoid race condition. * The calls to gpio_direction_output() and gpio_direction_input() * leads to this function call. */ @@ -569,7 +581,6 @@ static int samsung_gpio_set_direction(struct gpio_chip *gc, struct samsung_pinctrl_drv_data *drvdata; void __iomem *reg; u32 data, mask, shift; - unsigned long flags; bank = gc_to_pin_bank(gc); type = bank->type; @@ -586,31 +597,42 @@ static int samsung_gpio_set_direction(struct gpio_chip *gc, reg += 4; } - spin_lock_irqsave(&bank->slock, flags); - data = readl(reg); data &= ~(mask << shift); if (!input) data |= FUNC_OUTPUT << shift; writel(data, reg); - spin_unlock_irqrestore(&bank->slock, flags); - return 0; } /* gpiolib gpio_direction_input callback function. */ static int samsung_gpio_direction_input(struct gpio_chip *gc, unsigned offset) { - return samsung_gpio_set_direction(gc, offset, true); + struct samsung_pin_bank *bank = gc_to_pin_bank(gc); + unsigned long flags; + int ret; + + spin_lock_irqsave(&bank->slock, flags); + ret = samsung_gpio_set_direction(gc, offset, true); + spin_unlock_irqrestore(&bank->slock, flags); + return ret; } /* gpiolib gpio_direction_output callback function. */ static int samsung_gpio_direction_output(struct gpio_chip *gc, unsigned offset, int value) { - samsung_gpio_set(gc, offset, value); - return samsung_gpio_set_direction(gc, offset, false); + struct samsung_pin_bank *bank = gc_to_pin_bank(gc); + unsigned long flags; + int ret; + + spin_lock_irqsave(&bank->slock, flags); + samsung_gpio_set_value(gc, offset, value); + ret = samsung_gpio_set_direction(gc, offset, false); + spin_unlock_irqrestore(&bank->slock, flags); + + return ret; } /*