From patchwork Thu Sep 12 14:43:07 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Merello X-Patchwork-Id: 11143253 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 52A8E13BD for ; Thu, 12 Sep 2019 14:43:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 3060F20856 for ; Thu, 12 Sep 2019 14:43:37 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Z6ECsUYp" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732783AbfILOng (ORCPT ); Thu, 12 Sep 2019 10:43:36 -0400 Received: from mail-wm1-f65.google.com ([209.85.128.65]:56256 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732722AbfILOng (ORCPT ); Thu, 12 Sep 2019 10:43:36 -0400 Received: by mail-wm1-f65.google.com with SMTP id g207so329512wmg.5 for ; Thu, 12 Sep 2019 07:43:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=2L6nEw3n0siJ/8DCjgw0WGkK1bU5suHRoNfpehW8eSU=; b=Z6ECsUYputhDYXn+te2bDfbLPm+7JoT4Ktuj1CAIn6atPgxZnaITVakzQCIhM32M0X Jboq57YlD+ZZHGw1NMhHfojkc9C6r38tt58scNujNlG4vD9WzIZbgin8RgXkTPDE/Hsj PAMT9dT8UHsWcCXdIdfizUC0S6B0Qp1RYZKjdj/JeUicWBOn+baddEfzg/vFNQJy67OO QZOhIzTJRIccalYl019GhgUHveFo7Q9iNGWCm3d/7uA9YUEfKjyVMfj4QoDQfAu6GiMe daG7etVMZh2+ujBDUs5dKLC1lihhOoM8UBxC1RAypKWOxsTIPtXIIjjAj1TPt9KZzLnQ BOfw== 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=2L6nEw3n0siJ/8DCjgw0WGkK1bU5suHRoNfpehW8eSU=; b=OcryXPI4X9S4SJsMkPPFT6QSEwtt5e8EMFMFGm+UkdF3qoFPP/SA2FnpaF2xc1ppTR bF3Rsu/3T7Y5ghQiTsx8Ea1NRw6Zcpm96cdM5Oqe1KbUIa9tzU1Nfv1/isjCO2/j5XhA jv57Om46lTUo1KXCMIBwrOC/N3nrmn4NQZaLumN5Cy5Iy82i1NnG6le4CN0348TOvZRs 7K4Z2i2fS/fZw3wE/9FUkcjZLuM5hjfukRk0dBin0UYJ3F7MuauEKQsrskVRbbh6IZfu EEF5tt11Qzazp7hrUNbZWDZtSIGetplliZ1H4Y8R0QvtuXV8heH4apJfJFlmOhMDBqKN KfBg== X-Gm-Message-State: APjAAAVfAhiYAPoBjr4jt1h131K18qJH7FYAVP4zt7ZK1OwYodVlgZlf cQsfmCf1DwP6MrKMLf5VVus= X-Google-Smtp-Source: APXvYqyvDJg6H97dpelGzFnnR8QVTAconGLfJK5uYOqWMPs8Torg527Ehyw34cWpkji3Z8H1Hop+xA== X-Received: by 2002:a7b:cf11:: with SMTP id l17mr305413wmg.162.1568299414737; Thu, 12 Sep 2019 07:43:34 -0700 (PDT) Received: from NewMoon.iit.local ([90.147.180.254]) by smtp.gmail.com with ESMTPSA id q19sm35175256wra.89.2019.09.12.07.43.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2019 07:43:33 -0700 (PDT) From: Andrea Merello To: lars@metafoo.de, Michael.Hennerich@analog.com, jic23@kernel.org, knaack.h@gmx.de, pmeerw@pmeerw.net Cc: linux-iio@vger.kernel.org, Andrea Merello Subject: [PATCH 1/4] iio: ad7949: kill pointless "readback"-handling code Date: Thu, 12 Sep 2019 16:43:07 +0200 Message-Id: <20190912144310.7458-2-andrea.merello@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190912144310.7458-1-andrea.merello@gmail.com> References: <20190912144310.7458-1-andrea.merello@gmail.com> Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org The device could be configured to spit out also the configuration word while reading the AD result value (in the same SPI xfer) - this is called "readback" in the device datasheet. The driver checks if readback is enabled and it eventually adjusts the SPI xfer length and it applies proper shifts to still get the data, discarding the configuration word. The readback option is actually never enabled (the driver disables it), so the said checks do not serve for any purpose. Since enabling the readback option seems not to provide any advantage (the driver entirely sets the configuration word without relying on any default value), just kill the said, unused, code. Signed-off-by: Andrea Merello Reviewed-by: Alexandru Ardelean --- drivers/iio/adc/ad7949.c | 27 +++------------------------ 1 file changed, 3 insertions(+), 24 deletions(-) diff --git a/drivers/iio/adc/ad7949.c b/drivers/iio/adc/ad7949.c index ac0ffff6c5ae..518044c31a73 100644 --- a/drivers/iio/adc/ad7949.c +++ b/drivers/iio/adc/ad7949.c @@ -57,29 +57,11 @@ struct ad7949_adc_chip { u32 buffer ____cacheline_aligned; }; -static bool ad7949_spi_cfg_is_read_back(struct ad7949_adc_chip *ad7949_adc) -{ - if (!(ad7949_adc->cfg & AD7949_CFG_READ_BACK)) - return true; - - return false; -} - -static int ad7949_spi_bits_per_word(struct ad7949_adc_chip *ad7949_adc) -{ - int ret = ad7949_adc->resolution; - - if (ad7949_spi_cfg_is_read_back(ad7949_adc)) - ret += AD7949_CFG_REG_SIZE_BITS; - - return ret; -} - static int ad7949_spi_write_cfg(struct ad7949_adc_chip *ad7949_adc, u16 val, u16 mask) { int ret; - int bits_per_word = ad7949_spi_bits_per_word(ad7949_adc); + int bits_per_word = ad7949_adc->resolution; int shift = bits_per_word - AD7949_CFG_REG_SIZE_BITS; struct spi_message msg; struct spi_transfer tx[] = { @@ -107,7 +89,7 @@ static int ad7949_spi_read_channel(struct ad7949_adc_chip *ad7949_adc, int *val, unsigned int channel) { int ret; - int bits_per_word = ad7949_spi_bits_per_word(ad7949_adc); + int bits_per_word = ad7949_adc->resolution; int mask = GENMASK(ad7949_adc->resolution, 0); struct spi_message msg; struct spi_transfer tx[] = { @@ -138,10 +120,7 @@ static int ad7949_spi_read_channel(struct ad7949_adc_chip *ad7949_adc, int *val, ad7949_adc->current_channel = channel; - if (ad7949_spi_cfg_is_read_back(ad7949_adc)) - *val = (ad7949_adc->buffer >> AD7949_CFG_REG_SIZE_BITS) & mask; - else - *val = ad7949_adc->buffer & mask; + *val = ad7949_adc->buffer & mask; return 0; } From patchwork Thu Sep 12 14:43:08 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Merello X-Patchwork-Id: 11143255 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7827113BD for ; Thu, 12 Sep 2019 14:43:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 5576120CC7 for ; Thu, 12 Sep 2019 14:43:38 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hyBMDEUa" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732784AbfILOnh (ORCPT ); Thu, 12 Sep 2019 10:43:37 -0400 Received: from mail-wr1-f66.google.com ([209.85.221.66]:45377 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732708AbfILOnh (ORCPT ); Thu, 12 Sep 2019 10:43:37 -0400 Received: by mail-wr1-f66.google.com with SMTP id l16so28681073wrv.12 for ; Thu, 12 Sep 2019 07:43:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=8qKtktIW48wn6megrwv4Wyp2PCeXeuD2/owEpjgyYCs=; b=hyBMDEUalzb8J7W+9zqEhhCOXYLK8Ca9zLJaXcVgDvPjcCcaDImGPHjKEZcetx5XEA cbhSmkA/jzDD+mtq29QoK2h3fkcQ9MAk514EalyZHlmkmob5eM60y7sGhSlphQU039dI 0mnl642tMdwXIESRav0LyB1Qhefb1ypcgbzX98ulRBGnDzCC5u6JofrSntn1zLys/nDj CBuuAgPn4VhIikC7jSjNu7ux9jSUoz6JE709DyxmuuHZM8G7PpRFtW9YClUzUdQcNXsH b6ZlG2vn7SSo9vGXuwtbovAx/cpIuHoCqPTMw/Form4mkL79dv/mVM3JHGH+Ox3Y2Dbq JKuQ== 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=8qKtktIW48wn6megrwv4Wyp2PCeXeuD2/owEpjgyYCs=; b=Kd8YE/hA8jp8+L0tmuaQTTYylJCZVvlkAaEcmjVMf2qfTFIarpnp3vT2d4JBDXz8cK gaQ60dt4BQD6fwcwkePyAmZoe0mT4eebxYhRbdbryo653cGl9kR8IsWhK5ALvnmsgfqC xVkwHkuqmPLAHQ/Z++TCwltApYOsEvbjSt+MdGJkRTrRNEX2UwrJ9wsKk9R92PmSsoiF yzYGW5Yuq50QdpoBOJhELJhRYmWkgVnUR7CLQ/U6712feeuImtZLD4TS9bnrvnr1AcqH 817LIxLO8Nyk1L9CvliB9rFqj2aTCYku0SX9/lKzc5YFeCVvoX0fLE5QilJ0PYZa3pC4 DmiQ== X-Gm-Message-State: APjAAAUgIGOstI2OjJy5ycBNhqbvLLzhhtDXQ0YnjUtkJTyDto9YgZDA z7VI3bM7ivo2Q97YeM3xoP0= X-Google-Smtp-Source: APXvYqzr1Taq5m0PJ7mYDj3qzajsGPCjCukotrGI+P6LNTTNJh4175oTngnzbfQiHAFb32MEY6TrhQ== X-Received: by 2002:adf:cd8e:: with SMTP id q14mr13274195wrj.187.1568299415797; Thu, 12 Sep 2019 07:43:35 -0700 (PDT) Received: from NewMoon.iit.local ([90.147.180.254]) by smtp.gmail.com with ESMTPSA id q19sm35175256wra.89.2019.09.12.07.43.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2019 07:43:35 -0700 (PDT) From: Andrea Merello To: lars@metafoo.de, Michael.Hennerich@analog.com, jic23@kernel.org, knaack.h@gmx.de, pmeerw@pmeerw.net Cc: linux-iio@vger.kernel.org, Andrea Merello Subject: [PATCH 2/4] iio: ad7949: fix incorrect SPI xfer len Date: Thu, 12 Sep 2019 16:43:08 +0200 Message-Id: <20190912144310.7458-3-andrea.merello@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190912144310.7458-1-andrea.merello@gmail.com> References: <20190912144310.7458-1-andrea.merello@gmail.com> Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org This driver supports 14-bits and 16-bits devices. All of them have a 14-bit configuration registers. All SPI trasfers, for reading AD conversion results and for writing the configuration register, fit in two bytes. The driver always uses 4-bytes xfers which seems at least pointless (maybe even harmful). This patch trims the SPI xfer len and the buffer size to two bytes. Signed-off-by: Andrea Merello Reviewed-by: Alexandru Ardelean --- drivers/iio/adc/ad7949.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/iio/adc/ad7949.c b/drivers/iio/adc/ad7949.c index 518044c31a73..5c2b3446fa4a 100644 --- a/drivers/iio/adc/ad7949.c +++ b/drivers/iio/adc/ad7949.c @@ -54,7 +54,7 @@ struct ad7949_adc_chip { u8 resolution; u16 cfg; unsigned int current_channel; - u32 buffer ____cacheline_aligned; + u16 buffer ____cacheline_aligned; }; static int ad7949_spi_write_cfg(struct ad7949_adc_chip *ad7949_adc, u16 val, @@ -67,7 +67,7 @@ static int ad7949_spi_write_cfg(struct ad7949_adc_chip *ad7949_adc, u16 val, struct spi_transfer tx[] = { { .tx_buf = &ad7949_adc->buffer, - .len = 4, + .len = 2, .bits_per_word = bits_per_word, }, }; @@ -95,7 +95,7 @@ static int ad7949_spi_read_channel(struct ad7949_adc_chip *ad7949_adc, int *val, struct spi_transfer tx[] = { { .rx_buf = &ad7949_adc->buffer, - .len = 4, + .len = 2, .bits_per_word = bits_per_word, }, }; From patchwork Thu Sep 12 14:43:09 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Merello X-Patchwork-Id: 11143257 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B1B88924 for ; Thu, 12 Sep 2019 14:43:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 9051B20856 for ; Thu, 12 Sep 2019 14:43:40 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="eiw6Ha65" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732799AbfILOnj (ORCPT ); Thu, 12 Sep 2019 10:43:39 -0400 Received: from mail-wr1-f66.google.com ([209.85.221.66]:46199 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732722AbfILOnj (ORCPT ); Thu, 12 Sep 2019 10:43:39 -0400 Received: by mail-wr1-f66.google.com with SMTP id d17so15988261wrq.13 for ; Thu, 12 Sep 2019 07:43:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=0S5huaYKgRvwr4SWApclRaTXV4uX21BxatCAo87A5Sw=; b=eiw6Ha65281PYedEzUJ6eCGWsRU8vMM9yb2R4gDzPkWQ3PeKZOZPdMEwQKAyvsXD/2 F6+H+0eQAlqUTFfy03CcTWXcclPCTGW3Wfhz8nPkuyFRucNjgnUcB6atJg7wYt+VcWam 6Kh1dQY014qV2XmUxgfDDodHztwA4kBxRVVnVwV7cH7GO23LuE9D4gn/8DZ/A3U2/T81 mpIreyCP/33owO3a0D06RQAgTgCv4TaZc7hZV95MrocYpuNe4zlbi5H1qbuFj70d5LYa LizkVDr76XyJztC50ZH8bHvIF34+zIbP3hUC0m6JUzEEMAVRcXhQ4QPmIKDvkDEleIWz mhWQ== 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=0S5huaYKgRvwr4SWApclRaTXV4uX21BxatCAo87A5Sw=; b=n2sGj3/hRwIr+r29pnREUG+kQ1e6DzOZO3nTkJrrXlEqugYks6PmVUv29nyNYDpCUJ MeElIvQ00fWEpldwSSGq5qAr9zF0kwXpa0SxIyzI08W9mIfPAvxksF8qu+32fYm8Ytad CdOey04+/2PbHZUgSqLM8cUr232RFDJhJ5MfESiiqWsNFkoV2gM9FaDEHB88rfUczBk4 9OaWF5gZnjCif33jk5NFBXab9Iz0F77HNWuCFUWEfgKkFoErFEZKJTFfcrt7Gb2Vx0O1 yJekFnFFWtohKi6+SaKubonNYEO2DPXl0vP8rve3NSewfaFdm3GY3mnAiiWcC7LYfBhs n2DA== X-Gm-Message-State: APjAAAWewNPHXMlW1IQF+xrJvyPVplGnH7OF+E50SSLRKZD1tf6zHRqg yhb53RGBA5dxJ7ZyBnGIFU8= X-Google-Smtp-Source: APXvYqzOEii8Hz5BW3kZOjyQVBUN/zAP+E7Q7hVE9sHFvH4SPiGHp8dwjVYoeUKwT+H8oRAw2X0zHA== X-Received: by 2002:a5d:5281:: with SMTP id c1mr3770078wrv.339.1568299416955; Thu, 12 Sep 2019 07:43:36 -0700 (PDT) Received: from NewMoon.iit.local ([90.147.180.254]) by smtp.gmail.com with ESMTPSA id q19sm35175256wra.89.2019.09.12.07.43.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2019 07:43:36 -0700 (PDT) From: Andrea Merello To: lars@metafoo.de, Michael.Hennerich@analog.com, jic23@kernel.org, knaack.h@gmx.de, pmeerw@pmeerw.net Cc: linux-iio@vger.kernel.org, Andrea Merello Subject: [PATCH 3/4] iio: ad7949: fix SPI xfer delays Date: Thu, 12 Sep 2019 16:43:09 +0200 Message-Id: <20190912144310.7458-4-andrea.merello@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190912144310.7458-1-andrea.merello@gmail.com> References: <20190912144310.7458-1-andrea.merello@gmail.com> Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org The driver calls udelay(2) after each SPI xfer. However, according to the specifications, the SPI timing should be as follows: 1- The end of SPI xfer (CNV/CS rising edge) causes the device to initiate the conversion phase, which takes up to 2.2uS. 2- At the end of the conversion phase, the device starts the acquisition phase for the next conversion automatically (regardless to the state of CNV pin); the conversion phase should last at least 1.8 uS The whole cycle timing is thus 4uS long. The SPI data is read during the acquisition phase (RAC mode, no need to worry about "Tdata"). In order to be compliant wrt these timing specifications we should wait 4uS after each SPI xfer (that is conservative, because there is also the SPI xfer duration itself - which at the maximum supported clock should be about 320nS). This patch enlarges the delay up to 4uS and it also removes the explicit calls to udelay(), relying on spi_transfer->delay_usecs. Signed-off-by: Andrea Merello --- drivers/iio/adc/ad7949.c | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/drivers/iio/adc/ad7949.c b/drivers/iio/adc/ad7949.c index 5c2b3446fa4a..25d1e1b24257 100644 --- a/drivers/iio/adc/ad7949.c +++ b/drivers/iio/adc/ad7949.c @@ -69,6 +69,7 @@ static int ad7949_spi_write_cfg(struct ad7949_adc_chip *ad7949_adc, u16 val, .tx_buf = &ad7949_adc->buffer, .len = 2, .bits_per_word = bits_per_word, + .delay_usecs = 4, }, }; @@ -77,11 +78,6 @@ static int ad7949_spi_write_cfg(struct ad7949_adc_chip *ad7949_adc, u16 val, spi_message_init_with_transfers(&msg, tx, 1); ret = spi_sync(ad7949_adc->spi, &msg); - /* - * This delay is to avoid a new request before the required time to - * send a new command to the device - */ - udelay(2); return ret; } @@ -97,6 +93,7 @@ static int ad7949_spi_read_channel(struct ad7949_adc_chip *ad7949_adc, int *val, .rx_buf = &ad7949_adc->buffer, .len = 2, .bits_per_word = bits_per_word, + .delay_usecs = 4, }, }; @@ -112,12 +109,6 @@ static int ad7949_spi_read_channel(struct ad7949_adc_chip *ad7949_adc, int *val, if (ret) return ret; - /* - * This delay is to avoid a new request before the required time to - * send a new command to the device - */ - udelay(2); - ad7949_adc->current_channel = channel; *val = ad7949_adc->buffer & mask; From patchwork Thu Sep 12 14:43:10 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Merello X-Patchwork-Id: 11143259 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E8A411599 for ; Thu, 12 Sep 2019 14:43:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id C74A220856 for ; Thu, 12 Sep 2019 14:43:40 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="GC5qOSUz" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732722AbfILOnk (ORCPT ); Thu, 12 Sep 2019 10:43:40 -0400 Received: from mail-wr1-f68.google.com ([209.85.221.68]:45382 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732654AbfILOnj (ORCPT ); Thu, 12 Sep 2019 10:43:39 -0400 Received: by mail-wr1-f68.google.com with SMTP id l16so28681188wrv.12 for ; Thu, 12 Sep 2019 07:43:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=LxB2q1Y40lbfz2eJu63DEyuUv8iiUDK1+WsBkxB5X8I=; b=GC5qOSUzjcOMMSTkHH3Ts4FYcAwJakJSPuZ54LTM2W98ycN0hoUsTw/Qmqk3WgnnmK oMZIa3KmbJsaxsnonDV0/BVEK9y0AB/nQOqEo6m7dRDn62BE5bpqVVnxeJh5CwtAhk0e hW/jpeWUoIphs6r1xtVUaEKjFN7GR0lfqBxwyWnomEzaKwwv+z4rcepSwKkhULA1yb+l RMNQnGM8RtoHdb2QXKzPmfS5U0HkCh7Xqi/aZzppx5o7SDT9ykRRKn1myUHalWmlbSH4 cjM4vbsoOWOh/E2Qw/MCxqqSmvCQLNPIkhb8wg47TEkCMPACV7IEL2FFs8vGEldCCkst wgQQ== 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=LxB2q1Y40lbfz2eJu63DEyuUv8iiUDK1+WsBkxB5X8I=; b=f8vkfaTc079A4AhganxQE0w4tLe+ESdG0E8HECOW9+Ak3+NVtRvHEnXovgTXw1sWJy uT15veWapf1iPiS2ll+hfi4y0bnDbTq4ZR40H5RC8DpZt/0q849WHgGmFhHQjSihzfR0 WnUZrB7Yhb1bctsq6GhezOyIU2u47ORgx8NZrybUuyADc/70EE8wEH97e2mGeqxnsq7i NqCSQpdT6EgKPemZJk+kTDrDsl3zvGyLIOCRleF4klxnkd71L4sFCDKmTpi8dq35xMxr Xp1e9Q4GgzWnRvq0By6oPBaXh/zp4/RxApNpTIsquKI9f2ck7wg1xso2O/Ot+y7K+2Ne /BBg== X-Gm-Message-State: APjAAAVfpSPjAVOr2HO0jcw4BPKwSZA6KlrvmWYLapYHfd8ju99ftDrG rJneaK3261nEo/xoX5Y9E/s= X-Google-Smtp-Source: APXvYqyGlHvAmDME1asPRpaLM+IL53z785wZ6P77jjCH3lfqHAorwB/HmQ5X8EgXNO56jjob0+XrjQ== X-Received: by 2002:adf:ec8b:: with SMTP id z11mr940121wrn.207.1568299417982; Thu, 12 Sep 2019 07:43:37 -0700 (PDT) Received: from NewMoon.iit.local ([90.147.180.254]) by smtp.gmail.com with ESMTPSA id q19sm35175256wra.89.2019.09.12.07.43.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2019 07:43:37 -0700 (PDT) From: Andrea Merello To: lars@metafoo.de, Michael.Hennerich@analog.com, jic23@kernel.org, knaack.h@gmx.de, pmeerw@pmeerw.net Cc: linux-iio@vger.kernel.org, Andrea Merello Subject: [PATCH 4/4] iio: ad7949: fix channels mixups Date: Thu, 12 Sep 2019 16:43:10 +0200 Message-Id: <20190912144310.7458-5-andrea.merello@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190912144310.7458-1-andrea.merello@gmail.com> References: <20190912144310.7458-1-andrea.merello@gmail.com> Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Each time we need to read a sample the driver writes the CFG register (setting the channel to be read in such register) and then it performs another xfer to read the resulting value. This does not work correctly because while writing the CFG register the acquisition phase is ongoing using the _previous_ CFG settings. Then the device performs the conversion during xfer delay on the voltage stored duting the said acquisitaion phase. Finally the driver performs the read (during the next acquisition phase, which is the one done with the right settings) and it gets the last converted value, that is the wrong data. In case the configuration is not actually changed, then we still get correct data, but in case the configuration changes (and this happens e.g. switching the MUX on another channel), we get wrong data (data from the previously selected channel). This patch fixes this by performing one more "dummy" transfer in order to ending up in reading the data when it's really ready. Signed-off-by: Andrea Merello --- drivers/iio/adc/ad7949.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/drivers/iio/adc/ad7949.c b/drivers/iio/adc/ad7949.c index 25d1e1b24257..b1dbe2075ca9 100644 --- a/drivers/iio/adc/ad7949.c +++ b/drivers/iio/adc/ad7949.c @@ -85,6 +85,7 @@ static int ad7949_spi_read_channel(struct ad7949_adc_chip *ad7949_adc, int *val, unsigned int channel) { int ret; + int i; int bits_per_word = ad7949_adc->resolution; int mask = GENMASK(ad7949_adc->resolution, 0); struct spi_message msg; @@ -97,12 +98,19 @@ static int ad7949_spi_read_channel(struct ad7949_adc_chip *ad7949_adc, int *val, }, }; - ret = ad7949_spi_write_cfg(ad7949_adc, - channel << AD7949_OFFSET_CHANNEL_SEL, - AD7949_MASK_CHANNEL_SEL); - if (ret) - return ret; + /* + * 1: write CFG for sample 'n' and read garbage (sample n-2) + * 2: write something and read garbage (sample n-1) + */ + for (i = 0; i < 2; i++) { + ret = ad7949_spi_write_cfg(ad7949_adc, + channel << AD7949_OFFSET_CHANNEL_SEL, + AD7949_MASK_CHANNEL_SEL); + if (ret) + return ret; + } + /* 3: write something and read data for sample 'n' */ ad7949_adc->buffer = 0; spi_message_init_with_transfers(&msg, tx, 1); ret = spi_sync(ad7949_adc->spi, &msg);