From patchwork Mon Jul 15 20:29:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Li X-Patchwork-Id: 1960839 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=nxp.com header.i=@nxp.com header.a=rsa-sha256 header.s=selector2 header.b=f7oHbCkR; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2604:1380:45d1:ec00::1; helo=ny.mirrors.kernel.org; envelope-from=linux-pwm+bounces-2821-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org [IPv6:2604:1380:45d1:ec00::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WNDQN4T7lz1xqc for ; Tue, 16 Jul 2024 06:32:12 +1000 (AEST) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id BEA581C212BC for ; Mon, 15 Jul 2024 20:32:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3C4101442F0; Mon, 15 Jul 2024 20:30:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=nxp.com header.i=@nxp.com header.b="f7oHbCkR" X-Original-To: linux-pwm@vger.kernel.org Received: from AS8PR04CU009.outbound.protection.outlook.com (mail-westeuropeazon11011011.outbound.protection.outlook.com [52.101.70.11]) (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 D62CC143C6C; Mon, 15 Jul 2024 20:30:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.70.11 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721075430; cv=fail; b=aZb3A107YjxfQj+XsJdvoE4DF3DFQ1Ei1cKEX7VT8+xX+ud4dUh/euRz6Bv3oCNfyfj01eAyr8lWObF9fcwjLXU3UdjoMQgWT8guQyeUpVZ79pzRBMvFdC81cuXzvs4N0sI/rYcryCngTe/7Ko+kb+GIxgxOdxtFpvkjaoY1P44= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721075430; c=relaxed/simple; bh=Io9k/6Chqe/RxeT+6sunyKqWzcFgWqXaDQgBQg3+vMM=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=GVb/4QP1PHl9Dza6W/vwLQKxoMoXDmpHrLKnN5WpdQMho1zo+sevOXEA3QmNCYgoB9OnvdbvamGyFxc1mlGGYnJzQgbSJRKLcuWfmPhS/3W++LrEBWyV3XvkwaI9FDbYkD4I/Vf61W8MrFoJP/SsCJTofPH40nO/54m1xGUGYlo= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com; spf=pass smtp.mailfrom=nxp.com; dkim=pass (1024-bit key) header.d=nxp.com header.i=@nxp.com header.b=f7oHbCkR; arc=fail smtp.client-ip=52.101.70.11 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=nxp.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=swz8MBfOq6o1lSmvHfl1orzhaOWLweivMWKZc9mn2/CjQzpbpwgAGngV/Mm0ZjE5qAJ+rLkzt5YJkH3QHu/HriTL0drC/Kmc9sA57MOXl1eJj/vxblgL+ORk0sMpy6i02NasPJfnZFLz++DwtK3BaKn+ca1MA61OWd4dp3+jZoZG03ETzPinzchC85LQBkoTLGUjPFGI7fPawBcRMnx0P1mUSSlwR5DvOZ5PS5lKSouFmZ7VDeh+8mvAqLLnpNb1DMS4w6x8zNgP4c5zcR2L72dCYgzixWJI/SD3fdfvmPLMVdyWib6UZFO3+e0IcfrVBFCm13eNFG0zt79QL6EIDg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Jjq23hHJmyANEWygqMGhoMyEohbY3TN4P17gWnY6onw=; b=T5+E0UC73IGv2w+Hs4ClRrnXf4vwgkpRSIgV5cUdud8Y+Q1lBMTitfdw+RDO5X9nRg3do0yKp4IxcIeVi4nRmHqvt6J2ZSDv9luG2Q3ird2cnTgOwTdg78bKHGSYu8S3TGVD0UTmNeEJoPGIHv5RyEHs2ARHBrF2jPv95g4y8vRdMBedeVboUNx5IR2imTv/ydC4jb2qnf5DG9F/X03Ctw4MfpZLrdDecgQVQizzhaw/ma8CL0Z/KDJS483FCRz4YfevrAThU5iy9MElcq+YfsQ0Snrmp0dlsneWoKjMBCbPv4g995G7cjz2Ok0AzVmNjOd/uHeuaS5V3t9igUqKNA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Jjq23hHJmyANEWygqMGhoMyEohbY3TN4P17gWnY6onw=; b=f7oHbCkR4SIjbcBiTw42ln5vBr5BePME3nkJT0ptpAp1iOwcCbx0Tg4A3wSeBz703W2mGfDChmmuBHH9181FdFL81kgnyvW/6x4W3t+eqwaIITa9+ppTuiZGSFT6hwSrwFaoU3Z9b8LtBVbW3SPEYQYKbtUhK9WJTIZDime5MVw= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from PAXPR04MB9642.eurprd04.prod.outlook.com (2603:10a6:102:240::14) by PAXPR04MB9253.eurprd04.prod.outlook.com (2603:10a6:102:2bd::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7762.28; Mon, 15 Jul 2024 20:30:24 +0000 Received: from PAXPR04MB9642.eurprd04.prod.outlook.com ([fe80::9126:a61e:341d:4b06]) by PAXPR04MB9642.eurprd04.prod.outlook.com ([fe80::9126:a61e:341d:4b06%4]) with mapi id 15.20.7762.027; Mon, 15 Jul 2024 20:30:24 +0000 From: Frank Li Date: Mon, 15 Jul 2024 16:29:52 -0400 Subject: [PATCH v2 3/3] pwm: imx27: workaround of the pwm output bug when decrease the duty cycle Message-Id: <20240715-pwm-v2-3-ff3eece83cbb@nxp.com> References: <20240715-pwm-v2-0-ff3eece83cbb@nxp.com> In-Reply-To: <20240715-pwm-v2-0-ff3eece83cbb@nxp.com> To: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Shawn Guo , Sascha Hauer , Pengutronix Kernel Team , Fabio Estevam , Philipp Zabel Cc: linux-pwm@vger.kernel.org, devicetree@vger.kernel.org, imx@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, pratikmanvar09@gmail.com, francesco@dolcini.it, Frank Li , Clark Wang , Jun Li X-Mailer: b4 0.13-dev-e586c X-Developer-Signature: v=1; a=ed25519-sha256; t=1721075406; l=5471; i=Frank.Li@nxp.com; s=20240130; h=from:subject:message-id; bh=2Fx4wXwCrUhiTONLxXSADi3uaxoZ+vACq4nDfpp5ot0=; b=DkA4Cq/DwLbRv7Lq8zNAJMA/ORNaYhHBbvql7FyI/3BKsWbbeYyq1iGI8D7aHfcDDhUHuTpX0 Ywb7pJ1+n3VCXJb7qScGhF+KBbbZUi/VLwNrHOR+du2qZSTXgbUXTOJ X-Developer-Key: i=Frank.Li@nxp.com; a=ed25519; pk=I0L1sDUfPxpAkRvPKy7MdauTuSENRq+DnA+G4qcS94Q= X-ClientProxiedBy: SJ0PR03CA0166.namprd03.prod.outlook.com (2603:10b6:a03:338::21) To PAXPR04MB9642.eurprd04.prod.outlook.com (2603:10a6:102:240::14) Precedence: bulk X-Mailing-List: linux-pwm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PAXPR04MB9642:EE_|PAXPR04MB9253:EE_ X-MS-Office365-Filtering-Correlation-Id: 7e38e6e6-af66-4821-0051-08dca50cf4df X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|1800799024|376014|52116014|7416014|38350700014; X-Microsoft-Antispam-Message-Info: =?utf-8?q?ME/YN/ljdzVpKo8xh6Vu6UsCJRU9D9L?= =?utf-8?q?aJ8kV74eEV4SPNzEBkKw9dpet6GnxIKnF0EhRAs9xNFLtkq/mxAbZho6r39/vzdkV?= =?utf-8?q?GUznVu7EEi5BAJUgIBiLr3zxnwSsMvIxxqlr3KJscn+IPC1Ce/4CtmuvGiquFAbP6?= =?utf-8?q?QEMu1T43VqkdIi4CdDXIYyRGPoAKBwAORBg5WdOc7vfLywnWWwFHEuXrZmhePizT6?= =?utf-8?q?q/df3nUXQjG7BtydebwP3OeYJNbpIRGpUt8vPJTTnmzXD9Al8ZB+ktpmEhGA14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?VveUGCKsuaXcMvLMnZYnkGG4xpl0tgz2cfdQxtYzm1hjVz2ZdC0C0Vqbaufs1ImKn?= =?utf-8?q?SFSlXwvZOUIvrRtrmZ97k9t26BFEoLepxKm8g//qIpghmy0KyE4zWr6XSPCY3lt2m?= =?utf-8?q?bINURgUPMJzzKu2zBFj3PreaUZ/jxdt3cjeHIolAVVG9fymcC22eg8vw=3D?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PAXPR04MB9642.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(1800799024)(376014)(52116014)(7416014)(38350700014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?gJ09xudpznL8UAZF0V56lgOan0BV?= =?utf-8?q?L6IRdOhCa219I0fL1kF+fZB/AKP73S0CUZL7kF6ajG7CAMHCbb9rJ489Yj4PISMiB?= =?utf-8?q?4TTaOkhaD+P7hwSowNvUUrk7Ceq3zBWX2LbxyJd+BRqI+Sw4c40ik1HEs1Jwq4Ef5?= =?utf-8?q?EF23/fUjOT5UkykVg2XQfaJwkZ5k0BqLZF1eXjtNW28D3eMENfZS/e9PYs7xY7izu?= =?utf-8?q?J2MH/YH2COFodoxZ6P++1y7kRRiwfkK24uvhaR+fDtAhWbsiBQ60NYEyC2JbrlPXR?= =?utf-8?q?HKZQQSLniSpiz9VZp1T4dlQW9YnUkRA2Ob305RyMO+UamRsouZ0Pyf+qPylyIk1eL?= =?utf-8?q?cUfsEJq9FQpeuTT/fIsOl8ljLMz823wVXPinnd/M2Ad3rcwCTkxa41i6xE7sRoPyS?= =?utf-8?q?bn+TPsXgKWk5cw1Ql1ha152L87xFruwv+7hdx5xunQp0rFdWFnYTR/fL7C4bkWhbE?= =?utf-8?q?nVs0XhsGwB9YYtYQf6tx9vq3bWqRI0QWTDsnSdSj3BT/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dcAIzXAmg?= =?utf-8?q?p4xiS6u723KujVGfb5VSufpArhZP8G023Lu2g86XRathgfwNt6cRgmDetuDv75Zdp?= =?utf-8?q?IDXZlXi7lSPBeOODBfn5g7+d62Q4/+tLuXyz9HJcALnFqmlBBTFbMjnE=3D?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7e38e6e6-af66-4821-0051-08dca50cf4df X-MS-Exchange-CrossTenant-AuthSource: PAXPR04MB9642.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Jul 2024 20:30:24.7227 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: ph5i0N4E7n7ByiALahESR+KlS6Otes+TLC5jyUrszfPjbKb7liT1ckUe2nYjunndnWkUvIc6kkkY2m/0B9gnCg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAXPR04MB9253 From: Clark Wang Implement workaround for ERR051198 (https://www.nxp.com/docs/en/errata/IMX8MN_0N14Y.pdf) PWM output may not function correctly if the FIFO is empty when a new SAR value is programmed Description: When the PWM FIFO is empty, a new value programmed to the PWM Sample register (PWM_PWMSAR) will be directly applied even if the current timer period has not expired. If the new SAMPLE value programmed in the PWM_PWMSAR register is less than the previous value, and the PWM counter register (PWM_PWMCNR) that contains the current COUNT value is greater than the new programmed SAMPLE value, the current period will not flip the level. This may result in an output pulse with a duty cycle of 100%. Workaround: Program the current SAMPLE value in the PWM_PWMSAR register before updating the new duty cycle to the SAMPLE value in the PWM_PWMSAR register. This will ensure that the new SAMPLE value is modified during a non-empty FIFO, and can be successfully updated after the period expires. Write the old SAR value before updating the new duty cycle to SAR. This avoids writing the new value into an empty FIFO. This only resolves the issue when the PWM period is longer than 2us (or <500KHz) because write register is not quick enough when PWM period is very short. Fixes: 166091b1894d ("[ARM] MXC: add pwm driver for i.MX SoCs") Reviewed-by: Jun Li Signed-off-by: Clark Wang Signed-off-by: Frank Li --- Change from v1 to v2 - address comments in https://lore.kernel.org/linux-pwm/20211221095053.uz4qbnhdqziftymw@pengutronix.de/ About disable/enable pwm instead of disable/enable irq: Some pmw periphal may sensitive to period. Disable/enable pwm will increase period, althouhg it is okay for most case, such as LED backlight or FAN speed. But some device such servo may require strict period. - address comments in https://lore.kernel.org/linux-pwm/d72d1ae5-0378-4bac-8b77-0bb69f55accd@gmx.net/ Using official errata number fix typo 'filp' add {} for else I supposed fixed all previous issues, let me know if I missed one. --- drivers/pwm/pwm-imx27.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/drivers/pwm/pwm-imx27.c b/drivers/pwm/pwm-imx27.c index 253afe94c4776..e12eaaebe3499 100644 --- a/drivers/pwm/pwm-imx27.c +++ b/drivers/pwm/pwm-imx27.c @@ -27,6 +27,7 @@ #define MX3_PWMSR 0x04 /* PWM Status Register */ #define MX3_PWMSAR 0x0C /* PWM Sample Register */ #define MX3_PWMPR 0x10 /* PWM Period Register */ +#define MX3_PWMCNR 0x14 /* PWM Counter Register */ #define MX3_PWMCR_FWM GENMASK(27, 26) #define MX3_PWMCR_STOPEN BIT(25) @@ -232,8 +233,11 @@ static int pwm_imx27_apply(struct pwm_chip *chip, struct pwm_device *pwm, { unsigned long period_cycles, duty_cycles, prescale; struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip); + void __iomem *reg_sar = imx->mmio_base + MX3_PWMSAR; unsigned long long c; unsigned long long clkrate; + unsigned long flags; + int val; int ret; u32 cr; @@ -274,7 +278,53 @@ static int pwm_imx27_apply(struct pwm_chip *chip, struct pwm_device *pwm, pwm_imx27_sw_reset(chip); } - writel(duty_cycles, imx->mmio_base + MX3_PWMSAR); + /* + * This is a limited workaround. When the SAR FIFO is empty, the new + * write value will be directly applied to SAR even the current period + * is not over. + * + * If the new SAR value is less than the old one, and the counter is + * greater than the new SAR value, the current period will not filp + * the level. This will result in a pulse with a duty cycle of 100%. + * So, writing the current value of the SAR to SAR here before updating + * the new SAR value can avoid this issue. + * + * Add a spin lock and turn off the interrupt to ensure that the + * real-time performance can be guaranteed as much as possible when + * operating the following operations. + * + * 1. Add a threshold of 1.5us. If the time T between the read current + * count value CNR and the end of the cycle is less than 1.5us, wait + * for T to be longer than 1.5us before updating the SAR register. + * This is to avoid the situation that when the first SAR is written, + * the current cycle just ends and the SAR FIFO that just be written + * is emptied again. + * + * 2. Use __raw_writel() to minimize the interval between two writes to + * the SAR register to increase the fastest pwm frequency supported. + * + * When the PWM period is longer than 2us(or <500KHz), this workaround + * can solve this problem. + */ + val = FIELD_GET(MX3_PWMSR_FIFOAV, readl_relaxed(imx->mmio_base + MX3_PWMSR)); + if (duty_cycles < imx->duty_cycle && val < MX3_PWMSR_FIFOAV_2WORDS) { + c = clkrate * 1500; + do_div(c, NSEC_PER_SEC); + + local_irq_save(flags); + if (state->period >= 2000) + readl_poll_timeout_atomic(imx->mmio_base + MX3_PWMCNR, val, + period_cycles - val >= c, 0, 10); + + val = FIELD_GET(MX3_PWMSR_FIFOAV, readl_relaxed(imx->mmio_base + MX3_PWMSR)); + if (!val) + writel_relaxed(imx->duty_cycle, reg_sar); + writel_relaxed(duty_cycles, reg_sar); + local_irq_restore(flags); + } else { + writel_relaxed(duty_cycles, reg_sar); + } + writel(period_cycles, imx->mmio_base + MX3_PWMPR); /*