From patchwork Mon Jan 1 13:37:48 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 10138549 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 30D6260362 for ; Mon, 1 Jan 2018 13:38:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 22C002851B for ; Mon, 1 Jan 2018 13:38:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1793328817; Mon, 1 Jan 2018 13:38:29 +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 ACA1D2851B for ; Mon, 1 Jan 2018 13:38:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751381AbeAANi2 (ORCPT ); Mon, 1 Jan 2018 08:38:28 -0500 Received: from mail-wr0-f195.google.com ([209.85.128.195]:44354 "EHLO mail-wr0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751111AbeAANi1 (ORCPT ); Mon, 1 Jan 2018 08:38:27 -0500 Received: by mail-wr0-f195.google.com with SMTP id l41so34309812wre.11 for ; Mon, 01 Jan 2018 05:38:27 -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; bh=9p7K4uwxZBHdTHVcweMiUe5e2APW4JcZL4d5OeHxVTo=; b=XWMpQmkLubd+Q9Q1j0iSG7whYw7WB4lRsAM7D5pCz5o3u1MiIS1ELB17pCDYjTO6NB gVPF2LdbESTrRpupc7WJH8BdGz7+a1J4mbMDz8JIHshWWIBgE08dtjBVQoAP3rdHxFoP iS/o3ow7Enu5AucOP1UPe1jukryA5TqzUIH4g= 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; bh=9p7K4uwxZBHdTHVcweMiUe5e2APW4JcZL4d5OeHxVTo=; b=TbVK3drXsBR2wC2hr+UATCcja1FzsYR4R4Uz1xtw7IkGyCPn/JGUiPtKzcCxSmx4lo IlepcvSFXrjwhoiiRCmm04D1ykIEgZh4zZ8yVIkxSiBsTELbLaJx7vIRDjV0MR1Nq79T dR1dlYZyTSc5ff4Rcuu+Zekzpfg5HVVWBsTdcB+mtCTpdWZw1T/qh8PWeZNktulqK9kA YvtCQDLiEcmcgM743mmwDVJqosZ+1+TlRpnvthGrw7D+cZ5peyouMpo5ef+ELCy+EfZp +KgoutVaoDeZnvHJfjKJNJ8308aXyOsBXbD+1bxljjN30LplhpkNyPp/w0EOP6Apf1KD l2bw== X-Gm-Message-State: AKGB3mKczApG9ZFTxKq7qsfv+Qgv6IRPtgih3dxL1HVwNCwFTPNuotA0 XCgGgOUFnrrTQgYHTucB69mZ8g== X-Google-Smtp-Source: ACJfBovqzPaz7F0RoAwJZ00Nj5Toqha4aXUYeAUYP1tl0WPyTcrXHGvetMmG/mr6kOds5/yCdU3xjQ== X-Received: by 10.223.133.162 with SMTP id 31mr38442031wrt.251.1514813906592; Mon, 01 Jan 2018 05:38:26 -0800 (PST) Received: from localhost.localdomain (catv-80-98-14-92.catv.broadband.hu. [80.98.14.92]) by smtp.gmail.com with ESMTPSA id d7sm53491267wrd.54.2018.01.01.05.38.25 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 01 Jan 2018 05:38:25 -0800 (PST) From: Linus Walleij To: Mark Brown , linux-spi@vger.kernel.org Cc: Linus Walleij Subject: [PATCH 4/5] spi: spi-gpio: Make optional chipselect handling more explicit Date: Mon, 1 Jan 2018 14:37:48 +0100 Message-Id: <20180101133749.29567-5-linus.walleij@linaro.org> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180101133749.29567-1-linus.walleij@linaro.org> References: <20180101133749.29567-1-linus.walleij@linaro.org> 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 I don't like the use of a NULL GPIO descriptor to handle the "no chipselect connected" case, as it makes the code hard to read. Use a clear bool "has_cs" that we use to explicitly handle the case when no chip select is connected to it is clear to readers how this is achieved. When there is no chip select connected, we don't even allocate a placeholder for the GPIO descriptor. Signed-off-by: Linus Walleij --- drivers/spi/spi-gpio.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/drivers/spi/spi-gpio.c b/drivers/spi/spi-gpio.c index ab2cb9427481..c12e588e54e7 100644 --- a/drivers/spi/spi-gpio.c +++ b/drivers/spi/spi-gpio.c @@ -47,6 +47,7 @@ struct spi_gpio { struct gpio_desc *sck; struct gpio_desc *miso; struct gpio_desc *mosi; + bool has_cs; /* Will be allocated times number of devices beyond end of struct */ struct gpio_desc *cs_gpios[0]; }; @@ -215,15 +216,18 @@ static u32 spi_gpio_spec_txrx_word_mode3(struct spi_device *spi, static void spi_gpio_chipselect(struct spi_device *spi, int is_active) { struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); - struct gpio_desc *cs = spi_gpio->cs_gpios[spi->chip_select]; /* set initial clock line level */ if (is_active) gpiod_set_value_cansleep(spi_gpio->sck, spi->mode & SPI_CPOL); - if (cs) + /* Drive chip select line, if we have one */ + if (spi_gpio->has_cs) { + struct gpio_desc *cs = spi_gpio->cs_gpios[spi->chip_select]; + /* SPI chip selects are normally active-low */ gpiod_set_value_cansleep(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active); + } } static int spi_gpio_setup(struct spi_device *spi) @@ -293,9 +297,6 @@ static int spi_gpio_request(struct device *dev, if (IS_ERR(spi_gpio->cs_gpios[i])) return PTR_ERR(spi_gpio->cs_gpios[i]); } - /* Dummy chipselect line if the single device is not using chipselect */ - if (!num_chipselects) - spi_gpio->cs_gpios[0] = NULL; return 0; } @@ -354,7 +355,6 @@ static int spi_gpio_probe(struct platform_device *pdev) struct spi_gpio_platform_data *pdata; u16 master_flags = 0; bool use_of = 0; - int num_devices; status = spi_gpio_probe_dt(pdev); if (status < 0) @@ -368,19 +368,17 @@ static int spi_gpio_probe(struct platform_device *pdev) return -ENODEV; #endif - if (use_of && !pdata->num_chipselect) - num_devices = 1; - else - num_devices = pdata->num_chipselect; - master = spi_alloc_master(&pdev->dev, sizeof(*spi_gpio) + - (sizeof(struct gpio_desc *) * num_devices)); + (sizeof(struct gpio_desc *) * pdata->num_chipselect)); if (!master) return -ENOMEM; spi_gpio = spi_master_get_devdata(master); platform_set_drvdata(pdev, spi_gpio); + /* Determine if we have chip selects connected */ + spi_gpio->has_cs = !!pdata->num_chipselect; + spi_gpio->pdev = pdev; if (pdata) spi_gpio->pdata = *pdata; @@ -393,7 +391,8 @@ static int spi_gpio_probe(struct platform_device *pdev) master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32); master->flags = master_flags; master->bus_num = pdev->id; - master->num_chipselect = num_devices; + /* The master needs to think there is a chipselect even if not connected */ + master->num_chipselect = spi_gpio->has_cs ? pdata->num_chipselect : 1; master->setup = spi_gpio_setup; master->cleanup = spi_gpio_cleanup; #ifdef CONFIG_OF