From patchwork Thu Sep 26 10:51:30 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Ardelean X-Patchwork-Id: 11162453 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 BAD5713B1 for ; Thu, 26 Sep 2019 10:54:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 86780222C2 for ; Thu, 26 Sep 2019 10:54:06 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=analog.onmicrosoft.com header.i=@analog.onmicrosoft.com header.b="fktm5wD+" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726950AbfIZKyB (ORCPT ); Thu, 26 Sep 2019 06:54:01 -0400 Received: from mx0b-00128a01.pphosted.com ([148.163.139.77]:3998 "EHLO mx0b-00128a01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726359AbfIZKw1 (ORCPT ); Thu, 26 Sep 2019 06:52:27 -0400 Received: from pps.filterd (m0167090.ppops.net [127.0.0.1]) by mx0b-00128a01.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id x8QAqK8u018025; Thu, 26 Sep 2019 06:52:20 -0400 Received: from nam05-dm3-obe.outbound.protection.outlook.com (mail-dm3nam05lp2056.outbound.protection.outlook.com [104.47.49.56]) by mx0b-00128a01.pphosted.com with ESMTP id 2v6hkcq42j-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT); Thu, 26 Sep 2019 06:52:20 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=f5uUbUH2lplTyKi4fff4mZSP/fInkilfDjx7kdQRDO+6Z+9nzPo/iL/IyhUVFAj4M28sPNfqohH8ISAAG/hbGfs/MwbezyYBZd7g6QEEUQis+yQLS9Xed/5qmbni/eZfk56W3ovFBlaKlsP9wRBgJ/ldFUppzJoXpwVcNJs08gettUacU54qrBXAX41Fq8xlSH+t0gLIgYEc5OEshnu5G8EOUdY/fgF2j9PlZ2uqTBaFXm2mZKCDCVlP/9jmdvjFbZ2aDkgg2gVwOsHFTKKu3b7lHLgplkLmGjNiipa7nPuuuRnKpOeWiWBFGeEdxkSMVHzhlmnd8AM+Sj9YqJiC/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ESeHHXBfFrG988glXvaIvhiPmUc6BnNdkc+lsz3piTo=; b=UnbQFKsCJHyZ/B91BHq+K2LpR5oCd1k0uiB00hFhajBqG5Zry5npY18NxPhXJkaip72KWeKiKpokc7ynJ2FUspMw8KrLF1AcGPUVR1I4J7xX5K8/Vepjrgy+NDCA+repJ7ElhNDJvkMhUFc43LTOxbIyHIyICl2WU687Xo39afUqbXnsCI65oX93jR+PU/ndd9TJj8QXidmcspn1+HUNq43ek6kb4OJgph3NR9Kdn4OO8r+Z7WiPUuKcqRmOSGpMSeixhALgHdACvteCZCTjauhu6Ip95HhJbVMQ8NAY9oxdk3oi480smhhzuhZlb2tcVhR3L9wSfp5Qwh5gLg6wkg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 137.71.25.57) smtp.rcpttodomain=lists.infradead.org smtp.mailfrom=analog.com; dmarc=bestguesspass action=none header.from=analog.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=analog.onmicrosoft.com; s=selector2-analog-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ESeHHXBfFrG988glXvaIvhiPmUc6BnNdkc+lsz3piTo=; b=fktm5wD+UKwEVefxVl/w+1UZzNnnRvPZAAJDmtdAh8QFaga8O0Mofv8m0MPL3GNpkJbiu7f5pd1JJCZ4jhNhIW93j4iBqOf3DxDm/okeGN43W9oGo6uvRCtE7Y2DviJ9dT9FsxUc1p8PKeTC0OGTeDgP0WadeQ0yYdUdf7Tx+OQ= Received: from BN6PR03CA0079.namprd03.prod.outlook.com (2603:10b6:405:6f::17) by BN8PR03MB4708.namprd03.prod.outlook.com (2603:10b6:408:6d::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2284.20; Thu, 26 Sep 2019 10:52:14 +0000 Received: from SN1NAM02FT022.eop-nam02.prod.protection.outlook.com (2a01:111:f400:7e44::203) by BN6PR03CA0079.outlook.office365.com (2603:10b6:405:6f::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2305.17 via Frontend Transport; Thu, 26 Sep 2019 10:52:14 +0000 Received-SPF: Pass (protection.outlook.com: domain of analog.com designates 137.71.25.57 as permitted sender) receiver=protection.outlook.com; client-ip=137.71.25.57; helo=nwd2mta2.analog.com; Received: from nwd2mta2.analog.com (137.71.25.57) by SN1NAM02FT022.mail.protection.outlook.com (10.152.72.148) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_RSA_WITH_AES_256_CBC_SHA) id 15.20.2284.25 via Frontend Transport; Thu, 26 Sep 2019 10:52:14 +0000 Received: from NWD2HUBCAS7.ad.analog.com (nwd2hubcas7.ad.analog.com [10.64.69.107]) by nwd2mta2.analog.com (8.13.8/8.13.8) with ESMTP id x8QAqD6s014763 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=OK); Thu, 26 Sep 2019 03:52:13 -0700 Received: from saturn.ad.analog.com (10.48.65.123) by NWD2HUBCAS7.ad.analog.com (10.64.69.107) with Microsoft SMTP Server id 14.3.408.0; Thu, 26 Sep 2019 06:52:13 -0400 From: Alexandru Ardelean To: , , , , , CC: , , , , , , , Alexandru Ardelean Subject: [PATCH v4 02/19] spi: introduce spi_delay struct as "value + unit" & spi_delay_exec() Date: Thu, 26 Sep 2019 13:51:30 +0300 Message-ID: <20190926105147.7839-3-alexandru.ardelean@analog.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190926105147.7839-1-alexandru.ardelean@analog.com> References: <20190926105147.7839-1-alexandru.ardelean@analog.com> MIME-Version: 1.0 X-ADIRoutedOnPrem: True X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:137.71.25.57;IPV:NLI;CTRY:US;EFV:NLI;SFV:NSPM;SFS:(10009020)(136003)(396003)(346002)(39860400002)(376002)(199004)(189003)(2870700001)(246002)(476003)(51416003)(7416002)(14444005)(107886003)(11346002)(126002)(486006)(336012)(54906003)(426003)(110136005)(7636002)(305945005)(8936002)(50226002)(50466002)(7696005)(106002)(8676002)(446003)(4326008)(2616005)(186003)(47776003)(316002)(2906002)(26005)(5660300002)(478600001)(86362001)(70206006)(44832011)(1076003)(6666004)(356004)(48376002)(76176011)(2201001)(70586007)(36756003)(2101003);DIR:OUT;SFP:1101;SCL:1;SRVR:BN8PR03MB4708;H:nwd2mta2.analog.com;FPR:;SPF:Pass;LANG:en;PTR:nwd2mail11.analog.com;A:1;MX:1; X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 7a26c2ea-a270-4637-f7f1-08d7426f975f X-Microsoft-Antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600167)(711020)(4605104)(4709080)(1401327)(4618075)(2017052603328);SRVR:BN8PR03MB4708; X-MS-TrafficTypeDiagnostic: BN8PR03MB4708: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7219; X-Forefront-PRVS: 0172F0EF77 X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Message-Info: Vk8BOIpZ8upk4WghlPYBlqIKReD/m6fkX0WLyTwMRB+G/1HqVM5ORg80Y6W2Er5GJwWkC/9ezV03TupxXGAOS8BbbOk3TzQqPa4WY9lgp5m6U3RvoY7WG6WAU3qFbfaif3meB+fME7EAPiXcaRfr3Zb6AUdgRLwPq9zUbcyC9vDMh6yH3AhJcSpnHk2Lfm2n15KN7b4xPLl8wMn770kSwjg2xbVFL1hi7myl5xgya8BEq9rFMzJaLiYEgl9iRTsK2GOmquy7KqEkr1iDx71akPmlEUMeM1N1EZmNiPCP1v6+12t74XKe+9ml/EneypcQ/9gvGCm1vbNXInAH8sXbAl7L9BA+Q6h8m+V5MkLeNUl8A0n/yJSyTTsaHxkLc3bQx0MbFNpUc7efe02OGlRCawo1bvakTucwX1OSfZIdilo= X-OriginatorOrg: analog.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Sep 2019 10:52:14.3365 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 7a26c2ea-a270-4637-f7f1-08d7426f975f X-MS-Exchange-CrossTenant-Id: eaa689b4-8f87-40e0-9c6f-7228de4d754a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=eaa689b4-8f87-40e0-9c6f-7228de4d754a;Ip=[137.71.25.57];Helo=[nwd2mta2.analog.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN8PR03MB4708 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.95,1.0.8 definitions=2019-09-26_05:2019-09-25,2019-09-26 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 impostorscore=0 mlxlogscore=999 clxscore=1015 mlxscore=0 suspectscore=0 lowpriorityscore=0 priorityscore=1501 adultscore=0 spamscore=0 malwarescore=0 bulkscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-1908290000 definitions=main-1909260103 Sender: linux-spi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org There are plenty of delays that have been introduced in SPI core. Most of them are in micro-seconds, some need to be in nano-seconds, and some in clock-cycles. For some of these delays (related to transfers & CS timing) it may make sense to have a `spi_delay` struct that abstracts these a bit. The important element of these delays [for unification] seems to be the `unit` of the delay. It looks like micro-seconds is good enough for most people, but every-once in a while, some delays seem to require other units of measurement. This change adds the `spi_delay` struct & a `spi_delay_exec()` function that processes a `spi_delay` object/struct to execute the delay. It's a copy of the `cs_change_delay` mechanism, but without the default for 10 uS. The clock-cycle delay unit is a bit special, as it needs to be bound to an `spi_transfer` object to execute. Signed-off-by: Alexandru Ardelean --- drivers/spi/spi.c | 51 +++++++++++++++++++++++++++++++++++++++++ include/linux/spi/spi.h | 18 ++++++++++++--- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index c90e02e6d62f..1883de8ffa82 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -1106,6 +1106,57 @@ static void _spi_transfer_delay_ns(u32 ns) } } +static int _spi_delay_to_ns(struct spi_delay *_delay, struct spi_transfer *xfer) +{ + u32 delay = _delay->value; + u32 unit = _delay->unit; + u32 hz; + + if (!delay) + return 0; + + switch (unit) { + case SPI_DELAY_UNIT_USECS: + delay *= 1000; + break; + case SPI_DELAY_UNIT_NSECS: /* nothing to do here */ + break; + case SPI_DELAY_UNIT_SCK: + /* clock cycles need to be obtained from spi_transfer */ + if (!xfer) + return -EINVAL; + /* if there is no effective speed know, then approximate + * by underestimating with half the requested hz + */ + hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2; + if (!hz) + return -EINVAL; + delay *= DIV_ROUND_UP(1000000000, hz); + break; + default: + return -EINVAL; + } + + return delay; +} + +int spi_delay_exec(struct spi_delay *_delay, struct spi_transfer *xfer) +{ + int delay; + + if (!_delay) + return -EINVAL; + + delay = _spi_delay_to_ns(_delay, xfer); + if (delay < 0) + return delay; + + _spi_transfer_delay_ns(delay); + + return 0; +} +EXPORT_SYMBOL_GPL(spi_delay_exec); + static void _spi_transfer_cs_change_delay(struct spi_message *msg, struct spi_transfer *xfer) { diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h index af4f265d0f67..c18cfa7cda35 100644 --- a/include/linux/spi/spi.h +++ b/include/linux/spi/spi.h @@ -89,6 +89,21 @@ void spi_statistics_add_transfer_stats(struct spi_statistics *stats, #define SPI_STATISTICS_INCREMENT_FIELD(stats, field) \ SPI_STATISTICS_ADD_TO_FIELD(stats, field, 1) +/** + * struct spi_delay - SPI delay information + * @value: Value for the delay + * @unit: Unit for the delay + */ +struct spi_delay { +#define SPI_DELAY_UNIT_USECS 0 +#define SPI_DELAY_UNIT_NSECS 1 +#define SPI_DELAY_UNIT_SCK 2 + u16 value; + u8 unit; +}; + +extern int spi_delay_exec(struct spi_delay *_delay, struct spi_transfer *xfer); + /** * struct spi_device - Controller side proxy for an SPI slave device * @dev: Driver model representation of the device. @@ -834,9 +849,6 @@ struct spi_transfer { u16 delay_usecs; u16 cs_change_delay; u8 cs_change_delay_unit; -#define SPI_DELAY_UNIT_USECS 0 -#define SPI_DELAY_UNIT_NSECS 1 -#define SPI_DELAY_UNIT_SCK 2 u32 speed_hz; u16 word_delay;