From patchwork Fri Sep 1 11:15:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 13372432 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4156DCA0FE1 for ; Fri, 1 Sep 2023 11:16:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=eH+P4ycvPIpU2jLlVBy1An6kG5APhtt7SwbyvWIg8Y4=; b=3hR/DY2aKpQ+o+ ypHJ/I+CCr7UJPM29xXjZ5GqoNd3Qb7CUHesUuTk5PUwzFx573A/FXeIor8PqfQjq9Jmw7JyGlI1+ b6eRe/3DwWcwhJC9UK1Xt79edvHofRs1aESBJNeP4OyVGs+1bYL7gKPk9WF6ylzssuS3u131XBdnm 9tryRiZyG53D1RQsx/sGP30vPo+i/5EhRDSX3aJYfyj5RSm0V8u3NpekP+ZVS0QuLgTp/RSuO4WwB VbQYaRAUDgjoaPS0jUtiugNQpQjIDbomNByGpg6axcmjKzd5aqKXdP8gKMI5CNRkqpb4qlonH9gMn 1H1K3hZpGsxpaXq0a2fA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qc28J-00HBA0-10; Fri, 01 Sep 2023 11:16:11 +0000 Received: from mail-wr1-x42d.google.com ([2a00:1450:4864:20::42d]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qc285-00HB3H-2D for linux-arm-kernel@lists.infradead.org; Fri, 01 Sep 2023 11:16:09 +0000 Received: by mail-wr1-x42d.google.com with SMTP id ffacd0b85a97d-31adc5c899fso1628028f8f.2 for ; Fri, 01 Sep 2023 04:15:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1693566954; x=1694171754; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=JXKeOLDoyy5/kC3fUdlXL9lLNwTUvGaVFBeNIEjZIMQ=; b=mvLm81+7gOITv2f2PBGNzdIeUicvOAVo1hPBWmsFYkCpIFIbJYu/HTfdT0ECYGy4FJ wrK7G6fzr0UpncUMRT8PvJh8b6SGpOeIkIXZvFIoysA8zdoLPCEi5cGF4iyvayYTXTvk h/JPVnvUnErr1gzyql8kOr8yNSM8mwMh5qakEKqoaff4AjnodUTjO9J7AxZn62jgUTLR 6p2bW1IX4IYqS9m3uXZM+9OnWICbfEaD3ulVPbLx/Dy3+hGqUiZ5+2m1yd7lphzL9RBh I7VGgpdW3TeZkXxoZ/wy+N0LinJwKUQggF1UPtAwdTv6hfcewVK6VAFfDWGlz3VhmkBL cOrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693566954; x=1694171754; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=JXKeOLDoyy5/kC3fUdlXL9lLNwTUvGaVFBeNIEjZIMQ=; b=HzyVzdc744V+3mtDb8W7+sdb1v1ia2oA+qoQ2NZLCbNL+2JBk2oUV5VN1ZCTi/ug9h +Eg7fTsVffxwdZiiIqMMJQh7O7VCvLYvCR8nW/PmTsqLtuKvtJiv5CNyR4zecWjp82gf yuRhtO386wfDlaTrMX3fCIF0Z/Ck0p16r8r1/CfxXbkFyaNLkL0q8hewFAbyXRiEm8Lf kG2g+Uaa17ZfYf5Onco4z691pkc53f5bP5IaTM6Awa8lQLDclZiE7MNgazbInmFUIBHB HrHthZBkVj0GGkFisI9TZ5GmaOw+TAaoTYsjcykYjW7DqUziIC30rGDQ+k0L3ymYbYft DX6w== X-Gm-Message-State: AOJu0YzkyUKrDudyYQECzO1sZ6RQQcy4AtKUagIfke30HRibmPRBDWEE xj9l8r7mT+soy+RxZkKBx8LOMg== X-Google-Smtp-Source: AGHT+IE+Q3n8iF3DTTB+G/JNTL/+AMsHyYDbREZ9/1BFcmOOL2dovB8KiBgpxGdxnlcSGkTBS0CL8g== X-Received: by 2002:a5d:4e0e:0:b0:316:fc86:28ae with SMTP id p14-20020a5d4e0e000000b00316fc8628aemr1394230wrt.15.1693566953774; Fri, 01 Sep 2023 04:15:53 -0700 (PDT) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:e94b:1054:6760:aa27]) by smtp.gmail.com with ESMTPSA id s2-20020a5d4242000000b003197c2316ecsm4943457wrr.112.2023.09.01.04.15.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Sep 2023 04:15:53 -0700 (PDT) From: Bartosz Golaszewski To: Mark Brown , Florian Fainelli , Andy Shevchenko , Linus Walleij , Ray Jui , Scott Branden Cc: Broadcom internal kernel review list , linux-spi@vger.kernel.org, linux-rpi-kernel@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, Bartosz Golaszewski Subject: [RFT PATCH v2] spi: bcm2835: reduce the abuse of the GPIO API Date: Fri, 1 Sep 2023 13:15:48 +0200 Message-Id: <20230901111548.12733-1-brgl@bgdev.pl> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230901_041604_428592_75B7AAD9 X-CRM114-Status: GOOD ( 26.80 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Bartosz Golaszewski Currently the bcm2835 SPI driver uses functions that are available exclusively to GPIO providers as a way to handle a platform quirk. Let's use a slightly better alternative that avoids poking around in GPIOLIB's internals and use GPIO lookup tables. Link: https://www.spinics.net/lists/linux-gpio/msg36218.html Signed-off-by: Bartosz Golaszewski Reviewed-by: Linus Walleij --- This is only build-tested. It should work, but it would be great if someone from broadcom could test this. v1 -> v2: - don't use devres for managing the GPIO but put it manually in .cleanup() - add a mailing list link explaining the background of the bug - fix kerneldoc drivers/spi/spi-bcm2835.c | 59 +++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/drivers/spi/spi-bcm2835.c b/drivers/spi/spi-bcm2835.c index e7bb2714678a..e06738705075 100644 --- a/drivers/spi/spi-bcm2835.c +++ b/drivers/spi/spi-bcm2835.c @@ -11,6 +11,7 @@ * spi-atmel.c, Copyright (C) 2006 Atmel Corporation */ +#include #include #include #include @@ -26,9 +27,10 @@ #include #include #include -#include /* FIXME: using chip internals */ -#include /* FIXME: using chip internals */ +#include /* FIXME: using GPIO lookup tables */ #include +#include +#include #include /* SPI register offsets */ @@ -83,6 +85,7 @@ MODULE_PARM_DESC(polling_limit_us, * struct bcm2835_spi - BCM2835 SPI controller * @regs: base address of register map * @clk: core clock, divided to calculate serial clock + * @cs_gpio: chip-select GPIO descriptor * @clk_hz: core clock cached speed * @irq: interrupt, signals TX FIFO empty or RX FIFO ¾ full * @tfr: SPI transfer currently processed @@ -117,6 +120,7 @@ MODULE_PARM_DESC(polling_limit_us, struct bcm2835_spi { void __iomem *regs; struct clk *clk; + struct gpio_desc *cs_gpio; unsigned long clk_hz; int irq; struct spi_transfer *tfr; @@ -1156,15 +1160,11 @@ static void bcm2835_spi_handle_err(struct spi_controller *ctlr, bcm2835_spi_reset_hw(bs); } -static int chip_match_name(struct gpio_chip *chip, void *data) -{ - return !strcmp(chip->label, data); -} - static void bcm2835_spi_cleanup(struct spi_device *spi) { struct bcm2835_spidev *target = spi_get_ctldata(spi); struct spi_controller *ctlr = spi->controller; + struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr); if (target->clear_rx_desc) dmaengine_desc_free(target->clear_rx_desc); @@ -1175,6 +1175,9 @@ static void bcm2835_spi_cleanup(struct spi_device *spi) sizeof(u32), DMA_TO_DEVICE); + gpiod_put(bs->cs_gpio); + spi_set_csgpiod(spi, 0, NULL); + kfree(target); } @@ -1221,7 +1224,7 @@ static int bcm2835_spi_setup(struct spi_device *spi) struct spi_controller *ctlr = spi->controller; struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr); struct bcm2835_spidev *target = spi_get_ctldata(spi); - struct gpio_chip *chip; + struct gpiod_lookup_table *lookup __free(kfree) = NULL; int ret; u32 cs; @@ -1288,29 +1291,37 @@ static int bcm2835_spi_setup(struct spi_device *spi) } /* - * Translate native CS to GPIO + * TODO: The code below is a slightly better alternative to the utter + * abuse of the GPIO API that I found here before. It creates a + * temporary lookup table, assigns it to the SPI device, gets the GPIO + * descriptor and then releases the lookup table. * - * FIXME: poking around in the gpiolib internals like this is - * not very good practice. Find a way to locate the real problem - * and fix it. Why is the GPIO descriptor in spi->cs_gpiod - * sometimes not assigned correctly? Erroneous device trees? + * More on the problem that it addresses: + * https://www.spinics.net/lists/linux-gpio/msg36218.html */ + lookup = kzalloc(struct_size(lookup, table, 1), GFP_KERNEL); + if (!lookup) { + ret = -ENOMEM; + goto err_cleanup; + } - /* get the gpio chip for the base */ - chip = gpiochip_find("pinctrl-bcm2835", chip_match_name); - if (!chip) - return 0; + lookup->dev_id = dev_name(&spi->dev); + lookup->table[0].key = "pinctrl-bcm2835"; + lookup->table[0].chip_hwnum = (8 - (spi_get_chipselect(spi, 0))); + lookup->table[0].con_id = "cs"; + lookup->table[0].flags = GPIO_LOOKUP_FLAGS_DEFAULT; - spi_set_csgpiod(spi, 0, gpiochip_request_own_desc(chip, - 8 - (spi_get_chipselect(spi, 0)), - DRV_NAME, - GPIO_LOOKUP_FLAGS_DEFAULT, - GPIOD_OUT_LOW)); - if (IS_ERR(spi_get_csgpiod(spi, 0))) { - ret = PTR_ERR(spi_get_csgpiod(spi, 0)); + gpiod_add_lookup_table(lookup); + + bs->cs_gpio = gpiod_get(&spi->dev, "cs", GPIOD_OUT_LOW); + gpiod_remove_lookup_table(lookup); + if (IS_ERR(bs->cs_gpio)) { + ret = PTR_ERR(bs->cs_gpio); goto err_cleanup; } + spi_set_csgpiod(spi, 0, bs->cs_gpio); + /* and set up the "mode" and level */ dev_info(&spi->dev, "setting up native-CS%i to use GPIO\n", spi_get_chipselect(spi, 0));