From patchwork Tue Nov 8 17:48:52 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Ferre X-Patchwork-Id: 9417765 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id BEB6960459 for ; Tue, 8 Nov 2016 16:50:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AF31B28A54 for ; Tue, 8 Nov 2016 16:50:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A3AA52902E; Tue, 8 Nov 2016 16:50:04 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 9804B28A54 for ; Tue, 8 Nov 2016 16:50:00 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.85_2 #1 (Red Hat Linux)) id 1c49ZP-0000oL-KV; Tue, 08 Nov 2016 16:48:23 +0000 Received: from smtpout.microchip.com ([198.175.253.82] helo=email.microchip.com) by bombadil.infradead.org with esmtps (Exim 4.85_2 #1 (Red Hat Linux)) id 1c49ZI-0000lc-Mo for linux-arm-kernel@lists.infradead.org; Tue, 08 Nov 2016 16:48:18 +0000 Received: from tenerife.corp.atmel.com (10.10.76.4) by chn-sv-exch04.mchp-main.com (10.10.76.105) with Microsoft SMTP Server id 14.3.181.6; Tue, 8 Nov 2016 09:47:53 -0700 From: Nicolas Ferre To: , Mark Brown , Alexandre Belloni , Boris BREZILLON , , Subject: [PATCH v2] spi: atmel: use managed resource for gpio chip select Date: Tue, 8 Nov 2016 18:48:52 +0100 Message-ID: <20161108174852.14311-1-nicolas.ferre@atmel.com> X-Mailer: git-send-email 2.9.0 MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAC+NgFvrDosTGxcLF5cOiu4tRKcLg9q7VN/kt7n3axmhx/9ppFouvTcYW/1+fZrXomLKKyaLx0hF2i2nT3zE7sAcwRLFm5iXlVySwZqyf1sVS8FGq4vzDH+wNjBtFuxi5OIQE1jFKzJ7axNTFyMnBJqArcXbCEzYQW0TgIaPEgiUsIEXMAg1MErs+dLGDJIQFPCV6e3+wgNgsAioSnbemMILYvAI2Egtbv4PFJQTkJBrO32eGiAtKnJz5BCzOLCAhcfDFC7C4EFBv3/t+doj6QIlbn1cyQthOEl0nJ7JB2HYSh6dfhKpxkNi+6wsLTM2uz5eZIWxtie2v9rFC2DoS2w72Q9XYSuyZMZEJwnaXePBoOZTtKzHrYQNUTZRE47OHTBMYxWYhOXUWklMXMDKtYpR29vDTDQ7TdY1w9jAw0ctNzijQzU3MzNNLzs/dxAiJmcwdjMca/A8xSnIwKYnybp10PlyILyk/pTIjsTgjvqg0J7X4EKMEB4+SCK/lcqAcb3FBYm5xZjpMSoaDQ0mCNwIkJViUmp5akZaZU5JaBJE+xSgpJc4bDZIUAOnLKM2Dy11iFJUS5o1aCpTjKUgtys0sgYjfYhTmeMgkxJKXn5cqBXQiAxBoML5iFOdgVBLmdQeZxZOZVwJ3wiug65iArlu3+zTIdSWJCCmpBsbdE95da113+R5fhUG+bYSfzpWSt6a1Jsdko2MXK5Tk/l64IDHF5eRkJu38D2G/UkveHbpubR7DF/pi1kK+KImuFevYWS99viyZrvT8oeaxm28sdsn90TBK0+T44rLw3bfLcdpMpikFDW9//lR+FOj3ucM6MVh66YEmNcnZ/ZVSUwMO/ea9vEmJpTgj0VCLuag4EQBc4+3+DwMAAA== X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20161108_084816_881985_5C6BAED2 X-CRM114-Status: GOOD ( 14.68 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Nicolas Ferre , linux-kernel@vger.kernel.org, Wenyou Yang , Ludovic Desroches , Cyrille Pitchen , linux-arm-kernel@lists.infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Use the managed gpio CS pin request so that we avoid having trouble in the cleanup code. In fact, if module was configured with DT, cleanup code released invalid pin. Since resource wasn't freed, module cannot be reinserted. This require to extract the gpio request call from the "setup" function and call it in the appropriate probe function. Reported-by: Alexander Morozov Signed-off-by: Nicolas Ferre --- v2: fix devm_gpio_request location: the setup code for device was not proper location. Move it to the probe function and add needed DT routines to handle all CS gpio specified. drivers/spi/spi-atmel.c | 50 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/drivers/spi/spi-atmel.c b/drivers/spi/spi-atmel.c index 32683a13dd60..a60925614480 100644 --- a/drivers/spi/spi-atmel.c +++ b/drivers/spi/spi-atmel.c @@ -24,6 +24,7 @@ #include #include +#include #include #include @@ -1204,7 +1205,6 @@ static int atmel_spi_setup(struct spi_device *spi) u32 csr; unsigned int bits = spi->bits_per_word; unsigned int npcs_pin; - int ret; as = spi_master_get_devdata(spi->master); @@ -1247,16 +1247,9 @@ static int atmel_spi_setup(struct spi_device *spi) if (!asd) return -ENOMEM; - if (as->use_cs_gpios) { - ret = gpio_request(npcs_pin, dev_name(&spi->dev)); - if (ret) { - kfree(asd); - return ret; - } - + if (as->use_cs_gpios) gpio_direction_output(npcs_pin, !(spi->mode & SPI_CS_HIGH)); - } asd->npcs_pin = npcs_pin; spi->controller_state = asd; @@ -1471,13 +1464,11 @@ static int atmel_spi_transfer_one_message(struct spi_master *master, static void atmel_spi_cleanup(struct spi_device *spi) { struct atmel_spi_device *asd = spi->controller_state; - unsigned gpio = (unsigned long) spi->controller_data; if (!asd) return; spi->controller_state = NULL; - gpio_free(gpio); kfree(asd); } @@ -1499,6 +1490,39 @@ static void atmel_get_caps(struct atmel_spi *as) } /*-------------------------------------------------------------------------*/ +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 int atmel_spi_probe(struct platform_device *pdev) { @@ -1577,6 +1601,10 @@ 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) {