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: 123065 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp7869137qgn; Mon, 1 Jan 2018 05:38:28 -0800 (PST) X-Google-Smtp-Source: ACJfBouCsFXDVlx60TZ48wdzmt5/rTJaDsPumyEA0GtODj/+lSTp+O2nW95H5M57zzzTjAsar6S1 X-Received: by 10.84.234.198 with SMTP id i6mr41699204plt.159.1514813908797; Mon, 01 Jan 2018 05:38:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1514813908; cv=none; d=google.com; s=arc-20160816; b=inwSU4j71XLVPis5tRNWFclHSqqoJqiMnA5vmF110Je2AB9VHSJXP4VkeAwPP7ecmj 0odoCX2WITHe8KpZlViRCpmNB4y1Nhb8IIIgeCHYKC33yVhqUFXaNNLg1aUC5GKUTkeX hF0MsEfPF48Vz2e/iwUDqskJSGJN+pD6soxkQLp2RVjdhqcdBnK/ZdjvsrxL7kTgAYPT 14WOcGDrbiStoUi0osyrS2n2yLGc5LErd7MnSWeUERhppO10bIR9zNamMeUYBJsSy7Bb 3LMr4gx4ZOqf0XBsC00q2lpNeZDzI6FvT/NoHQ4ZoH8CCaRe+sYQcI0mahjuCx1Y2Qpr Wstg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=ymAe4SQ61qPen07LFsl3GMinnpfuRi9D7KmawPUYLv4=; b=ea1RkphKSAUu1X4Ce4RIy+rApxbUROIbYy5p8O9mXilZZfe1evq0pPMhFVgXNiYbGY BD9YFn3537sQARqtrBjy4wA0unr5/ti1T9abNgx9ku77I1eUczgc/sXtjPT+jU3O2kIm XNPV7Vwe4XlwgX0PfGcFqd3rSKPNNaft0oKKaW0OsAfxUCBzjM81b6Y5ZsGwCmrTNi6b gwl9rqM+gPN2JevzCMTPwQpSrwQXiUkfcr1/BSv/v4kEoFbH1ZeuphvwMsKNpyqgAvTX q/mTz+cdV17fr91RHA3ryKE4wn59h9EDlTK3u8N932tYZ51ZKGOs2lcvZK7gYJwxs2Tm owwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=XWMpQmkL; 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=fail (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 f13si8470380pga.543.2018.01.01.05.38.28; Mon, 01 Jan 2018 05:38:28 -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=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=XWMpQmkL; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751381AbeAANi2 (ORCPT + 1 other); 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-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 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(-) -- 2.14.3 -- 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 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