From patchwork Sat Dec 15 23:38:19 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 153940 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp955833ljp; Sat, 15 Dec 2018 15:46:08 -0800 (PST) X-Google-Smtp-Source: AFSGD/UNi7REwsGG/8bgyrvkYK43WJWea9XHY3n6tqxnpN8Fm7GzH6d4YCegy7YGfMqXo4b7kWUr X-Received: by 2002:aa7:868f:: with SMTP id d15mr7836989pfo.225.1544917568155; Sat, 15 Dec 2018 15:46:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544917568; cv=none; d=google.com; s=arc-20160816; b=FB0pLONoaLMUpWuHMbecqYs/IqQ22K2NBS/9m/8399zlmSMU6qsob9BXU+2uuy1eMJ TiZi9VOW2kNJjHDxHZ49B/UmbYCUzRGwefEax5rK7S641Z43kyRLN1y0gCPm9HbTcmwl LNXtPtjb8DNQ00G2gckIzJNtwPYyTH6tLq01u+kRVuboM6UG0s8X5Psy0fz8qnI8N0gF wZAf5ok5m+EUG9YGkfimzzcieYznaWotxfbC6xBOtdBt1dT4CYPqBuwrG63FpMIcLTue fJhtHy7jkL+1aA3I8Y/ReaOxhAwVlM2LvFKq2WJ2TxiRf71ZfzoodhkOAXTB6G2cxf/o FpJA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=iHv29zTjPCD2odmzofUPFh/r9yEa+aBrNFMCDdhB260=; b=qctScp6/2mZ68efvrWhz3TTFvUdJwt1CZLHy4qAeqpoxoUlznB5jmqZzZklwdSRdpd HQ9LBJw2F3KnjQvzBBKQkqnvU+tvZIPmjhKMZuEovBbrFJKrd2ROUXed7w/qHhsLvdvv 4QK/9aigix+4eSQIjgQdPf/ufgz4OXg1zkYOYSsxOcBxGcWWlXDj6GEEdd7gXkxDhugk A9Lr22yTa65yqFpeP1cqkHV+iVniO/f/OKKN6ZcJRA42u837EQpBy8Vm8hFu+qp9nmfc 5gOBU9nW0mytD24IDIkFExfJFL8BRxFbR74b8dfz+9HJRs4ljPhMu0hJH1JKYglXZzUV oMng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="HOS/z83j"; spf=pass (google.com: best guess record for domain of linux-spi-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-spi-owner@vger.kernel.org; dmarc=pass (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 1si7185999pld.239.2018.12.15.15.46.07; Sat, 15 Dec 2018 15:46:08 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-spi-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=@linaro.org header.s=google header.b="HOS/z83j"; spf=pass (google.com: best guess record for domain of linux-spi-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-spi-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726683AbeLOXqH (ORCPT + 1 other); Sat, 15 Dec 2018 18:46:07 -0500 Received: from mail-lj1-f194.google.com ([209.85.208.194]:37111 "EHLO mail-lj1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727340AbeLOXqH (ORCPT ); Sat, 15 Dec 2018 18:46:07 -0500 Received: by mail-lj1-f194.google.com with SMTP id e5-v6so7951120lja.4 for ; Sat, 15 Dec 2018 15:46:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=iHv29zTjPCD2odmzofUPFh/r9yEa+aBrNFMCDdhB260=; b=HOS/z83jyGPTsaj4ayCHBIdzmDrxUUuGq7x7XjaDmR9hJ3ZKCrDiNiWFaQgUtpnpxY lJxacyHGB//swcYhT3PZ72CiDcf6C5vdotFyA3opHQfA80Niqsk+bUA9Z+C16/vqehux Mqxi/Mecwp8RgfOtOhD5iP9p8eGjrs6zpzGpQ= 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=iHv29zTjPCD2odmzofUPFh/r9yEa+aBrNFMCDdhB260=; b=Fh16H9xmkD8FXmaMdSS/iapo9rCwGvXo1wM4U2ALaBjiB1FtqNocntvipXb/ccAsg+ Rbg5IxioPY2I11jh5rSxEKlM0Z22IdhlT/Q4epmSpoJ2LT4BL5mkKo0++tWr8eYXKDpr LBjZf7DL6FIjdx38FbvIXtbpLy4WcuoJwRj66XYIFttTk1e+UvuN3CPlwq5cYysdZu9U Rmr+EJtDsiuDGsZl9AhaDm4wa3r4hbXuqrjbpeffczbdXzHTxapaDlP41gUTuJnxW4jU 9SOI/mx7HAaa1DlHc72I3Z1NBBhB4CkfxB9IVxRVK2aS/AOo+hObBwV00ZEZADiPJCep jZYQ== X-Gm-Message-State: AA+aEWYmyUql0j3MvG70U0pWBJuvZAKe/ZvLTq2KeRGQC2xsxfCH08uh csBGTyFjYu7z4ruwkp3LMw/vVg== X-Received: by 2002:a2e:c41:: with SMTP id o1-v6mr4646239ljd.152.1544917564103; Sat, 15 Dec 2018 15:46:04 -0800 (PST) Received: from localhost.localdomain (c-ae7b71d5.014-348-6c756e10.bbcust.telenor.se. [213.113.123.174]) by smtp.gmail.com with ESMTPSA id v19sm1709880lfe.69.2018.12.15.15.46.02 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 15 Dec 2018 15:46:03 -0800 (PST) From: Linus Walleij To: Mark Brown , linux-spi@vger.kernel.org Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski , linuxarm@huawei.com, Linus Walleij , Eugen Hristev , Nicolas Ferre , Radu Pirea Subject: [PATCH 3/7 v1] spi: atmel: Convert to use CS GPIO descriptors Date: Sun, 16 Dec 2018 00:38:19 +0100 Message-Id: <20181215233823.1042-4-linus.walleij@linaro.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181215233823.1042-1-linus.walleij@linaro.org> References: <20181215233823.1042-1-linus.walleij@linaro.org> MIME-Version: 1.0 Sender: linux-spi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org This converts the Atmel SPI master driver to use GPIO descriptors for chip select handling. The Atmel driver has duplicate code to look up and initialize CS GPIOs from the device tree, so this is removed. It further has code to retrieve a CS GPIO from .controller_data but this seems to be completely unused in the kernel (legacy codepath?) so I deleted this support. It keeps track of polarity when switching the CS, but this is not needed anymore since we moved this over to the gpiolib. The local handling of the "npcs_pin" (I guess this might mean "negative polarity chip select pin") is preserved, but I strongly suspect this can be switched over to handling by the core and using the SPI_MASTER_GPIO_SS flag on the master to assure that the additional CS handling in the driver is also done. Cc: Eugen Hristev Cc: Nicolas Ferre Cc: Radu Pirea Cc: Linuxarm Signed-off-by: Linus Walleij --- drivers/spi/spi-atmel.c | 93 ++++++++++++----------------------------- 1 file changed, 27 insertions(+), 66 deletions(-) -- 2.19.2 diff --git a/drivers/spi/spi-atmel.c b/drivers/spi/spi-atmel.c index 74fddcd3282b..f53f0c5e63da 100644 --- a/drivers/spi/spi-atmel.c +++ b/drivers/spi/spi-atmel.c @@ -23,8 +23,7 @@ #include #include -#include -#include +#include #include #include @@ -312,7 +311,7 @@ struct atmel_spi { /* Controller-specific per-slave state */ struct atmel_spi_device { - unsigned int npcs_pin; + struct gpio_desc *npcs_pin; u32 csr; }; @@ -355,7 +354,6 @@ static bool atmel_spi_is_v2(struct atmel_spi *as) static void cs_activate(struct atmel_spi *as, struct spi_device *spi) { struct atmel_spi_device *asd = spi->controller_state; - unsigned active = spi->mode & SPI_CS_HIGH; u32 mr; if (atmel_spi_is_v2(as)) { @@ -379,7 +377,7 @@ static void cs_activate(struct atmel_spi *as, struct spi_device *spi) mr = spi_readl(as, MR); if (as->use_cs_gpios) - gpio_set_value(asd->npcs_pin, active); + gpiod_set_value(asd->npcs_pin, 1); } else { u32 cpol = (spi->mode & SPI_CPOL) ? SPI_BIT(CPOL) : 0; int i; @@ -396,19 +394,16 @@ static void cs_activate(struct atmel_spi *as, struct spi_device *spi) mr = spi_readl(as, MR); mr = SPI_BFINS(PCS, ~(1 << spi->chip_select), mr); if (as->use_cs_gpios && spi->chip_select != 0) - gpio_set_value(asd->npcs_pin, active); + gpiod_set_value(asd->npcs_pin, 1); spi_writel(as, MR, mr); } - dev_dbg(&spi->dev, "activate %u%s, mr %08x\n", - asd->npcs_pin, active ? " (high)" : "", - mr); + dev_dbg(&spi->dev, "activate NPCS, mr %08x\n", mr); } static void cs_deactivate(struct atmel_spi *as, struct spi_device *spi) { struct atmel_spi_device *asd = spi->controller_state; - unsigned active = spi->mode & SPI_CS_HIGH; u32 mr; /* only deactivate *this* device; sometimes transfers to @@ -420,14 +415,12 @@ static void cs_deactivate(struct atmel_spi *as, struct spi_device *spi) spi_writel(as, MR, mr); } - dev_dbg(&spi->dev, "DEactivate %u%s, mr %08x\n", - asd->npcs_pin, active ? " (low)" : "", - mr); + dev_dbg(&spi->dev, "DEactivate NPCS, mr %08x\n", mr); if (!as->use_cs_gpios) spi_writel(as, CR, SPI_BIT(LASTXFER)); else if (atmel_spi_is_v2(as) || spi->chip_select != 0) - gpio_set_value(asd->npcs_pin, !active); + gpiod_set_value(asd->npcs_pin, 0); } static void atmel_spi_lock(struct atmel_spi *as) __acquires(&as->lock) @@ -1188,7 +1181,6 @@ static int atmel_spi_setup(struct spi_device *spi) struct atmel_spi_device *asd; u32 csr; unsigned int bits = spi->bits_per_word; - unsigned int npcs_pin; as = spi_master_get_devdata(spi->master); @@ -1217,25 +1209,27 @@ static int atmel_spi_setup(struct spi_device *spi) csr |= SPI_BF(DLYBS, 0); csr |= SPI_BF(DLYBCT, 0); - /* chipselect must have been muxed as GPIO (e.g. in board setup) */ - npcs_pin = (unsigned long)spi->controller_data; - - if (!as->use_cs_gpios) - npcs_pin = spi->chip_select; - else if (gpio_is_valid(spi->cs_gpio)) - npcs_pin = spi->cs_gpio; - asd = spi->controller_state; if (!asd) { asd = kzalloc(sizeof(struct atmel_spi_device), GFP_KERNEL); if (!asd) return -ENOMEM; - if (as->use_cs_gpios) - gpio_direction_output(npcs_pin, - !(spi->mode & SPI_CS_HIGH)); + /* + * If use_cs_gpios is true this means that we have "cs-gpios" + * defined in the device tree node so we should have + * gotten the GPIO lines from the device tree inside the + * SPI core. Warn if this is not the case but continue since + * CS GPIOs are after all optional. + */ + if (as->use_cs_gpios) { + if (!spi->cs_gpiod) { + dev_err(&spi->dev, + "host claims to use CS GPIOs but no CS found in DT by the SPI core\n"); + } + asd->npcs_pin = spi->cs_gpiod; + } - asd->npcs_pin = npcs_pin; spi->controller_state = asd; } @@ -1473,41 +1467,6 @@ static void atmel_get_caps(struct atmel_spi *as) as->caps.has_pdc_support = version < 0x212; } -/*-------------------------------------------------------------------------*/ -static int atmel_spi_gpio_cs(struct platform_device *pdev) -{ - struct spi_master *master = platform_get_drvdata(pdev); - struct atmel_spi *as = spi_master_get_devdata(master); - struct device_node *np = master->dev.of_node; - int i; - int ret = 0; - int nb = 0; - - if (!as->use_cs_gpios) - return 0; - - if (!np) - return 0; - - nb = of_gpio_named_count(np, "cs-gpios"); - for (i = 0; i < nb; i++) { - int cs_gpio = of_get_named_gpio(pdev->dev.of_node, - "cs-gpios", i); - - if (cs_gpio == -EPROBE_DEFER) - return cs_gpio; - - if (gpio_is_valid(cs_gpio)) { - ret = devm_gpio_request(&pdev->dev, cs_gpio, - dev_name(&pdev->dev)); - if (ret) - return ret; - } - } - - return 0; -} - static void atmel_spi_init(struct atmel_spi *as) { spi_writel(as, CR, SPI_BIT(SWRST)); @@ -1560,6 +1519,7 @@ static int atmel_spi_probe(struct platform_device *pdev) goto out_free; /* the spi->mode bits understood by this driver: */ + master->use_gpio_descriptors = true; master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; master->bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 16); master->dev.of_node = pdev->dev.of_node; @@ -1592,6 +1552,11 @@ static int atmel_spi_probe(struct platform_device *pdev) atmel_get_caps(as); + /* + * If there are chip selects in the device tree, those will be + * discovered by the SPI core when registering the SPI master + * and assigned to each SPI device. + */ as->use_cs_gpios = true; if (atmel_spi_is_v2(as) && pdev->dev.of_node && @@ -1600,10 +1565,6 @@ static int atmel_spi_probe(struct platform_device *pdev) master->num_chipselect = 4; } - ret = atmel_spi_gpio_cs(pdev); - if (ret) - goto out_unmap_regs; - as->use_dma = false; as->use_pdc = false; if (as->caps.has_dma_support) {