From patchwork Thu May 6 03:00:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jay Fang X-Patchwork-Id: 12241535 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B8034C43460 for ; Thu, 6 May 2021 03:00:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9B00C600D4 for ; Thu, 6 May 2021 03:00:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231842AbhEFDBZ (ORCPT ); Wed, 5 May 2021 23:01:25 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:17469 "EHLO szxga05-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231745AbhEFDBY (ORCPT ); Wed, 5 May 2021 23:01:24 -0400 Received: from DGGEMS405-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4FbJCy2qdWzkWqK; Thu, 6 May 2021 10:57:50 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by DGGEMS405-HUB.china.huawei.com (10.3.19.205) with Microsoft SMTP Server id 14.3.498.0; Thu, 6 May 2021 11:00:16 +0800 From: Jay Fang To: , CC: , , , , Subject: [PATCH 1/2] spi: Correct CS GPIOs polarity when using GPIO descriptors Date: Thu, 6 May 2021 11:00:16 +0800 Message-ID: <1620270017-52643-2-git-send-email-f.fangjian@huawei.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1620270017-52643-1-git-send-email-f.fangjian@huawei.com> References: <1620270017-52643-1-git-send-email-f.fangjian@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org When using GPIO descriptors for CS GPIOs we need to inspect the child node to determine if the 'cs-gpios' active flags should have inverted semantics. Previously, commit 6953c57ab172 ("gpio: of: Handle SPI chipselect legacy bindings") introduced a special quirk to solve the conflict between the 'cs-gpios' flags and the optional SPI slaves flags. The principle is that the SPI slave flag will take precedence when we get a conflict. For more details, see Documentation/devicetree/bindings/spi/spi-controller.yaml. If following the previous commit, we need to add a new quirk to deal with this in ACPI. Instead, this patch introduces a generic implementation to solve this conflict by SPI core. The 'cs-gpios' active rules are as follows: 1) DT (SPI device have active low chip selects by default): device node | cs-gpio | CS pin state active ================+===============+===================== spi-cs-high | - | H - | - | L spi-cs-high | ACTIVE_HIGH | H - | ACTIVE_HIGH | L spi-cs-high | ACTIVE_LOW | H - | ACTIVE_LOW | L 2) ACPI: device node | cs-gpio | CS pin state active ================+===============+===================== PolarityLow | ACTIVE_LOW | L PolarityLow | ACTIVE_HIGH | L PolarityHigh | ACTIVE_LOW | H PolarityHigh | ACTIVE_HIGH | H Signed-off-by: Jay Fang Signed-off-by: Zihao Tang --- drivers/spi/spi.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index b08efe8..2c0324d 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -33,6 +33,7 @@ #include #include #include +#include <../gpio/gpiolib.h> #define CREATE_TRACE_POINTS #include @@ -604,9 +605,26 @@ int spi_add_device(struct spi_device *spi) } /* Descriptors take precedence */ - if (ctlr->cs_gpiods) + if (ctlr->cs_gpiods) { spi->cs_gpiod = ctlr->cs_gpiods[spi->chip_select]; - else if (ctlr->cs_gpios) + + /* We need to handle the active flag conflict between + * the cs-gpio and the SPI slave. The SPI slave active + * flag will determine the active state of cs-gpio. + */ + if (spi->mode & SPI_CS_HIGH) { + if (test_bit(FLAG_ACTIVE_LOW, &spi->cs_gpiod->flags)) { + dev_warn(&spi->dev, "GPIO handle specifies " + "active low - ignored\n"); + clear_bit(FLAG_ACTIVE_LOW, &spi->cs_gpiod->flags); + } + } else { + if (!test_bit(FLAG_ACTIVE_LOW, &spi->cs_gpiod->flags)) + dev_warn(&spi->dev, "enforce active low on " + "chipselect handle\n"); + set_bit(FLAG_ACTIVE_LOW, &spi->cs_gpiod->flags); + } + } else if (ctlr->cs_gpios) spi->cs_gpio = ctlr->cs_gpios[spi->chip_select]; /* Drivers may modify this initial i/o setup, but will