From patchwork Thu Aug 31 19:49:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 13371832 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 C8188C83F2F for ; Thu, 31 Aug 2023 19:50:35 +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=loQ/LFHUaRDq5CIfiF8+UcAL/Gt+kVXv9Oa2ENiD4Rk=; b=o5NfqqXbFCQ5B7 yr3bmcMsTfvn+AS3QcO1eT1RImPio4D+mu86e7YVJuCEiAmz5cHMRMLM3DEMYyvkIcu92umfd7Teu swIKt9r+i8PS7+ZkAGbLBmjjTRe/XMY14/TwW6glZapk4LMoX7SHngKDtSXH1CNRdBfDk04tnZ0By Y/pXW9wtZbb1hWfU/sagtkK/lthSFELjA3BLdDbG+GIPrPStwFVHUfXbCh2twCux5TfoNckd9iXBX hYsIj1k9b6HiUoFFr3KcqbOGd3OGCiOn6kxEDCrCbEVa39aUsk0qKczmutnO/q4hxEX3GF4dHosQV yBhZifK48DfGIGtJmsUg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qbng0-00FlWM-0j; Thu, 31 Aug 2023 19:50:00 +0000 Received: from mail-wm1-x331.google.com ([2a00:1450:4864:20::331]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qbnfv-00FlVP-2E for linux-arm-kernel@lists.infradead.org; Thu, 31 Aug 2023 19:49:57 +0000 Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-401b3ea0656so11920185e9.0 for ; Thu, 31 Aug 2023 12:49:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1693511392; x=1694116192; 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=XC4h7QPz634lMnRrF4WW/WFuYrL3CG+anMN89egIVWI=; b=lA9DBn4c+oqUOoFAmo4z8V4B8Mn63hyxLVN6Qb5NVdy/rvbPAGblM+5Fuv3wBuZR+f WgZ0VjpD0FvszUfNaGn2/oQl2ab7No2gHF2zgygQftn3g2+4ixyHH1Mpv8mNuVnarxv0 jWVojS8/NGROv34KZeMrMGKU14m68me5A5xdAAnvdsvaEtvX3WpW8lE8WzEXW89L5izK zNsM/lVZmVtJsq1DHekZJs7qJ0FxHenfiroD13vK7HOKxcmB4U9WGpK0uC7+Tm3/mfaa axoryZoAy06NFjbKw/+q6epesLpoBU4aZXbD9epwNEEdeCN79TpQAQiaoCMymLGImNN8 vt4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693511392; x=1694116192; 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=XC4h7QPz634lMnRrF4WW/WFuYrL3CG+anMN89egIVWI=; b=EccaIYgZ5VswTl+xUQ1M5O6p1f2b/nde9+yA7nRoK2yDoTZX3dI2gJF+nihemC15T3 iKoFxKTHV1gHD6DXfwqQoGxBoLSrWfKIejddoY46tSllBZLt7jnMLnakbVJ+KZkmUQvz EL34h+wMfqU2adw5ZWbg5VnXY/ZRsZq/I4MNrTLGWUctDtec6mKBjALlvix5qMEBZqFr z/aj9B2iKFq3GIvkxEjwZQ7cTpykEX43iRTef7hBa3zfwHkOjJwKJ8ex4r1vBuO1T8oC fmRHpI9OGilffuMTWy3r/Tl2vKIv6JUi94P9YFpPOe6DLSpfQen9V66Y7Sby5Bf8h7+n ycNw== X-Gm-Message-State: AOJu0YzTFEwtlX1QB5KkOleViYHDDyyXOgNAHPOfgCoRI4t166WvBnQa 9lzPN64xsZwX7NAt+naSJOTp+Q== X-Google-Smtp-Source: AGHT+IH0hYYVjHkuWRlFzUZ48R1rnlngflqv6+UTjWWB2+DGqaB9XWDjqOhrDuRXhqJ+BoNANV4Rpw== X-Received: by 2002:a05:600c:b52:b0:401:b3a5:ebf8 with SMTP id k18-20020a05600c0b5200b00401b3a5ebf8mr233462wmr.16.1693511391723; Thu, 31 Aug 2023 12:49:51 -0700 (PDT) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:a203:c11c:d490:8489]) by smtp.gmail.com with ESMTPSA id k33-20020a05600c1ca100b0040210a27e29sm2893779wms.32.2023.08.31.12.49.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Aug 2023 12:49:51 -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] spi: bcm2835: reduce the abuse of the GPIO API Date: Thu, 31 Aug 2023 21:49:34 +0200 Message-Id: <20230831194934.19628-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-20230831_124955_948581_1960AB09 X-CRM114-Status: GOOD ( 25.39 ) 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 meant for GPIO providers exclusively to locate the GPIO chip it gets its CS pins from and request the relevant pin. I don't know the background and what bug forced this. I can however propose a slightly better solution that allows the driver to request the GPIO correctly using a temporary lookup table. Signed-off-by: Bartosz Golaszewski --- This is only build-tested. It should work, but it would be great if someone from broadcom could test this. drivers/spi/spi-bcm2835.c | 54 ++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/drivers/spi/spi-bcm2835.c b/drivers/spi/spi-bcm2835.c index e7bb2714678a..3c422f0e1087 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 */ @@ -117,6 +119,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,11 +1159,6 @@ 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); @@ -1221,7 +1219,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 +1286,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? + * Still the real problem is unsolved. Looks like the cs_gpiods table + * is not assigned correctly from DT? */ + 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 = devm_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));