From patchwork Wed May 24 05:42:38 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Packham X-Patchwork-Id: 9745071 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 7B31760210 for ; Wed, 24 May 2017 05:42:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6BC49288AA for ; Wed, 24 May 2017 05:42:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5FFB7288CB; Wed, 24 May 2017 05:42:55 +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=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A069E288B0 for ; Wed, 24 May 2017 05:42:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S938275AbdEXFmu (ORCPT ); Wed, 24 May 2017 01:42:50 -0400 Received: from gate2.alliedtelesis.co.nz ([202.36.163.20]:49906 "EHLO gate2.alliedtelesis.co.nz" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S936307AbdEXFmt (ORCPT ); Wed, 24 May 2017 01:42:49 -0400 Received: from mmarshal3.atlnz.lc (mmarshal3.atlnz.lc [10.32.18.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by gate2.alliedtelesis.co.nz (Postfix) with ESMTPS id CCB868448F; Wed, 24 May 2017 17:42:45 +1200 (NZST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alliedtelesis.co.nz; s=mail; t=1495604565; bh=dWgza0moxqNokMcgFAuvyN4WiEKJuJefnJZQ42FV61Q=; h=From:To:CC:Subject:Date:References; b=v0OcWdvRN7VFAsfzGMo5tf1GKQAI7TUSILUfSRlffVdRBjRw6E7cMzfdfexxEbXpm +pKmQ4JZSjKgBw8XGKkLl6/N7J5ukVE6Rm1H3mSwa09Foel92YZDZgKz0kFOIuVruq 9LqDPgl6M+wNCnk8QzVmaMsV9aFOtv1mIlGG0XPE= Received: from svr-chch-ex1.atlnz.lc (Not Verified[10.32.16.77]) by mmarshal3.atlnz.lc with Trustwave SEG (v7, 5, 7, 9061) id ; Wed, 24 May 2017 17:42:45 +1200 Received: from svr-chch-ex1.atlnz.lc (2001:df5:b000:bc8::77) by svr-chch-ex1.atlnz.lc (2001:df5:b000:bc8::77) with Microsoft SMTP Server (TLS) id 15.0.1156.6; Wed, 24 May 2017 17:42:39 +1200 Received: from svr-chch-ex1.atlnz.lc ([fe80::409d:36f5:8899:92e8]) by svr-chch-ex1.atlnz.lc ([fe80::409d:36f5:8899:92e8%12]) with mapi id 15.00.1156.000; Wed, 24 May 2017 17:42:39 +1200 From: Chris Packham To: Andy Shevchenko CC: Mark Brown , linux-spi , "linux-kernel@vger.kernel.org" Subject: Re: [PATCH 2/2] spi: use gpio_desc instead of numeric gpio Thread-Topic: [PATCH 2/2] spi: use gpio_desc instead of numeric gpio Thread-Index: AQHS03mJMBZLgXj6wk+vkKwLY4S9KQ== Date: Wed, 24 May 2017 05:42:38 +0000 Message-ID: <1be4374ca1b1422fb2b14298bf21ae9d@svr-chch-ex1.atlnz.lc> References: <20170523040322.10433-1-chris.packham@alliedtelesis.co.nz> <20170523040322.10433-2-chris.packham@alliedtelesis.co.nz> Accept-Language: en-NZ, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [2001:df5:b000:22:64d6:cd65:4e1f:7871] MIME-Version: 1.0 Sender: linux-spi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP On 24/05/17 08:43, Chris Packham wrote: > Hi Andy, > > On 24/05/17 06:28, Andy Shevchenko wrote: >> On Tue, May 23, 2017 at 7:03 AM, Chris Packham >> wrote: >>> By using a gpio_desc and gpiod_set_value() instead of a numeric gpio and >>> gpio_set_value() the gpio flags are taken into account. This is useful >>> when using a gpio chip-select to supplement a controllers native >>> chip-select. >> >> I think would be better to move everything in SPI core to GPIO descriptors. > > I did consider it but it's a big change and I don't have access a lot of > gear to test on (maybe 2 or 3 SoCs with a SPI host controller and the > same SPI-NOR chips). > > I can give it a try. Perhaps converting the spi core structures over and > leaving the slaves using numeric gpios. Then later converting the slaves. > OK so I've had a look at this. The changes to change struct spi_master to use gpio_desc in drivers/spi/spi-gpio.c are pretty straight forward (in-line patch below, apologies for MUA wrapping). There are a few SPI host drivers that deal with cs_gpios directly. spi-mt65xx.c and spi-davinci.c would be trivial to update. spi-ep93xx.c and spi-imx.c are harder because they handle non-dt platforms. Changing struct spi_device to use gpio_desc would be wider reaching but probably automatable s/gpio_set_value/gpiod_set_value/. I'm a little too nervous about messing up spi-ep93xx.c and spi-ep93xx.c to push ahead with changing all of SPI core. I'd be happy to help out someone with access to platforms using those. -- 8< -- index b39c0f9956dd..80a96d3daa45 100644 -- >8 -- >> >>> >>> Signed-off-by: Chris Packham >>> --- >>> >>> Notes: >>> My specific use-case is I have a board that uses the spi-orion driver but >>> only has one CS pin available. In order to access two spi slave devices the >>> board has a 1-of-2 decoder/demultiplexer which is driven via a gpio. >>> >>> The problem is that for one of the 2 slave devices the gpio level required >>> is opposite to the chip-select so I can't simply specify "spi-cs-high". >>> With this change I can flag the gpio as active low and the gpio subsystem >>> takes care of the additional inversion required. >>> >>> drivers/spi/spi.c | 5 ++++- >>> 1 file changed, 4 insertions(+), 1 deletion(-) >>> >>> diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c >>> index 6f87fec409b5..b39c0f9956dd 100644 >>> --- a/drivers/spi/spi.c >>> +++ b/drivers/spi/spi.c >>> @@ -725,7 +725,10 @@ static void spi_set_cs(struct spi_device *spi, bool enable) >>> enable = !enable; >>> >>> if (gpio_is_valid(spi->cs_gpio)) { >>> - gpio_set_value(spi->cs_gpio, !enable); >>> + struct gpio_desc *gpio = gpio_to_desc(spi->cs_gpio); >>> + >>> + if (gpio) >>> + gpiod_set_value(gpio, !enable); >>> /* Some SPI masters need both GPIO CS & slave_select */ >>> if ((spi->master->flags & SPI_MASTER_GPIO_SS) && >>> spi->master->set_cs) >>> -- >>> 2.13.0 >>> >>> -- >>> To unsubscribe from this list: send the line "unsubscribe linux-spi" in >>> the body of a message to majordomo@vger.kernel.org >>> More majordomo info at http://vger.kernel.org/majordomo-info.html >> >> >> > > > --- To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -40,6 +40,7 @@ #include #include #include +#include #define CREATE_TRACE_POINTS #include @@ -531,8 +532,8 @@ int spi_add_device(struct spi_device *spi) goto done; } - if (master->cs_gpios) - spi->cs_gpio = master->cs_gpios[spi->chip_select]; + if (master->cs_gpios[spi->chip_select]) + spi->cs_gpio = desc_to_gpio(master->cs_gpios[spi->chip_select]); /* Drivers may modify this initial i/o setup, but will * normally rely on the device being setup. Devices @@ -1878,7 +1879,8 @@ EXPORT_SYMBOL_GPL(spi_alloc_master); #ifdef CONFIG_OF static int of_spi_register_master(struct spi_master *master) { - int nb, i, *cs; + int nb, i; + struct gpio_desc **cs; struct device_node *np = master->dev.of_node; if (!np) @@ -1894,7 +1896,7 @@ static int of_spi_register_master(struct spi_master *master) return nb; cs = devm_kzalloc(&master->dev, - sizeof(int) * master->num_chipselect, + sizeof(*master->cs_gpios) * master->num_chipselect, GFP_KERNEL); master->cs_gpios = cs; @@ -1902,10 +1904,16 @@ static int of_spi_register_master(struct spi_master *master) return -ENOMEM; for (i = 0; i < master->num_chipselect; i++) - cs[i] = -ENOENT; + cs[i] = NULL; - for (i = 0; i < nb; i++) - cs[i] = of_get_named_gpio(np, "cs-gpios", i); + for (i = 0; i < nb; i++) { + struct gpio_desc *gpio; + + gpio = devm_gpiod_get_index(&master->dev, "cs", i, + GPIOD_OUT_HIGH); + if (!IS_ERR(gpio)) + cs[i] = gpio; + } return 0; } diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h index 935bd2854ff1..46960ca62d5b 100644 --- a/include/linux/spi/spi.h +++ b/include/linux/spi/spi.h @@ -556,7 +556,7 @@ struct spi_master { struct spi_message *message); /* gpio chip select */ - int *cs_gpios; + struct gpio_desc **cs_gpios; /* statistics */ struct spi_statistics statistics;