From patchwork Fri Apr 19 15:36:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nuno Sa via B4 Relay X-Patchwork-Id: 13636518 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4B2A112FB16; Fri, 19 Apr 2024 15:36:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713541012; cv=none; b=amHxpyR82XbovW3/cHMKY0NQ1DsSsIv32OP4fZ/wYX6JvZDS9gPOl3DSIlYjbl1yaHXVqtcXqJx/Fqa18FfNUn9YMTD8vD/69c8WducALXVN4szDiR8FfAx83JKBi1LAWr7rRxZiPOkTWNR0ksFAx/VQb05GPYV8mukgxeZxqps= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713541012; c=relaxed/simple; bh=OT1f5HQ85Uw5dWLz6Hgn1lTmPE4744OaSYCe5yedbzQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=fsUl+Ccuc6rnxpEF+J/T1dGvc4z+Y13QNxc6/HczxBSg3WzrkhFFl15AUsJC8U2ZoHTlG0bRoWd1UxI9dJyH6CeA7cJWoIfx9LQKwz6FfqQpEOZgJdbCpCFh0owUB53LLtZCF+7HdXX1E60pZUB6SUJM22udJ0zlC4O9S1HJrrs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NjYffbgJ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="NjYffbgJ" Received: by smtp.kernel.org (Postfix) with ESMTPS id EB53AC3277B; Fri, 19 Apr 2024 15:36:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713541012; bh=OT1f5HQ85Uw5dWLz6Hgn1lTmPE4744OaSYCe5yedbzQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=NjYffbgJg3Q4Bxx6lFC9MCSFlCiUPp+9G2qjRzPe1CMzA5jYIka2dzM285FDtZ5ST 0GhnRe3Pdj0kw1ika+QCYn6vrqtCF4YbDeraU9BV7tE5lkHaEQHoqjdow2E88i9rcY pM9AjLlSL3oVTOwhNg2vlMBbaBaCS5K6WS2iuDyPqLlhnyO/c+xxa5DTAmVAFrxGiX mlrzUjgi8aOvKSawUaSnCL6Rpp4bZ21ni/+Kld8yDNEe46GSn5ztgcxzJScOTsGrep UCsGYr6WPcOJFdRERzSlukAzKOTkBE39PV6A2VBMDmZ0DO6CR9yYZ6qsKiF7yVblMP NGRDoPgdKRnJw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id D8C5AC04FF6; Fri, 19 Apr 2024 15:36:51 +0000 (UTC) From: Nuno Sa via B4 Relay Date: Fri, 19 Apr 2024 17:36:44 +0200 Subject: [PATCH 1/8] iio: backend: add API for interface tuning Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240419-ad9467-new-features-v1-1-3e7628ff6d5e@analog.com> References: <20240419-ad9467-new-features-v1-0-3e7628ff6d5e@analog.com> In-Reply-To: <20240419-ad9467-new-features-v1-0-3e7628ff6d5e@analog.com> To: linux-iio@vger.kernel.org, devicetree@vger.kernel.org Cc: Jonathan Cameron , Alexandru Ardelean , Lars-Peter Clausen , Michael Hennerich , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Olivier Moysan , Nuno Sa X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1713541010; l=7623; i=nuno.sa@analog.com; s=20231116; h=from:subject:message-id; bh=UN9oAzFiKwoRseAjq8NdAF3wlTP1db9PUGQ2DM4QoRQ=; b=zrKmro4hJnByJECx3y8HxHJVzShYKzBsLW3PXbD9QqFSpirl9Mp95kNO7C5vmEUua+V078crW O+d96m0jpB5DkKcLVjxjWfjs6oMx8uMb20NKtTmFRMCxYYrFZTcNJMF X-Developer-Key: i=nuno.sa@analog.com; a=ed25519; pk=3NQwYA013OUYZsmDFBf8rmyyr5iQlxV/9H4/Df83o1E= X-Endpoint-Received: by B4 Relay for nuno.sa@analog.com/20231116 with auth_id=100 X-Original-From: Nuno Sa Reply-To: nuno.sa@analog.com From: Nuno Sa This is in preparation for supporting interface tuning in one for the devices using the axi-adc backend. The new added interfaces are all needed for that calibration: * iio_backend_test_pattern_set(); * iio_backend_chan_status(); * iio_backend_iodelay_set(); * iio_backend_data_sample_trigger(). Signed-off-by: Nuno Sa --- drivers/iio/industrialio-backend.c | 86 ++++++++++++++++++++++++++++++++++++++ include/linux/iio/backend.h | 57 +++++++++++++++++++++---- 2 files changed, 136 insertions(+), 7 deletions(-) diff --git a/drivers/iio/industrialio-backend.c b/drivers/iio/industrialio-backend.c index 2fea2bbbe47fd..45eea3b725a35 100644 --- a/drivers/iio/industrialio-backend.c +++ b/drivers/iio/industrialio-backend.c @@ -186,6 +186,92 @@ int iio_backend_data_format_set(struct iio_backend *back, unsigned int chan, } EXPORT_SYMBOL_NS_GPL(iio_backend_data_format_set, IIO_BACKEND); +/** + * iio_backend_test_pattern_set - Configure a test pattern + * @back: Backend device + * @chan: Channel number + * @pattern: + * + * Configure a test pattern on the backend. This is typically used for + * calibrating the timings on the data digital interface. + * + * RETURNS: + * 0 on success, negative error number on failure. + */ +int iio_backend_test_pattern_set(struct iio_backend *back, + unsigned int chan, + enum iio_backend_test_pattern pattern) +{ + if (pattern >= IIO_BACKEND_TEST_PATTERN_MAX) + return -EINVAL; + + return iio_backend_op_call(back, test_pattern_set, chan, pattern); +} +EXPORT_SYMBOL_NS_GPL(iio_backend_test_pattern_set, IIO_BACKEND); + +/** + * iio_backend_chan_status - Get the channel status + * @back: Backend device + * @chan: Channel number + * @status: Channel status + * + * Get the current state of the backend channel. Typically used to check if + * there were any errors sending/receiving data. + * + * RETURNS: + * 0 on success, negative error number on failure. + */ +int iio_backend_chan_status(struct iio_backend *back, unsigned int chan, + struct iio_backend_chan_status *status) +{ + return iio_backend_op_call(back, chan_status, chan, status); +} +EXPORT_SYMBOL_NS_GPL(iio_backend_chan_status, IIO_BACKEND); + +/** + * iio_backend_iodelay_set - Set digital I/O delay + * @back: Backend device + * @lane: Lane number + * @tap: Number of taps + * + * Controls delays on sending/receiving data. One usecase for this is to + * calibrate the data digital interface so we get the best results when + * transferring data. Note that @tap has no unit since the actual delay per tap + * is very backend specific. Hence, frontend devices typically should go through + * an array of @taps (the size of that array should typically match the size of + * calibration points on the frontend device) and call this API. + * + * RETURNS: + * 0 on success, negative error number on failure. + */ +int iio_backend_iodelay_set(struct iio_backend *back, unsigned int lane, + unsigned int tap) +{ + return iio_backend_op_call(back, iodelay_set, lane, tap); +} +EXPORT_SYMBOL_NS_GPL(iio_backend_iodelay_set, IIO_BACKEND); + +/** + * iio_backend_data_sample_trigger - Control when to sample data + * @back: Backend device + * @trigger: Data trigger + * + * Mostly useful for input backends. Configures the backend for when to sample + * data (eg: rising vs falling edge). + * + * RETURNS: + * 0 on success, negative error number on failure. + */ +int iio_backend_data_sample_trigger(struct iio_backend *back, + enum iio_backend_sample_trigger trigger) +{ + if (trigger >= IIO_BACKEND_SAMPLE_TRIGGER_MAX) + return -EINVAL; + + return iio_backend_op_call(back, data_sample_trigger, trigger); +} +EXPORT_SYMBOL_NS_GPL(iio_backend_data_sample_trigger, IIO_BACKEND); + static void iio_backend_free_buffer(void *arg) { struct iio_backend_buffer_pair *pair = arg; diff --git a/include/linux/iio/backend.h b/include/linux/iio/backend.h index a6d79381866ec..ad793fe0d78c2 100644 --- a/include/linux/iio/backend.h +++ b/include/linux/iio/backend.h @@ -15,6 +15,19 @@ enum iio_backend_data_type { IIO_BACKEND_DATA_TYPE_MAX }; +/* vendor specific from 32 */ +enum iio_backend_test_pattern { + /* modified prbs9 */ + IIO_BACKEND_ADI_PRBS_9A = 32, + IIO_BACKEND_TEST_PATTERN_MAX +}; + +enum iio_backend_sample_trigger { + IIO_BACKEND_SAMPLE_TRIGGER_EDGE_FALLING, + IIO_BACKEND_SAMPLE_TRIGGER_EDGE_RISING, + IIO_BACKEND_SAMPLE_TRIGGER_MAX +}; + /** * struct iio_backend_data_fmt - Backend data format * @type: Data type. @@ -28,15 +41,27 @@ struct iio_backend_data_fmt { bool enable; }; +/** + * struct iio_backend_chan_status - Backend channel status + * @errors: Errors occurred when sending/receiving data. + */ +struct iio_backend_chan_status { + bool errors; +}; + /** * struct iio_backend_ops - operations structure for an iio_backend - * @enable: Enable backend. - * @disable: Disable backend. - * @chan_enable: Enable one channel. - * @chan_disable: Disable one channel. - * @data_format_set: Configure the data format for a specific channel. - * @request_buffer: Request an IIO buffer. - * @free_buffer: Free an IIO buffer. + * @enable: Enable backend. + * @disable: Disable backend. + * @chan_enable: Enable one channel. + * @chan_disable: Disable one channel. + * @data_format_set: Configure the data format for a specific channel. + * @test_pattern_set: Configure a test pattern. + * @chan_status: Get the channel status. + * @iodelay_set: Set digital I/O delay. + * @data_sample_trigger: Control when to sample data. + * @request_buffer: Request an IIO buffer. + * @free_buffer: Free an IIO buffer. **/ struct iio_backend_ops { int (*enable)(struct iio_backend *back); @@ -45,6 +70,15 @@ struct iio_backend_ops { int (*chan_disable)(struct iio_backend *back, unsigned int chan); int (*data_format_set)(struct iio_backend *back, unsigned int chan, const struct iio_backend_data_fmt *data); + int (*test_pattern_set)(struct iio_backend *back, + unsigned int chan, + enum iio_backend_test_pattern pattern); + int (*chan_status)(struct iio_backend *back, unsigned int chan, + struct iio_backend_chan_status *status); + int (*iodelay_set)(struct iio_backend *back, unsigned int chan, + unsigned int tap); + int (*data_sample_trigger)(struct iio_backend *back, + enum iio_backend_sample_trigger trigger); struct iio_buffer *(*request_buffer)(struct iio_backend *back, struct iio_dev *indio_dev); void (*free_buffer)(struct iio_backend *back, @@ -56,6 +90,15 @@ int iio_backend_chan_disable(struct iio_backend *back, unsigned int chan); int devm_iio_backend_enable(struct device *dev, struct iio_backend *back); int iio_backend_data_format_set(struct iio_backend *back, unsigned int chan, const struct iio_backend_data_fmt *data); +int iio_backend_test_pattern_set(struct iio_backend *back, + unsigned int chan, + enum iio_backend_test_pattern pattern); +int iio_backend_chan_status(struct iio_backend *back, unsigned int chan, + struct iio_backend_chan_status *status); +int iio_backend_iodelay_set(struct iio_backend *back, unsigned int lane, + unsigned int tap); +int iio_backend_data_sample_trigger(struct iio_backend *back, + enum iio_backend_sample_trigger trigger); int devm_iio_backend_request_buffer(struct device *dev, struct iio_backend *back, struct iio_dev *indio_dev); From patchwork Fri Apr 19 15:36:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nuno Sa via B4 Relay X-Patchwork-Id: 13636520 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A09913049F; Fri, 19 Apr 2024 15:36:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713541012; cv=none; b=dDl6U2AlwbajMAjsLTtR5aOOQkciSV5wOKF2gT/KatOPjvGSJk7LZ3i4DAZN6rGI69ICUvJlXUAh0Y0Sbsjf7jyfuevmdvAmtekfk2Mc8AH14pbjOUA6DAam/+cFETzXZ82TilNENPJAvn7/lhnLJOV78EzVQQvPRwA201CzDNI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713541012; c=relaxed/simple; bh=M63do2rsHrBol9Y21pmkGXyr8SUKXE1yQfuo4S+xsMs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=W1at0h7vz7XjvjKtqeIrKfSa9WHacUxn6ietdeaOXzMlr7pJMzI0rXeccWV3njWeF/1v78TeW6P0ohNSqBJkTw+ieSnN8l++Fqq6cGQeBV7FNuSnCstlw8BCf1QaoUI9Iz7LOCMYZ4nKIYTvH6R/wsDr6AYDZgAQ+MnD2SyDaiA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gBSpE6pf; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="gBSpE6pf" Received: by smtp.kernel.org (Postfix) with ESMTPS id 13563C32781; Fri, 19 Apr 2024 15:36:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713541012; bh=M63do2rsHrBol9Y21pmkGXyr8SUKXE1yQfuo4S+xsMs=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=gBSpE6pfBPwdSyba8Jqysn8yJ1aw2s6fzeTykhH4nrZ1Yz6CQf0dyUZ80FqATt+mJ RIgBXC46MO3xABapu4G6TvJAAsA9+lEWBJ1XV33z8iTot4G1PjZ8ttOn9bODCnFTIq XV0Rw1pfpETZ5IYwhCj1YJ6GZGlQ7CfFVp1jqiYNsEPQTfveClcsUMvkqw5F9E+GUx lo6pT4wI+N9eZ2Ry+ICWCPcb6vC2A8AauGYgzX7FYVaES+3DGY1DDf+8eQAnQCvKiQ Zd2aj/tfFm4veoR2Rdm+v0SGTHEELJ7ELA3BmvVo6TtjVohwwfiik7sRRjYRn+UQ+F 0bo7FZxL0rBrw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id EED59C071DB; Fri, 19 Apr 2024 15:36:51 +0000 (UTC) From: Nuno Sa via B4 Relay Date: Fri, 19 Apr 2024 17:36:45 +0200 Subject: [PATCH 2/8] iio: adc: adi-axi-adc: only error out in major version mismatch Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240419-ad9467-new-features-v1-2-3e7628ff6d5e@analog.com> References: <20240419-ad9467-new-features-v1-0-3e7628ff6d5e@analog.com> In-Reply-To: <20240419-ad9467-new-features-v1-0-3e7628ff6d5e@analog.com> To: linux-iio@vger.kernel.org, devicetree@vger.kernel.org Cc: Jonathan Cameron , Alexandru Ardelean , Lars-Peter Clausen , Michael Hennerich , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Olivier Moysan , Nuno Sa X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1713541010; l=1320; i=nuno.sa@analog.com; s=20231116; h=from:subject:message-id; bh=PKeKv+/FjVf4CBXkS3IXNv+HfrXrDFDeJP+4Q7Z6xTk=; b=BFlPAe3M5OBgFWkkaWq/O6gsfGa0Vd1GEBUfwRCXcvzaGJ+FegFvmtfP1dTDZkZfHI3pR2ABp MTCRXzhpKF3AveS9Ijc58ZBmReShPeIV05HubsvboArxiYkuHx3A0nA X-Developer-Key: i=nuno.sa@analog.com; a=ed25519; pk=3NQwYA013OUYZsmDFBf8rmyyr5iQlxV/9H4/Df83o1E= X-Endpoint-Received: by B4 Relay for nuno.sa@analog.com/20231116 with auth_id=100 X-Original-From: Nuno Sa Reply-To: nuno.sa@analog.com From: Nuno Sa The IP core only has breaking changes when there major version changes. Hence, only match the major number. This is also in line with the other core ADI has upstream. The current check for erroring out 'expected_version > current_version"' is then wrong as we could just increase the core major with breaking changes and that would go unnoticed. Fixes: ef04070692a2 ("iio: adc: adi-axi-adc: add support for AXI ADC IP core") Signed-off-by: Nuno Sa --- drivers/iio/adc/adi-axi-adc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/iio/adc/adi-axi-adc.c b/drivers/iio/adc/adi-axi-adc.c index 4156639b3c8bd..a543b91124b07 100644 --- a/drivers/iio/adc/adi-axi-adc.c +++ b/drivers/iio/adc/adi-axi-adc.c @@ -207,9 +207,9 @@ static int adi_axi_adc_probe(struct platform_device *pdev) if (ret) return ret; - if (*expected_ver > ver) { + if (ADI_AXI_PCORE_VER_MAJOR(ver) != ADI_AXI_PCORE_VER_MAJOR(*expected_ver)) { dev_err(&pdev->dev, - "IP core version is too old. Expected %d.%.2d.%c, Reported %d.%.2d.%c\n", + "Major version mismatch. Expected %d.%.2d.%c, Reported %d.%.2d.%c\n", ADI_AXI_PCORE_VER_MAJOR(*expected_ver), ADI_AXI_PCORE_VER_MINOR(*expected_ver), ADI_AXI_PCORE_VER_PATCH(*expected_ver), From patchwork Fri Apr 19 15:36:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nuno Sa via B4 Relay X-Patchwork-Id: 13636522 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 711BF1304A7; Fri, 19 Apr 2024 15:36:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713541012; cv=none; b=Hb4tyMGgbEpshDeNyedLhBAfE2So+F80XbFSYilWDtcfx4RxUeiTprrIg2DJvDedgMFbYpmUQAMvOIQUjsX45eYu2t31Ya2TuafvbM0XAU54QLZsSQWNlpQiG+ZClHMm5Y06lpGoLsyZI++lb4zooEWpFsKjcBQrR/jBRlqa7TM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713541012; c=relaxed/simple; bh=KKVLahGVov+3Ev8//vGWIT9Ky/0x/0vrY7Px/D3ptpU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ccDBm2tYNKbfZAOLRU/9Z4tbkLVEOoi8RwQnW97QzBiV5KFB+QVRiOlckX6SDJ/ewjDRC7oTqQ2fuXK13bfHHT0jie5jKfndBdfewMjpohh5Lk7ysKqfIJmvROHPL0mYFiOPWPnB/TJqW322BvEM0CLwHGsoXm4gQRsPHuqTaes= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=amhmumE/; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="amhmumE/" Received: by smtp.kernel.org (Postfix) with ESMTPS id 1D19BC32786; Fri, 19 Apr 2024 15:36:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713541012; bh=KKVLahGVov+3Ev8//vGWIT9Ky/0x/0vrY7Px/D3ptpU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=amhmumE/kqag85+e3rCsGZQqUd0n9THSSUNKPmcb1E5sf4uVDpMVuJeXaV7Zq48bb Cg5Fja2J4XiqCkI3SAxRAsbsQnjbBwNaQ0pv0ns0Bgp26txkIeooy6i51kAcl1i5s5 vC8bgJa5NVNJwA04ScAN/LVlBABr/sJaZbVhmKXdsrtp3lYOXqm/PicLFoSyzqlAew ypoP9QuD9Rr28I/+ks1yQohBI6ipLu5EWOlSojrCUejnFPSjd9GsKRlUwP2pnelaAB sHqhycYHNlnP3Enh6CX9pzrF7S+b+vG/Ouea6dbe0a796srh6vr4yMl6d/71XeA1XG ghI1JtYlzZXOw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0FD9FC4345F; Fri, 19 Apr 2024 15:36:52 +0000 (UTC) From: Nuno Sa via B4 Relay Date: Fri, 19 Apr 2024 17:36:46 +0200 Subject: [PATCH 3/8] dt-bindings: adc: axi-adc: add clocks property Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240419-ad9467-new-features-v1-3-3e7628ff6d5e@analog.com> References: <20240419-ad9467-new-features-v1-0-3e7628ff6d5e@analog.com> In-Reply-To: <20240419-ad9467-new-features-v1-0-3e7628ff6d5e@analog.com> To: linux-iio@vger.kernel.org, devicetree@vger.kernel.org Cc: Jonathan Cameron , Alexandru Ardelean , Lars-Peter Clausen , Michael Hennerich , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Olivier Moysan , Nuno Sa X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1713541010; l=1048; i=nuno.sa@analog.com; s=20231116; h=from:subject:message-id; bh=ZntCSlHgOIAf1gqisnjrXuzKRZVpUr7uIRrPEaUInis=; b=HJBmv5brfYl/udSe+tJNa4T7w9PlMfU2vjNLhq6bstQ7ivb98lNF82DJyakUMqIx0+ruqr9p/ WLZEwd5JWahAPaQUKzPGm4HIAQ3H7lwNiIpXpRCmuZrPAj1LIRYCH20 X-Developer-Key: i=nuno.sa@analog.com; a=ed25519; pk=3NQwYA013OUYZsmDFBf8rmyyr5iQlxV/9H4/Df83o1E= X-Endpoint-Received: by B4 Relay for nuno.sa@analog.com/20231116 with auth_id=100 X-Original-From: Nuno Sa Reply-To: nuno.sa@analog.com From: Nuno Sa Add a required clock property as we can't access the device registers if the AXI bus clock is not properly enabled. Signed-off-by: Nuno Sa Reviewed-by: Krzysztof Kozlowski --- Documentation/devicetree/bindings/iio/adc/adi,axi-adc.yaml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Documentation/devicetree/bindings/iio/adc/adi,axi-adc.yaml b/Documentation/devicetree/bindings/iio/adc/adi,axi-adc.yaml index 3d49d21ad33df..e1f450b80db27 100644 --- a/Documentation/devicetree/bindings/iio/adc/adi,axi-adc.yaml +++ b/Documentation/devicetree/bindings/iio/adc/adi,axi-adc.yaml @@ -28,6 +28,9 @@ properties: reg: maxItems: 1 + clocks: + maxItems: 1 + dmas: maxItems: 1 @@ -48,6 +51,7 @@ required: - compatible - dmas - reg + - clocks additionalProperties: false @@ -58,6 +62,7 @@ examples: reg = <0x44a00000 0x10000>; dmas = <&rx_dma 0>; dma-names = "rx"; + clocks = <&axi_clk>; #io-backend-cells = <0>; }; ... From patchwork Fri Apr 19 15:36:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nuno Sa via B4 Relay X-Patchwork-Id: 13636521 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 71214131BC3; Fri, 19 Apr 2024 15:36:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713541012; cv=none; b=Ii42sq00aN7HxcwQphGKjVrm398XQYrUaa7A6WHEiF09Q6E/HtCa5icd42kZ5JNgqoVBieE2V9+d6wjW1y6wGC5b9vRadRHkLVXDhQL9bdv9NW5tnAloC80iwdDNdNbAgMkphLXMKxY05FniN5VeOiAZ3M9/YftqY+HatllnWSA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713541012; c=relaxed/simple; bh=GDtgREF7AuO6QjknBMOjXCBlXRJsLRlx/8Nk4OtwUys=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=gqsQojmgP10nh9MrPKcl+4GvnTAWsxaPLlDtmArj9J8HZJwPhubPZ+OFzfEjGRl0v7s93udQtNAelLALybbwjRSl3+IS8HWJmu/0cz3IrKtWVXOSXrGdfzFlIPBA8PKc7xiaR1ZKZkgDvUzlPEV7Uw5wEFVLeAOjtlhW3J3wX5g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MC4q56x6; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="MC4q56x6" Received: by smtp.kernel.org (Postfix) with ESMTPS id 2E6D3C4AF0A; Fri, 19 Apr 2024 15:36:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713541012; bh=GDtgREF7AuO6QjknBMOjXCBlXRJsLRlx/8Nk4OtwUys=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=MC4q56x6MGkNMcCSqvi6fUBv+rpCBGqlCKBLXA63yOplq4qUIPTF01XCzVWYq/mcE rlLKpIwOmcwjYzGzg4JkWuiXJXaBIxySchPiNpWgVi62k+DCoB8FM5cyJaPN8lNEUs 3lOSI1zaeN3QZRk32fw0C09Mfgekt5qtwCI1WusX+hVVy0rsVRBNrIVl97j/rwVRCn 8hr0lYdUnk67D5XpPyVFAsvXgrULNPXyzRMJKEi11WsAFxBRgENgFOWvQbxVb2plNi Sb6yH/1Av7a1g0HGWi3g6vwzcVs2hIGs02j0sN0voMtuJZfXApm76+/bJZDgpAZlCd uNe5XP38GQo4g== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 23529C04FF6; Fri, 19 Apr 2024 15:36:52 +0000 (UTC) From: Nuno Sa via B4 Relay Date: Fri, 19 Apr 2024 17:36:47 +0200 Subject: [PATCH 4/8] iio: adc: axi-adc: make sure AXI clock is enabled Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240419-ad9467-new-features-v1-4-3e7628ff6d5e@analog.com> References: <20240419-ad9467-new-features-v1-0-3e7628ff6d5e@analog.com> In-Reply-To: <20240419-ad9467-new-features-v1-0-3e7628ff6d5e@analog.com> To: linux-iio@vger.kernel.org, devicetree@vger.kernel.org Cc: Jonathan Cameron , Alexandru Ardelean , Lars-Peter Clausen , Michael Hennerich , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Olivier Moysan , Nuno Sa X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1713541010; l=1075; i=nuno.sa@analog.com; s=20231116; h=from:subject:message-id; bh=vcTpXfTnayLjDiRZnxJ+F5aPNAy5eajFubKg0Cjp4PM=; b=DJqz9cqAlXa2S8X6UlnFqgxQpwj+itH3404Ns1gnu3EjcUw0EhCiJKm9YwHY82rX9ZEvp/9R7 Mjva1Fe/xVeBDo064MiyI6ztaWaeeY9F5RsUuyOFwWyFcXGzYed56zd X-Developer-Key: i=nuno.sa@analog.com; a=ed25519; pk=3NQwYA013OUYZsmDFBf8rmyyr5iQlxV/9H4/Df83o1E= X-Endpoint-Received: by B4 Relay for nuno.sa@analog.com/20231116 with auth_id=100 X-Original-From: Nuno Sa Reply-To: nuno.sa@analog.com From: Nuno Sa We can only access the IP core registers if the bus clock is enabled. As such we need to get and enable it and not rely on anyone else to do it. Signed-off-by: Nuno Sa --- drivers/iio/adc/adi-axi-adc.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/iio/adc/adi-axi-adc.c b/drivers/iio/adc/adi-axi-adc.c index a543b91124b07..e3b2158829416 100644 --- a/drivers/iio/adc/adi-axi-adc.c +++ b/drivers/iio/adc/adi-axi-adc.c @@ -175,6 +175,7 @@ static int adi_axi_adc_probe(struct platform_device *pdev) struct adi_axi_adc_state *st; void __iomem *base; unsigned int ver; + struct clk *clk; int ret; st = devm_kzalloc(&pdev->dev, sizeof(*st), GFP_KERNEL); @@ -195,6 +196,10 @@ static int adi_axi_adc_probe(struct platform_device *pdev) if (!expected_ver) return -ENODEV; + clk = devm_clk_get_enabled(&pdev->dev, NULL); + if (IS_ERR(clk)) + return PTR_ERR(clk); + /* * Force disable the core. Up to the frontend to enable us. And we can * still read/write registers... From patchwork Fri Apr 19 15:36:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nuno Sa via B4 Relay X-Patchwork-Id: 13636523 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 961FD133994; Fri, 19 Apr 2024 15:36:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713541012; cv=none; b=ilOzkBAqUxuy0yUBo8HtEODBO7UbQiGNE3S28tdaBYifQO74ZaEXWb8cgSVbCW6sYnYSCT7Vfrff05XxdG4TWnFQ6N+G5vu47sbFpCkcu0Een99ns8XoTv0zwywQ5qGU6q9TKZtn0Um35jEXzLMSePlebuIpEjkijPaKJilT45A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713541012; c=relaxed/simple; bh=d2ddOxG6ia3/viS09wCXHV2UWCg2yg2SY7mLAsDE4xg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=KpcKGjkPWNrgiMWXhgYwI//41go96sl2S5kBKPDRemmrfSS/xckbawWIQzsm1845LeI3swetwQERlA5xd5OOBdR3Z4gr0ZjV8fox+/2NnaDCxQqMBfjxu3Xb/fvUeC3jGHZDNLUHiPTkEb5Ug0skDqvazMBof1+A/wAjFd5ohIQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=h5l3tY2I; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="h5l3tY2I" Received: by smtp.kernel.org (Postfix) with ESMTPS id 3EC22C4AF12; Fri, 19 Apr 2024 15:36:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713541012; bh=d2ddOxG6ia3/viS09wCXHV2UWCg2yg2SY7mLAsDE4xg=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=h5l3tY2IUgBx+pVG2P6worEIiQkMKvUIOaxEVpyOrWbXO5p++CPfBerDsj73gG0zX gGrif7lPpL70TLsIrSCkDLxZbhX1bzuL7xM38kas1Pej7h8Y3hHGiUTlzn+axnH1Uf b4dBNuqATHtJJ9/rahNPsSPLIbyjrRTNfDIn0T/aeN5kFNk4zlESeQQqWur+5sF3xB GO6cFxqdES95V87aMXQ8bJEXLxHCsNmyAOvlJiyML47W9dPdS60dWjQTD17kIX6BQg rxdJHYqNy5E3CnlcqStQ+YXudjoutiCvsYQz2+XDXwgTIyCgvE/7egcyfaf80UxWKN LwVuC3x7l1tVQ== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 33A6AC071DB; Fri, 19 Apr 2024 15:36:52 +0000 (UTC) From: Nuno Sa via B4 Relay Date: Fri, 19 Apr 2024 17:36:48 +0200 Subject: [PATCH 5/8] iio: adc: adi-axi-adc: remove regmap max register Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240419-ad9467-new-features-v1-5-3e7628ff6d5e@analog.com> References: <20240419-ad9467-new-features-v1-0-3e7628ff6d5e@analog.com> In-Reply-To: <20240419-ad9467-new-features-v1-0-3e7628ff6d5e@analog.com> To: linux-iio@vger.kernel.org, devicetree@vger.kernel.org Cc: Jonathan Cameron , Alexandru Ardelean , Lars-Peter Clausen , Michael Hennerich , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Olivier Moysan , Nuno Sa X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1713541010; l=815; i=nuno.sa@analog.com; s=20231116; h=from:subject:message-id; bh=yuttbukX0NN4jOLmY+xhQCCzS7vMjNuAM9G3y7vBkBQ=; b=y0o0eeLrYzGr245OURF3dJWvUh47qOXbJariJnO8XLiBAsBFls5dys91Zk9aFUSrNH4bMvYft EDhQDnfe4qvB602a01WCf7iWDyVf4jisiLbtAXr/wkjitG91qMSe48X X-Developer-Key: i=nuno.sa@analog.com; a=ed25519; pk=3NQwYA013OUYZsmDFBf8rmyyr5iQlxV/9H4/Df83o1E= X-Endpoint-Received: by B4 Relay for nuno.sa@analog.com/20231116 with auth_id=100 X-Original-From: Nuno Sa Reply-To: nuno.sa@analog.com From: Nuno Sa In one of the following patches, we'll have some new functionality that requires reads/writes on registers bigger than 0x8000. Hence, as this is an highly flexible core, don't bother in setting 'max_register' and remove it from regmap_config. Signed-off-by: Nuno Sa --- drivers/iio/adc/adi-axi-adc.c | 1 - 1 file changed, 1 deletion(-) diff --git a/drivers/iio/adc/adi-axi-adc.c b/drivers/iio/adc/adi-axi-adc.c index e3b2158829416..b312369b73661 100644 --- a/drivers/iio/adc/adi-axi-adc.c +++ b/drivers/iio/adc/adi-axi-adc.c @@ -156,7 +156,6 @@ static const struct regmap_config axi_adc_regmap_config = { .val_bits = 32, .reg_bits = 32, .reg_stride = 4, - .max_register = 0x0800, }; static const struct iio_backend_ops adi_axi_adc_generic = { From patchwork Fri Apr 19 15:36:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nuno Sa via B4 Relay X-Patchwork-Id: 13636524 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 772A4133286; Fri, 19 Apr 2024 15:36:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713541012; cv=none; b=uwTR4kk8NY2EHioDT6CS3TCj5etZcmLsFC01+HBqaQb3IX3xbO2wyNgUbjkl2+7g7sZYiYU8eBzVOBSg5ZA6vnHgyT9qzzDhFFXPYWnb90aevczbvbkc/Pldcyd8PrPgr+zWBv4rNF8XWShuIoTBgSQ8FCZXn6pY2enu82mnzI8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713541012; c=relaxed/simple; bh=7mLN6O+gkStwhWw7Spx2RV+GkT06Kb3PBj7KvqQghdo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=nnZxtAmDQDt8AUjzyR8WupBbhfH/INRWZss466+Ly+w8r5M3ZvbOcqlsbg5CZFKEPOgdO7NQbaduQa1WgJwu6omVBQpdgeqjJ1qDAMyupPjqmgQbi96jqH0z00ioNXR3gh7lxsl+9ZkwCyhq7ImQH3EUblFIjEB0KK1XoevHUug= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=c13XsSQt; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="c13XsSQt" Received: by smtp.kernel.org (Postfix) with ESMTPS id 535AEC4AF10; Fri, 19 Apr 2024 15:36:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713541012; bh=7mLN6O+gkStwhWw7Spx2RV+GkT06Kb3PBj7KvqQghdo=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=c13XsSQtbu8+gtpeqnkCD7Aw7J+VewjFG6dDJuKgB7BdxSdKXLeBeBcRsQ6JgZXa7 565hEm9U1JMpYOwSGKYZXESI3GhflyN31rxcrrID30J3fWAUa/oVVZk2mB/b6H0s91 HXkDZUm4/Fz9B90t8CxnZQJF5VsSnkkq6A+Uzr3R9YXXGuwU+4HFBQLgp7w93czxk/ eTD9c6FH/8w+3pm+9irDuJPJoLyM4ZdLatjsIOY/ktNcIM9zuPipgEysI17CZS29xX 1/C0XN9TrNvyK5b9C3+BFFXa46DIUs9/ZeqDB7ecIxu7YNb4R30HqYXEw1kuHWLRHw jZl/ZSwPOXDzg== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 46E35C4345F; Fri, 19 Apr 2024 15:36:52 +0000 (UTC) From: Nuno Sa via B4 Relay Date: Fri, 19 Apr 2024 17:36:49 +0200 Subject: [PATCH 6/8] iio: adc: adi-axi-adc: support digital interface calibration Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240419-ad9467-new-features-v1-6-3e7628ff6d5e@analog.com> References: <20240419-ad9467-new-features-v1-0-3e7628ff6d5e@analog.com> In-Reply-To: <20240419-ad9467-new-features-v1-0-3e7628ff6d5e@analog.com> To: linux-iio@vger.kernel.org, devicetree@vger.kernel.org Cc: Jonathan Cameron , Alexandru Ardelean , Lars-Peter Clausen , Michael Hennerich , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Olivier Moysan , Nuno Sa X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1713541010; l=5353; i=nuno.sa@analog.com; s=20231116; h=from:subject:message-id; bh=pTk3suYEFqSEy8qWfkkQymJKnE79QKBvOXkcNN9lEGU=; b=MpVkNgn2WLrIqBi6yTTxLhZDmBNWXmNmDBRw3sE7mb9JJn23+FAnDtLi8qAe+ZviYZO5pxvdl 5GqV+n6LWmwDLMfC/RIBgIsJHrbX2BRtpDbBpps+GTDgVlvhgMx3pmL X-Developer-Key: i=nuno.sa@analog.com; a=ed25519; pk=3NQwYA013OUYZsmDFBf8rmyyr5iQlxV/9H4/Df83o1E= X-Endpoint-Received: by B4 Relay for nuno.sa@analog.com/20231116 with auth_id=100 X-Original-From: Nuno Sa Reply-To: nuno.sa@analog.com From: Nuno Sa Implement the new IIO backend APIs for calibrating the data digital interfaces. While at it, removed the tabs in 'struct adi_axi_adc_state' and used spaces for the members. Signed-off-by: Nuno Sa --- drivers/iio/adc/adi-axi-adc.c | 113 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 111 insertions(+), 2 deletions(-) diff --git a/drivers/iio/adc/adi-axi-adc.c b/drivers/iio/adc/adi-axi-adc.c index b312369b7366..d58fa05499c4 100644 --- a/drivers/iio/adc/adi-axi-adc.c +++ b/drivers/iio/adc/adi-axi-adc.c @@ -7,11 +7,13 @@ */ #include +#include #include #include #include #include #include +#include #include #include #include @@ -37,6 +39,9 @@ #define ADI_AXI_REG_RSTN_MMCM_RSTN BIT(1) #define ADI_AXI_REG_RSTN_RSTN BIT(0) +#define ADI_AXI_ADC_REG_CTRL 0x0044 +#define ADI_AXI_ADC_CTRL_DDR_EDGESEL_MASK BIT(1) + /* ADC Channel controls */ #define ADI_AXI_REG_CHAN_CTRL(c) (0x0400 + (c) * 0x40) @@ -51,14 +56,28 @@ #define ADI_AXI_REG_CHAN_CTRL_PN_TYPE_OWR BIT(1) #define ADI_AXI_REG_CHAN_CTRL_ENABLE BIT(0) +#define ADI_AXI_ADC_REG_CHAN_STATUS(c) (0x0404 + (c) * 0x40) +#define ADI_AXI_ADC_CHAN_STAT_PN_MASK GENMASK(2, 1) + +#define ADI_AXI_ADC_REG_CHAN_CTRL_3(c) (0x0418 + (c) * 0x40) +#define ADI_AXI_ADC_CHAN_PN_SEL_MASK GENMASK(19, 16) + +/* IO Delays */ +#define ADI_AXI_ADC_REG_DELAY(l) (0x0800 + (l) * 0x4) +#define AXI_ADC_DELAY_CTRL_MASK GENMASK(4, 0) + +#define ADI_AXI_ADC_MAX_IO_NUM_LANES 15 + #define ADI_AXI_REG_CHAN_CTRL_DEFAULTS \ (ADI_AXI_REG_CHAN_CTRL_FMT_SIGNEXT | \ ADI_AXI_REG_CHAN_CTRL_FMT_EN | \ ADI_AXI_REG_CHAN_CTRL_ENABLE) struct adi_axi_adc_state { - struct regmap *regmap; - struct device *dev; + struct regmap *regmap; + struct device *dev; + /* lock to protect multiple accesses to the device registers */ + struct mutex lock; }; static int axi_adc_enable(struct iio_backend *back) @@ -104,6 +123,92 @@ static int axi_adc_data_format_set(struct iio_backend *back, unsigned int chan, ADI_AXI_REG_CHAN_CTRL_FMT_MASK, val); } +static int axi_adc_data_sample_trigger(struct iio_backend *back, + enum iio_backend_sample_trigger trigger) +{ + struct adi_axi_adc_state *st = iio_backend_get_priv(back); + + if (trigger == IIO_BACKEND_SAMPLE_TRIGGER_EDGE_RISING) + return regmap_clear_bits(st->regmap, ADI_AXI_ADC_REG_CTRL, + ADI_AXI_ADC_CTRL_DDR_EDGESEL_MASK); + if (trigger == IIO_BACKEND_SAMPLE_TRIGGER_EDGE_FALLING) + return regmap_set_bits(st->regmap, ADI_AXI_ADC_REG_CTRL, + ADI_AXI_ADC_CTRL_DDR_EDGESEL_MASK); + + return -EINVAL; +} + +static int axi_adc_iodelays_set(struct iio_backend *back, unsigned int lane, + unsigned int tap) +{ + struct adi_axi_adc_state *st = iio_backend_get_priv(back); + int ret; + u32 val; + + if (tap > FIELD_MAX(AXI_ADC_DELAY_CTRL_MASK)) + return -EINVAL; + if (lane > ADI_AXI_ADC_MAX_IO_NUM_LANES) + return -EINVAL; + + guard(mutex)(&st->lock); + ret = regmap_write(st->regmap, ADI_AXI_ADC_REG_DELAY(lane), tap); + if (ret) + return ret; + /* + * If readback is ~0, that means there are issues with the + * delay_clk. + */ + ret = regmap_read(st->regmap, ADI_AXI_ADC_REG_DELAY(lane), &val); + if (ret) + return ret; + if (val == U32_MAX) + return -EIO; + + return 0; +} + +static int axi_adc_test_pattern_set(struct iio_backend *back, + unsigned int chan, + enum iio_backend_test_pattern pattern) +{ + struct adi_axi_adc_state *st = iio_backend_get_priv(back); + + switch (pattern) { + case IIO_BACKEND_ADI_PRBS_9A: + return regmap_update_bits(st->regmap, ADI_AXI_ADC_REG_CHAN_CTRL_3(chan), + ADI_AXI_ADC_CHAN_PN_SEL_MASK, + FIELD_PREP(ADI_AXI_ADC_CHAN_PN_SEL_MASK, 0)); + default: + return -EINVAL; + } +} + +static int axi_adc_chan_status(struct iio_backend *back, unsigned int chan, + struct iio_backend_chan_status *status) +{ + struct adi_axi_adc_state *st = iio_backend_get_priv(back); + int ret; + u32 val; + + guard(mutex)(&st->lock); + /* reset test bits by setting them */ + ret = regmap_write(st->regmap, ADI_AXI_ADC_REG_CHAN_STATUS(chan), + ADI_AXI_ADC_CHAN_STAT_PN_MASK); + if (ret) + return ret; + + fsleep(1000); + + ret = regmap_read(st->regmap, ADI_AXI_ADC_REG_CHAN_STATUS(chan), &val); + if (ret) + return ret; + + if (ADI_AXI_ADC_CHAN_STAT_PN_MASK & val) + status->errors = true; + + return 0; +} + static int axi_adc_chan_enable(struct iio_backend *back, unsigned int chan) { struct adi_axi_adc_state *st = iio_backend_get_priv(back); @@ -166,6 +271,10 @@ static const struct iio_backend_ops adi_axi_adc_generic = { .chan_disable = axi_adc_chan_disable, .request_buffer = axi_adc_request_buffer, .free_buffer = axi_adc_free_buffer, + .data_sample_trigger = axi_adc_data_sample_trigger, + .iodelay_set = axi_adc_iodelays_set, + .test_pattern_set = axi_adc_test_pattern_set, + .chan_status = axi_adc_chan_status, }; static int adi_axi_adc_probe(struct platform_device *pdev) From patchwork Fri Apr 19 15:36:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nuno Sa via B4 Relay X-Patchwork-Id: 13636526 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AA997134CF2; Fri, 19 Apr 2024 15:36:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713541012; cv=none; b=i6FgjH5BzKQWfrtYKGrkb27+xnQsGC9jAURRN9OVQU4w44AWwMGNBhcmxym/I8JuCMExnK73WH1HAM4ShJmi5Lmgdo+mzNPEYocDYUU2Sznv30A90OBGtcV61IOqP8n1MQ/BYnPRj6CtCopuXLFCo7ZTwf5JpO7kgtrAV7mkxGo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713541012; c=relaxed/simple; bh=dKV+SJmNkbkvGbVwkoyrsBMcN+5DjDPpWTYe8E+QALc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=BkLxDyKzPr32M8sAmo/sLKeVHRi6r1nmlkqmTa+E4bWeSV0VZmeZznghQXVEG15Rk45gHLA/yi9ErBVlDJBiZ5170Ptg5w3PimhotpItn4b72OY0fRDQaQmkTMldj8+sdlfoW3KIx3bDG0D2VXgFkVaCh+xo5aPLOGfzIWpGrrw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=b1xQPa/T; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="b1xQPa/T" Received: by smtp.kernel.org (Postfix) with ESMTPS id 67243C4AF17; Fri, 19 Apr 2024 15:36:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713541012; bh=dKV+SJmNkbkvGbVwkoyrsBMcN+5DjDPpWTYe8E+QALc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=b1xQPa/TGyFQn+w6bpYbEHBEN8FyNc49y8ZSVcKZ8STCMlHv/IPhkX9ps3zg8Mw15 eO2zG+f+n9UywE1ZFG6pB9z/FQCCH9cBRM+3LuJ2Ao8s9FMuTY+6KpCW9tykC/xcIm +/uOX5Q4wo41LMdUTCgiJmSJZqsBWtezpU0HReSgB9xUv4l5Yb7ztXOPduCqR9NN2/ eq/e9YUcsPxCZtUTDk/vpQTYBWFufOTGmGHrI5c6a+XjTKcwR4DAY2tAiU/qV4ubt8 2A7d5P5e42AAxeevBOk39wWwXUICcs0uREZe14AK3oX8l++R3h254vEt7FtqlKMMOh faeabr3oJWRAw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5D783C04FF6; Fri, 19 Apr 2024 15:36:52 +0000 (UTC) From: Nuno Sa via B4 Relay Date: Fri, 19 Apr 2024 17:36:50 +0200 Subject: [PATCH 7/8] iio: adc: ad9467: cache the sample rate Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240419-ad9467-new-features-v1-7-3e7628ff6d5e@analog.com> References: <20240419-ad9467-new-features-v1-0-3e7628ff6d5e@analog.com> In-Reply-To: <20240419-ad9467-new-features-v1-0-3e7628ff6d5e@analog.com> To: linux-iio@vger.kernel.org, devicetree@vger.kernel.org Cc: Jonathan Cameron , Alexandru Ardelean , Lars-Peter Clausen , Michael Hennerich , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Olivier Moysan , Nuno Sa X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1713541010; l=1935; i=nuno.sa@analog.com; s=20231116; h=from:subject:message-id; bh=AVSjsumjo5TvKypskYPgN6fw3oHyN2j1uJ4FVRsJVt0=; b=M4HAt4TIvHefbkd2brW+3zFwv2Kv4zsyeem0qB7NE9I8pHZpgGeL+OOsjoUSvjFppixAuGByn qO9kYTydOw3DcqbxGs+JYFXrNh44kRoNQYC16xDcrQbD6IiiAVOtKnr X-Developer-Key: i=nuno.sa@analog.com; a=ed25519; pk=3NQwYA013OUYZsmDFBf8rmyyr5iQlxV/9H4/Df83o1E= X-Endpoint-Received: by B4 Relay for nuno.sa@analog.com/20231116 with auth_id=100 X-Original-From: Nuno Sa Reply-To: nuno.sa@analog.com From: Nuno Sa Since we allow to change the sampling frequency and do it with clk_round_rate(), we can cache it and use on the read_raw() interface. This will also be useful in a following patch supporting interface calibration. Signed-off-by: Nuno Sa --- drivers/iio/adc/ad9467.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/drivers/iio/adc/ad9467.c b/drivers/iio/adc/ad9467.c index 7475ec2a56c72..7db87ccc1ea4b 100644 --- a/drivers/iio/adc/ad9467.c +++ b/drivers/iio/adc/ad9467.c @@ -117,6 +117,7 @@ struct ad9467_state { struct iio_backend *back; struct spi_device *spi; struct clk *clk; + unsigned long sample_rate; unsigned int output_mode; unsigned int (*scales)[2]; @@ -331,7 +332,7 @@ static int ad9467_read_raw(struct iio_dev *indio_dev, case IIO_CHAN_INFO_SCALE: return ad9467_get_scale(st, val, val2); case IIO_CHAN_INFO_SAMP_FREQ: - *val = clk_get_rate(st->clk); + *val = st->sample_rate; return IIO_VAL_INT; default: @@ -346,6 +347,7 @@ static int ad9467_write_raw(struct iio_dev *indio_dev, struct ad9467_state *st = iio_priv(indio_dev); const struct ad9467_chip_info *info = st->info; long r_clk; + int ret; switch (mask) { case IIO_CHAN_INFO_SCALE: @@ -358,7 +360,12 @@ static int ad9467_write_raw(struct iio_dev *indio_dev, return -EINVAL; } - return clk_set_rate(st->clk, r_clk); + ret = clk_set_rate(st->clk, r_clk); + if (ret) + return ret; + + st->sample_rate = r_clk; + return 0; default: return -EINVAL; } @@ -543,6 +550,8 @@ static int ad9467_probe(struct spi_device *spi) if (IS_ERR(st->clk)) return PTR_ERR(st->clk); + st->sample_rate = clk_get_rate(st->clk); + st->pwrdown_gpio = devm_gpiod_get_optional(&spi->dev, "powerdown", GPIOD_OUT_LOW); if (IS_ERR(st->pwrdown_gpio)) From patchwork Fri Apr 19 15:36:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nuno Sa via B4 Relay X-Patchwork-Id: 13636525 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9CF5A134416; Fri, 19 Apr 2024 15:36:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713541012; cv=none; b=qHrd11TzzV36rFwqjTU+X59dowAEBeFJIHHKCmOAan/v0YSGGydBuD26h9rp/O6rrv6LWfnxnV5KAqI2UacThr9DVvtcf3vwIz5x3kWjIQ1TIlEdM72XOBCfVPdCZWzw+VuY7QJNhQgr+/9rKcLl3XDabR7u2ZW7y+IW0Dix7uA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713541012; c=relaxed/simple; bh=+DGHBdnrTYRitHjsY9XAZPyW9GtYKlA/s3wxV5NPYoA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=W8j81m6iizLh51jfFTPk2huriPtZsCfks9ojCfuY5W/LnU3inxuo09ax3Bd4z7u2SeV1zxwBrA9O0SUBrDKOb1mUkqGmGygtwtF9No7PXUnYuwUW/IoEQJGHVa27C1aXDBZ3YKfa4aFMO4G8lMq1kJ+1vcKHpfgDavIOREmRYZc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=pTFmBzYT; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="pTFmBzYT" Received: by smtp.kernel.org (Postfix) with ESMTPS id 7EA68C2BD10; Fri, 19 Apr 2024 15:36:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713541012; bh=+DGHBdnrTYRitHjsY9XAZPyW9GtYKlA/s3wxV5NPYoA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=pTFmBzYTOiHB3krBGT2/jQxXh5Fa+34mBEQRjv4IQmBR/spD514a2XrPcC502smuJ FYyXQh4WcrRsW/uPwepBq/v6ebu3LTbs7OXf3usHJDV2qcE9ddcdW4xXLi67Zx85Xj 65WsjPDVF6uyKbnK+h/2sobP6LyhYp2Bq5qyWHQjtJF8bjQuTJCnxqIAqN0W6uDd5g ysCFGa8QbQSUfZqn86GcMOBKNlf2VdClJtT4HLd5TeiJHR+1VpgrDfYDxeZkmIssA3 5fAHMuHGtVJ11r/mYknu8KuyfDy8BkvqhGGAwXMgXZom5oiULRgDFjBKNmLRsuXUek MTGc86jVEtcQA== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 758DAC4345F; Fri, 19 Apr 2024 15:36:52 +0000 (UTC) From: Nuno Sa via B4 Relay Date: Fri, 19 Apr 2024 17:36:51 +0200 Subject: [PATCH 8/8] iio: adc: ad9467: support digital interface calibration Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240419-ad9467-new-features-v1-8-3e7628ff6d5e@analog.com> References: <20240419-ad9467-new-features-v1-0-3e7628ff6d5e@analog.com> In-Reply-To: <20240419-ad9467-new-features-v1-0-3e7628ff6d5e@analog.com> To: linux-iio@vger.kernel.org, devicetree@vger.kernel.org Cc: Jonathan Cameron , Alexandru Ardelean , Lars-Peter Clausen , Michael Hennerich , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Olivier Moysan , Nuno Sa X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1713541010; l=11433; i=nuno.sa@analog.com; s=20231116; h=from:subject:message-id; bh=u8Tbte3YlvHi71mA8qJNTe770otJEy1OCeXGxPuLYyg=; b=brBhJ2v2ZGqz3S/MqlvurO/FklUWTZlyFe1cfBPLd1IJv4TxJCLGHTKAxSWOvF/X7zWEvEij3 QAQ8H+/R+dRCMO4y8ugyU0a/Q+Xj0ikIILYmaxL5lWRQybbF9ncul4K X-Developer-Key: i=nuno.sa@analog.com; a=ed25519; pk=3NQwYA013OUYZsmDFBf8rmyyr5iQlxV/9H4/Df83o1E= X-Endpoint-Received: by B4 Relay for nuno.sa@analog.com/20231116 with auth_id=100 X-Original-From: Nuno Sa Reply-To: nuno.sa@analog.com From: Nuno Sa To make sure that we have the best timings on the serial data interface we should calibrate it. This means going through the device supported values and see for which ones we get a successful result. To do that, we use a prbs test pattern both in the IIO backend and in the frontend devices. Then for each of the test points we see if there are any errors. Note that the backend is responsible to look for those errors. As calibrating the interface also requires that the data format is disabled (the one thing being done in ad9467_setup()), ad9467_setup() was removed and configuring the data fomat is now part of the calibration process. Signed-off-by: Nuno Sa --- drivers/iio/adc/ad9467.c | 337 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 296 insertions(+), 41 deletions(-) diff --git a/drivers/iio/adc/ad9467.c b/drivers/iio/adc/ad9467.c index 7db87ccc1ea4..44552dd6f4c6 100644 --- a/drivers/iio/adc/ad9467.c +++ b/drivers/iio/adc/ad9467.c @@ -4,6 +4,9 @@ * * Copyright 2012-2020 Analog Devices Inc. */ + +#include +#include #include #include #include @@ -100,6 +103,8 @@ #define AD9467_DEF_OUTPUT_MODE 0x08 #define AD9467_REG_VREF_MASK 0x0F +#define AD9647_MAX_TEST_POINTS 32 + struct ad9467_chip_info { const char *name; unsigned int id; @@ -110,6 +115,8 @@ struct ad9467_chip_info { unsigned long max_rate; unsigned int default_output_mode; unsigned int vref_mask; + unsigned int num_lanes; + bool has_dco; }; struct ad9467_state { @@ -243,6 +250,7 @@ static const struct ad9467_chip_info ad9467_chip_tbl = { .num_channels = ARRAY_SIZE(ad9467_channels), .default_output_mode = AD9467_DEF_OUTPUT_MODE, .vref_mask = AD9467_REG_VREF_MASK, + .num_lanes = 8, }; static const struct ad9467_chip_info ad9434_chip_tbl = { @@ -255,6 +263,7 @@ static const struct ad9467_chip_info ad9434_chip_tbl = { .num_channels = ARRAY_SIZE(ad9434_channels), .default_output_mode = AD9434_DEF_OUTPUT_MODE, .vref_mask = AD9434_REG_VREF_MASK, + .num_lanes = 6, }; static const struct ad9467_chip_info ad9265_chip_tbl = { @@ -267,6 +276,7 @@ static const struct ad9467_chip_info ad9265_chip_tbl = { .num_channels = ARRAY_SIZE(ad9467_channels), .default_output_mode = AD9265_DEF_OUTPUT_MODE, .vref_mask = AD9265_REG_VREF_MASK, + .has_dco = true, }; static int ad9467_get_scale(struct ad9467_state *st, int *val, int *val2) @@ -322,6 +332,276 @@ static int ad9467_set_scale(struct ad9467_state *st, int val, int val2) return -EINVAL; } +static void ad9467_dump_table(const unsigned long *err_map, unsigned int size, + bool invert) +{ +#ifdef DEBUG + unsigned int bit; + + pr_debug("Dump calibration table:\n"); + for (bit = 0; bit < size; bit++) { + if (bit == size / 2) { + if (!invert) + break; + pr_cont("\n"); + } + + pr_cont("%c", test_bit(bit, err_map) ? 'x' : 'o'); + } +#endif +} + +static int ad9467_outputmode_set(struct spi_device *spi, unsigned int mode) +{ + int ret; + + ret = ad9467_spi_write(spi, AN877_ADC_REG_OUTPUT_MODE, mode); + if (ret < 0) + return ret; + + return ad9467_spi_write(spi, AN877_ADC_REG_TRANSFER, + AN877_ADC_TRANSFER_SYNC); +} + +static int ad9647_calibrate_prepare(const struct ad9467_state *st) +{ + struct iio_backend_data_fmt data = { + .enable = false, + }; + unsigned int c; + int ret; + + ret = ad9467_spi_write(st->spi, AN877_ADC_REG_TEST_IO, + AN877_ADC_TESTMODE_PN9_SEQ); + if (ret) + return ret; + + ret = ad9467_spi_write(st->spi, AN877_ADC_REG_TRANSFER, + AN877_ADC_TRANSFER_SYNC); + if (ret) + return ret; + + ret = ad9467_outputmode_set(st->spi, st->info->default_output_mode); + if (ret) + return ret; + + for (c = 0; c < st->info->num_channels; c++) { + ret = iio_backend_data_format_set(st->back, c, &data); + if (ret) + return ret; + } + + ret = iio_backend_test_pattern_set(st->back, 0, + IIO_BACKEND_ADI_PRBS_9A); + if (ret) + return ret; + + return iio_backend_chan_enable(st->back, 0); +} + +static int ad9647_calibrate_polarity_set(const struct ad9467_state *st, + bool invert) +{ + enum iio_backend_sample_trigger trigger; + + if (st->info->has_dco) { + unsigned int phase = AN877_ADC_OUTPUT_EVEN_ODD_MODE_EN; + + if (invert) + phase |= AN877_ADC_INVERT_DCO_CLK; + + return ad9467_spi_write(st->spi, AN877_ADC_REG_OUTPUT_PHASE, + phase); + } + + if (invert) + trigger = IIO_BACKEND_SAMPLE_TRIGGER_EDGE_FALLING; + else + trigger = IIO_BACKEND_SAMPLE_TRIGGER_EDGE_RISING; + + return iio_backend_data_sample_trigger(st->back, trigger); +} + +static int ad9467_calibrate_status_check(const struct ad9467_state *st, + bool *stat) +{ + struct iio_backend_chan_status status = {0}; + int ret; + + ret = iio_backend_chan_status(st->back, 0, &status); + if (ret) + return ret; + + *stat = status.errors; + + return 0; +} + +/* + * The idea is pretty simple. Find the max number of successful points in a row + * and get the one in the middle. + */ +static unsigned int ad9467_find_optimal_point(const unsigned long *err_map, + unsigned int start, + unsigned int nbits, + unsigned int *val) +{ + unsigned int bit = start, end, start_cnt, cnt = 0; + + for_each_clear_bitrange_from(bit, end, err_map, nbits + start) { + if (end - bit > cnt) { + cnt = end - bit; + start_cnt = bit; + } + } + + if (cnt) + *val = start_cnt + cnt / 2; + + return cnt; +} + +static int ad9467_calibrate_apply(const struct ad9467_state *st, + unsigned int val) +{ + unsigned int lane; + int ret; + + if (st->info->has_dco) { + int ret; + + ret = ad9467_spi_write(st->spi, AN877_ADC_REG_OUTPUT_DELAY, + val); + if (ret) + return ret; + + return ad9467_spi_write(st->spi, AN877_ADC_REG_TRANSFER, + AN877_ADC_TRANSFER_SYNC); + } + + for (lane = 0; lane < st->info->num_lanes; lane++) { + ret = iio_backend_iodelay_set(st->back, lane, val); + if (ret) + return ret; + } + + return 0; +} + +static int ad9647_calibrate_stop(const struct ad9467_state *st) +{ + struct iio_backend_data_fmt data = { + .sign_extend = true, + .enable = true, + }; + unsigned int c, mode; + int ret; + + ret = iio_backend_chan_disable(st->back, 0); + if (ret) + return ret; + + for (c = 0; c < st->info->num_channels; c++) { + ret = iio_backend_data_format_set(st->back, c, &data); + if (ret) + return ret; + } + + mode = st->info->default_output_mode | AN877_ADC_OUTPUT_MODE_TWOS_COMPLEMENT; + ret = ad9467_outputmode_set(st->spi, mode); + if (ret) + return ret; + + ret = ad9467_spi_write(st->spi, AN877_ADC_REG_TEST_IO, + AN877_ADC_TESTMODE_OFF); + if (ret) + return ret; + + return ad9467_spi_write(st->spi, AN877_ADC_REG_TRANSFER, + AN877_ADC_TRANSFER_SYNC); +} + +static int ad9467_calibrate(const struct ad9467_state *st) +{ + DECLARE_BITMAP(err_map, AD9647_MAX_TEST_POINTS * 2); + unsigned int point, val, inv_val, cnt, inv_cnt = 0; + /* + * Half of the bitmap is for the inverted signal. The number of test + * points is the same though... + */ + unsigned int test_points = AD9647_MAX_TEST_POINTS; + unsigned long sample_rate = clk_get_rate(st->clk); + struct device *dev = &st->spi->dev; + bool invert = false, stat; + int ret; + + ret = ad9647_calibrate_prepare(st); + if (ret) + return ret; +retune: + ret = ad9647_calibrate_polarity_set(st, invert); + if (ret) + return ret; + + for (point = 0; point < test_points; point++) { + ret = ad9467_calibrate_apply(st, point); + if (ret) + return ret; + + ret = ad9467_calibrate_status_check(st, &stat); + if (ret < 0) + return ret; + + __assign_bit(point + invert * test_points, err_map, stat); + } + + if (!invert) { + cnt = ad9467_find_optimal_point(err_map, 0, test_points, &val); + /* + * We're happy if we find, at least, three good test points in + * a row. + */ + if (cnt < 3) { + invert = true; + goto retune; + } + } else { + inv_cnt = ad9467_find_optimal_point(err_map, test_points, + test_points, &inv_val); + if (!inv_cnt && !cnt) + return -EIO; + } + + ad9467_dump_table(err_map, BITS_PER_TYPE(err_map), invert); + + if (inv_cnt < cnt) { + ret = ad9647_calibrate_polarity_set(st, false); + if (ret) + return ret; + } else { + /* + * polarity inverted is the last test to run. Hence, there's no + * need to re-do any configuration. We just need to "normalize" + * the selected value. + */ + val = inv_val - test_points; + } + + if (st->info->has_dco) + dev_dbg(dev, "%sDCO 0x%X CLK %lu Hz\n", inv_cnt >= cnt ? "INVERT " : "", + val, sample_rate); + else + dev_dbg(dev, "%sIDELAY 0x%x\n", inv_cnt >= cnt ? "INVERT " : "", + val); + + ret = ad9467_calibrate_apply(st, val); + if (ret) + return ret; + + /* finally apply the optimal value */ + return ad9647_calibrate_stop(st); +} + static int ad9467_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int *val, int *val2, long m) @@ -360,12 +640,22 @@ static int ad9467_write_raw(struct iio_dev *indio_dev, return -EINVAL; } - ret = clk_set_rate(st->clk, r_clk); - if (ret) - return ret; + /* + * clk_set_rate() would also do this but since we would still + * need it for avoiding an unnecessary calibration, do it now. + */ + if (st->sample_rate == r_clk) + return 0; - st->sample_rate = r_clk; - return 0; + iio_device_claim_direct_scoped(return -EBUSY, indio_dev) { + ret = clk_set_rate(st->clk, r_clk); + if (ret) + return ret; + + st->sample_rate = r_clk; + ret = ad9467_calibrate(st); + } + return ret; default: return -EINVAL; } @@ -418,18 +708,6 @@ static const struct iio_info ad9467_info = { .read_avail = ad9467_read_avail, }; -static int ad9467_outputmode_set(struct spi_device *spi, unsigned int mode) -{ - int ret; - - ret = ad9467_spi_write(spi, AN877_ADC_REG_OUTPUT_MODE, mode); - if (ret < 0) - return ret; - - return ad9467_spi_write(spi, AN877_ADC_REG_TRANSFER, - AN877_ADC_TRANSFER_SYNC); -} - static int ad9467_scale_fill(struct ad9467_state *st) { const struct ad9467_chip_info *info = st->info; @@ -449,29 +727,6 @@ static int ad9467_scale_fill(struct ad9467_state *st) return 0; } -static int ad9467_setup(struct ad9467_state *st) -{ - struct iio_backend_data_fmt data = { - .sign_extend = true, - .enable = true, - }; - unsigned int c, mode; - int ret; - - mode = st->info->default_output_mode | AN877_ADC_OUTPUT_MODE_TWOS_COMPLEMENT; - ret = ad9467_outputmode_set(st->spi, mode); - if (ret) - return ret; - - for (c = 0; c < st->info->num_channels; c++) { - ret = iio_backend_data_format_set(st->back, c, &data); - if (ret) - return ret; - } - - return 0; -} - static int ad9467_reset(struct device *dev) { struct gpio_desc *gpio; @@ -589,7 +844,7 @@ static int ad9467_probe(struct spi_device *spi) if (ret) return ret; - ret = ad9467_setup(st); + ret = ad9467_calibrate(st); if (ret) return ret;