Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp1335626pxb; Fri, 6 Nov 2020 07:10:13 -0800 (PST) X-Google-Smtp-Source: ABdhPJwsjoG5+benW2jtp6BzZi4CXKQRT54FRLj2OSlJ5TFP3Djx+h9E6Z6W7M2Dj2urNF/lrF44 X-Received: by 2002:a50:9e69:: with SMTP id z96mr2511781ede.226.1604675407666; Fri, 06 Nov 2020 07:10:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604675407; cv=none; d=google.com; s=arc-20160816; b=U+Bmxi0Qaj/RWBELpL6xTvYHRDjmtLrkx7ykDdpfGTxwxsQnb+dS7eLDaPODJ2zRW/ xN/tvUD0rXjxmNptnWddZowXs8QIu67iNTQ+GuWpRoLLQd51mJSDDPlkJp0jHp7R4lXm La8s2FsTKWHxH5Q7Jvk0vlPlgqu/I6AKFOHQ/fd1Fel8hQ5JOB+90HGVI9EH822b9sZn ES+aQjfCTU67DqyAUoIHrOgwE/s/LNW6JvrHrazFS6RjRfnIdKOFN0I9Nlacbs2OvzEp hMT0hsZilVOTvIAEkEce6ClkeylJikX2q5NoYqBsClrgkXts08RRE0syS54e7nYg1PKd D/5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from :dkim-signature; bh=PVN4GnwT1M8qbwSdJv3enlb0YS6kxWNKqU65jyrdvdk=; b=F8UPkhfrvx1xv/scNI4keOssIifQ6VrEm7oXV+HnlsMpiS4jEw5hzD1pEuu58l1FdI ZdnNMm2bw0JnrkXQEC3AL/vmudTA+SObsbFC2ckwC0LCVO6HWKoFXHRYtrT0voj95LXD 07ezp2/yKiom2zzwDHJjnU1gpqaplUvd9jCA6v3Lmrfn29o/BlSeB+TESp2Vb4BWk8M/ Lqi8PaX1VVzn7TlLpp4J8rwZbhforzxrSFLKce+onWrPv8aRMjr0/VfdOjlgQrFNxkSl Er6bYZj+bJLU9waqp0DTcb7lF4R/dxtcN04/yhPWE1fmv5wM0tdZBS4YDok+AmsaGT+B pAuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Kg5lc83U; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v5si1621242edi.14.2020.11.06.07.09.39; Fri, 06 Nov 2020 07:10:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Kg5lc83U; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727020AbgKFPHN (ORCPT + 99 others); Fri, 6 Nov 2020 10:07:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49782 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726708AbgKFPHL (ORCPT ); Fri, 6 Nov 2020 10:07:11 -0500 Received: from mail-qk1-x742.google.com (mail-qk1-x742.google.com [IPv6:2607:f8b0:4864:20::742]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 91E7CC0613CF; Fri, 6 Nov 2020 07:07:11 -0800 (PST) Received: by mail-qk1-x742.google.com with SMTP id s14so1284636qkg.11; Fri, 06 Nov 2020 07:07:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=PVN4GnwT1M8qbwSdJv3enlb0YS6kxWNKqU65jyrdvdk=; b=Kg5lc83UF6M93kViL/jvkSmmIgxzuf7yhlb5awT4Ng7xoHi4QkpYVIn94V0H0b7dwj 0RiZeoypQGPTzMMue6rmdqQQeYNhlE813lXKpDHzEzg/+dB/jfDkq5VklMT8b3I1795h Z/cNEABj/QIBQz2LwAGNOYioYZB+Q/jYiGL/tCTuhJnI6zSHqbrjdWfewblZvxnDcWjv kapEEs9MArRTm+Ui2D8x9EaAOq90UGLFZn7iUAIpoS7P8Zsm3rYvQMhGJQ50C4HSbCfi i/eSmbjOC0RUP05DYuedZ1NQmTcnSqm6jHKFpZGAfvZWAK+mYg9V0b8gFuwpOYBTKgQy YmKg== 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; bh=PVN4GnwT1M8qbwSdJv3enlb0YS6kxWNKqU65jyrdvdk=; b=le8kXP+5625iK2qiW4iTgG1p6SahXibooHuYfxgEkfYfXJ//xqHe2686XpXIW9LKdw CQUpUBerD5S2ylVNk5SvewGN8EfoINAuTELK+Qxk1ut40qwj84VixlBzmeAEqTFX0BVl dgdEczpF1ExrGlRUqSGqfvAUsVhP9GiNr9MkirwN01IUKmPWT0WfcCSRKP7+mupcB2oq NNJSLPvx/2qWWqf+srAZW3X7wl6aOYQverl3sz5+qj1w67MU1y4uOwSBMy605iDzGXhG SEdQPRa8yxi4JaV51HOGjOKTMvdxoLXFP9ASB8EujaEprTh7NzW7HEiPbvyqpNJ70nJg cEVQ== X-Gm-Message-State: AOAM532wclC14myEO/3HziNIPLcjY9W2kNxktteUwcvL3PFpSCUMd8MW owxgO7nMcr0vjoekajXYSWo= X-Received: by 2002:a05:620a:1024:: with SMTP id a4mr2003922qkk.390.1604675230712; Fri, 06 Nov 2020 07:07:10 -0800 (PST) Received: from localhost.localdomain ([198.52.185.246]) by smtp.gmail.com with ESMTPSA id q188sm700731qka.56.2020.11.06.07.07.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Nov 2020 07:07:10 -0800 (PST) From: Sven Van Asbroeck X-Google-Original-From: Sven Van Asbroeck To: Mark Brown , Linus Walleij Cc: Sven Van Asbroeck , Jonathan Cameron , Simon Han , Lukas Wunner , linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v1] spi: fix client driver breakages when using GPIO descriptors Date: Fri, 6 Nov 2020 10:07:06 -0500 Message-Id: <20201106150706.29089-1-TheSven73@gmail.com> X-Mailer: git-send-email 2.17.1 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sven Van Asbroeck Commit f3186dd87669 ("spi: Optionally use GPIO descriptors for CS GPIOs") introduced the optional use of GPIO descriptors for chip selects. A side-effect of this change: when a SPI bus uses GPIO descriptors, all its client devices have SPI_CS_HIGH set in spi->mode. This flag is required for the SPI bus to operate correctly. This unfortunately breaks many client drivers, which use the following pattern to configure their underlying SPI bus: static int client_device_probe(struct spi_device *spi) { ... spi->mode = SPI_MODE_0; spi->bits_per_word = 8; err = spi_setup(spi); .. } In short, many client drivers overwrite the SPI_CS_HIGH bit in spi->mode, and break the underlying SPI bus driver. This is especially true for Freescale/NXP imx ecspi, where large numbers of spi client drivers now no longer work. Proposed fix: ------------- When using gpio descriptors, depend on gpiolib to handle CS polarity. Existing quirks in gpiolib ensure that this is handled correctly. Existing gpiolib behaviour will force the polarity of any chip-select gpiod to active-high (if 'spi-active-high' devicetree prop present) or active-low (if 'spi-active-high' absent). Irrespective of whether the gpio is marked GPIO_ACTIVE_[HIGH|LOW] in the devicetree. Loose ends: ----------- If this fix is applied: - is commit 138c9c32f090 ("spi: spidev: Fix CS polarity if GPIO descriptors are used") still necessary / correct ? Fixes: f3186dd87669 ("spi: Optionally use GPIO descriptors for CS GPIOs") Signed-off-by: Sven Van Asbroeck --- Tree: v5.10-rc2 To: Mark Brown To: Linus Walleij Cc: Jonathan Cameron Cc: Simon Han Cc: Lukas Wunner Cc: linux-spi@vger.kernel.org Cc: linux-kernel@vger.kernel.org drivers/spi/spi.c | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index 0cab239d8e7f..7566482c052c 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -812,18 +812,16 @@ static void spi_set_cs(struct spi_device *spi, bool enable) enable = !enable; if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio)) { - /* - * Honour the SPI_NO_CS flag and invert the enable line, as - * active low is default for SPI. Execution paths that handle - * polarity inversion in gpiolib (such as device tree) will - * enforce active high using the SPI_CS_HIGH resulting in a - * double inversion through the code above. - */ if (!(spi->mode & SPI_NO_CS)) { if (spi->cs_gpiod) + /* polarity handled by gpiolib */ gpiod_set_value_cansleep(spi->cs_gpiod, - !enable); + enable1); else + /* + * invert the enable line, as active low is + * default for SPI. + */ gpio_set_value_cansleep(spi->cs_gpio, !enable); } /* Some SPI masters need both GPIO CS & slave_select */ @@ -1992,15 +1990,6 @@ static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi, } spi->chip_select = value; - /* - * For descriptors associated with the device, polarity inversion is - * handled in the gpiolib, so all gpio chip selects are "active high" - * in the logical sense, the gpiolib will invert the line if need be. - */ - if ((ctlr->use_gpio_descriptors) && ctlr->cs_gpiods && - ctlr->cs_gpiods[spi->chip_select]) - spi->mode |= SPI_CS_HIGH; - /* Device speed */ if (!of_property_read_u32(nc, "spi-max-frequency", &value)) spi->max_speed_hz = value; -- 2.17.1