From patchwork Thu Jul 11 21:08:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Li X-Patchwork-Id: 13731063 Received: from DU2PR03CU002.outbound.protection.outlook.com (mail-northeuropeazon11012009.outbound.protection.outlook.com [52.101.66.9]) (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 2B4D12836A for ; Thu, 11 Jul 2024 21:09:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.66.9 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720732179; cv=fail; b=kzN3r9kOkHmTtA7MAD4VxRlYD21GNAfndrqcV1jxdWakXY7BODlPhqJLx1QGNBK/dJw/yyYWjdNp2gAvj6Cl7H+Wy1L6fN8Q6XjZYoDMVFVFatvC88bArN/qcPMcZhLOiztAM3RMjxwCbrEWc5FU7XP80/im62KVM4NS/d2Ac+Q= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720732179; c=relaxed/simple; bh=pT8hto8jasxqRR1wnXTN31s31kljdhdkKl0kbKVulDI=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=Mr4gJp0CZX42iquUctPcoMecxFDhdqgvffZlXqua99b0dwUj71rOKTaA4Br9yz5ZR2PE3O/NXIPOBtMjif1wR3sdsUJlvEW/MwdVlimlD/R8YNcZ+5Ry5nlFvyiN3lEEn1TPP12rsNn6w18Fj3s0t9z69nFoQgjHdFEElsE9Gao= 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=fe/Xt865; arc=fail smtp.client-ip=52.101.66.9 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=nxp.com header.i=@nxp.com header.b="fe/Xt865" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=KafeuTAR3LbqXJKi44CtsyP8K2Z+XsnHCtWADwaj8O5VjpeVO7S8ZhVKXL21SgkGuJLALlom0YjnXsElR1pSx2PFER1AoASxTUxz3gDeLFaHtukMNDDeT8ysueBWgvGXODV71bwfdUG9sUaSHeq0HNng96xrO7T6btYHq/oyyyEvluwR3kaXF8rtVdrbfLFgvTiFC32JrdmdZrw2RtxbXM0O7TxnnpwyYqhkhbftxH73nAWTFkLQVIdF5ZNyGHTGhSqF9z0101qwlqdR9H6Ehi0MqvEQRbrT79/xcdle91pOz1W4xsOPR6KYeyKgNDSzAzDaQ+h1sgdi2Wbe1Ao8qQ== 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=0HgHy2L/wkqbtzM2UW4DFT752rOoDt7Q/Ojbriar/5k=; b=TWn3njzPQD8NVT4UlgHYzkBZgbMpZsyxGoc2Uy1gyeppivBoduVZKNqr1pxLMUdugwmfD+qUhGT9ML6WU4Z2GZt5Kqh1aCZEsQ9pv0Y0keGnEmcJKulYjESjPYWZ3V1xhGwY7MS6Mzzwu3M4leIgWVTJc4hyuB6yopsZ2e89wdWMmOOh0G1gJNsleWj8GW1Mxj3+AeAWmAE/rPS7hQqT0pq4ozXauKuSCcbvH68craSy1pddAyptM6gFIyQygeqTv5C5++c19y/+YlxXRvRmYSh1zKFrNeTpvOpeMIoPr6Au65SLSrjVhlHiIwLetlldKqpqIuWdKwp0wD1bFpO53Q== 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=0HgHy2L/wkqbtzM2UW4DFT752rOoDt7Q/Ojbriar/5k=; b=fe/Xt865OxgoRvns35VmkeEu0fVuZMZFDisRgtSU8TkYmc3Bc0kwAsQor+ZBHsCDBl+MKIzOW+IsRidFHyddTeBzWaiWSmQiTeB9/lEYWSXxneHNSfqKSI8j1He96Hc/QjI8Rifre7r6opbRoKBKr2/FaqX0yXZJ+TfWBcEvRyo= 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 DBBPR04MB7915.eurprd04.prod.outlook.com (2603:10a6:10:1ea::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7762.22; Thu, 11 Jul 2024 21:09:34 +0000 Received: from PAXPR04MB9642.eurprd04.prod.outlook.com ([fe80::9126:a61e:341d:4b06]) by PAXPR04MB9642.eurprd04.prod.outlook.com ([fe80::9126:a61e:341d:4b06%2]) with mapi id 15.20.7741.033; Thu, 11 Jul 2024 21:09:34 +0000 From: Frank Li Date: Thu, 11 Jul 2024 17:08:58 -0400 Subject: [PATCH 3/3] pwm: imx27: workaround of the pwm output bug when decrease the duty cycle Message-Id: <20240711-pwm-v1-3-4d5766f99b8b@nxp.com> References: <20240711-pwm-v1-0-4d5766f99b8b@nxp.com> In-Reply-To: <20240711-pwm-v1-0-4d5766f99b8b@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, Frank Li , Clark Wang , Jun Li X-Mailer: b4 0.13-dev-e586c X-Developer-Signature: v=1; a=ed25519-sha256; t=1720732158; l=5043; i=Frank.Li@nxp.com; s=20240130; h=from:subject:message-id; bh=mKhhcEUhL7+dNPFdxDqp7YsAYZUKeNxwZ1WZHGuoG1Y=; b=QTOealos7b6dRVB6aSMOlrsvAtwiqbzE/chmVcBvhFVbo4TLav0qni2MX1J6zWcpUjURCLv3z JB4fdwQheo/Baup9p9Z5V7F6cPm8JdQ+Kv6zaHVl3UyY/KQRjQSG+Zw X-Developer-Key: i=Frank.Li@nxp.com; a=ed25519; pk=I0L1sDUfPxpAkRvPKy7MdauTuSENRq+DnA+G4qcS94Q= X-ClientProxiedBy: SJ0PR13CA0234.namprd13.prod.outlook.com (2603:10b6:a03:2c1::29) To PAXPR04MB9642.eurprd04.prod.outlook.com (2603:10a6:102:240::14) Precedence: bulk X-Mailing-List: imx@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PAXPR04MB9642:EE_|DBBPR04MB7915:EE_ X-MS-Office365-Filtering-Correlation-Id: 33ec8b9d-b783-4451-5505-08dca1edc37b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|52116014|7416014|366016|376014|1800799024|38350700014; X-Microsoft-Antispam-Message-Info: =?utf-8?q?pevQEVSIjRYyO/1G4u9WmTp4JRPW6b2?= =?utf-8?q?wincgEIGTqaBiWPkHr58w/+WVZUT4KqhLf5dewLNqyeFhdiq1vDtfY63GrCR2h5Va?= =?utf-8?q?/mmpiyQm8+HFn74+z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2FM3FIV8SD3zYx4fD8bVlBhMGYL+T3NtnUMPBC?= =?utf-8?q?mF+Xq3RstDbCK03h6eYnOsA6KMWuYQfFRdwp6KLOZAGo0TJwBI9qvCdsgPQPcEyXH?= =?utf-8?q?YAa47GvogUdGOIoGbLFbIpxudmslMPydc2hqH/b/NoIq4f7xdqhMj5N4t0W/oWCtA?= =?utf-8?q?qpDAucVT9UY976NCvg/jCJV/vZLjkXSemF7AgrFriuYM78Agu/WwHbPuOEv+9zEtl?= =?utf-8?q?8RHPyeBIWrFrrGJ7hsjOd5UYFJkFBZrCotJiiOQwq9SVhNANHqDft/6z/g1O3GfYC?= =?utf-8?q?h1CZftmszzGNoLtpcY59iRE2AEWJgav2OBhnbiie+E68dMjSq0LsPuhwF3zlOEOgs?= =?utf-8?q?KdXRBf65/Wo4bezdl1vqD6sQr2gXKb74Eg=3D=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)(52116014)(7416014)(366016)(376014)(1800799024)(38350700014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?4cHbg+ZeU7Eitt0k3nY2A+vNvAeC?= =?utf-8?q?SfE6nSdZo/+KDWeQG/N4TXxJW82op5/pvkcyYUJJgp76aVvNa2zlrpDqPVPWZSikF?= =?utf-8?q?AMwrS0QTwmTBNrEBu2AIjbyEz8W0pqXSckfVf08OJM2p98zojVKlh4S2o4wiVEYfm?= =?utf-8?q?SZcivZzHnN4KLWcoPzNsvyBmUH9gWjGFNOTr1WrtRx4u5rao0To3KbzNO/klCzAev?= =?utf-8?q?tGL1me9NJSsO2q4UYqmCfwmUqV+ZMN4BFXgNR0diRzZcnfVoIxXNTn+T1/qGQ4QAe?= =?utf-8?q?LLrbR4Fr+MXlClRv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GASkfgETdTaoT9AD0VSPKya8PzyfehjTjki+76j/LvQAUUQ?= =?utf-8?q?RmgNyFTRzyv7Fjm1PUsWDxWN4UmoyiHx0UeyYJW/K4nLeFk6iUXzqk0xtnJR+YNM6?= =?utf-8?q?tt+RJmAGJz9LIqStrbfS/FoWm66EbjfYwubXxVn87kTNbkVMY1LhT8uZP2Z/LJy6S?= =?utf-8?q?9ascVj+6mRWJYld01KzfZl7As+i6RoEF6C8IhHxdmxVGklod3vTXPVZAKTehJ2ZwK?= =?utf-8?q?Dw6Da6zp0ckxM7aVCpygbkn+mJfs+vP44kmqYJlFEO+o2V7ZeZfk9FYx3mchQKuNP?= =?utf-8?q?Epu+ME3ia8CS4PR/O8hs0XND4QTZLGkrJAPyI/XtdzeE4C24c+NAKyJItNwy4Yn2G?= =?utf-8?q?4zVJFELGx+tc?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 33ec8b9d-b783-4451-5505-08dca1edc37b X-MS-Exchange-CrossTenant-AuthSource: PAXPR04MB9642.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Jul 2024 21:09:33.9473 (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: BI1YD+Ndd9oUiXpTmVf9U5Hfi0CAQn7kJEJlC5sgkK2QG7dgy2wp9Eh2eUU9CYolrUfTAVO/whprv6hSOeXbEA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DBBPR04MB7915 From: Clark Wang When the SAR FIFO is empty, the write value is directly applied to SAR even though 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 flip the level. This result in a pulse with a 100% duty cycle. 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. Reviewed-by: Jun Li Signed-off-by: Clark Wang Signed-off-by: Frank Li --- drivers/pwm/pwm-imx27.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/drivers/pwm/pwm-imx27.c b/drivers/pwm/pwm-imx27.c index 032bce7d1fdd3..b817d73effc93 100644 --- a/drivers/pwm/pwm-imx27.c +++ b/drivers/pwm/pwm-imx27.c @@ -22,11 +22,13 @@ #include #include #include +#include #define MX3_PWMCR 0x00 /* PWM Control Register */ #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) @@ -92,6 +94,7 @@ struct pwm_imx27_chip { * value to return in that case. */ unsigned int duty_cycle; + spinlock_t lock; }; static inline struct pwm_imx27_chip *to_pwm_imx27_chip(struct pwm_chip *chip) @@ -220,10 +223,10 @@ static void pwm_imx27_wait_fifo_slot(struct pwm_chip *chip, sr = readl(imx->mmio_base + MX3_PWMSR); fifoav = FIELD_GET(MX3_PWMSR_FIFOAV, sr); - if (fifoav == MX3_PWMSR_FIFOAV_4WORDS) { + if (fifoav >= MX3_PWMSR_FIFOAV_3WORDS) { period_ms = DIV_ROUND_UP_ULL(pwm->state.period, NSEC_PER_MSEC); - msleep(period_ms); + msleep(period_ms * 2); sr = readl(imx->mmio_base + MX3_PWMSR); if (fifoav == FIELD_GET(MX3_PWMSR_FIFOAV, sr)) @@ -236,8 +239,10 @@ 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; @@ -279,7 +284,50 @@ 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. + */ + if (duty_cycles < imx->duty_cycle) { + c = clkrate * 1500; + do_div(c, NSEC_PER_SEC); + + spin_lock_irqsave(&imx->lock, flags); + if (state->period >= 2000) + readl_poll_timeout_atomic(imx->mmio_base + MX3_PWMCNR, val, + period_cycles - val >= c, 0, 10); + + if (!(MX3_PWMSR_FIFOAV & readl_relaxed(imx->mmio_base + MX3_PWMSR))) + __raw_writel(imx->duty_cycle, reg_sar); + __raw_writel(duty_cycles, reg_sar); + spin_unlock_irqrestore(&imx->lock, flags); + } else + writel(duty_cycles, reg_sar); + writel(period_cycles, imx->mmio_base + MX3_PWMPR); /* @@ -348,6 +396,7 @@ static int pwm_imx27_probe(struct platform_device *pdev) return PTR_ERR(imx->clk_32k); } + spin_lock_init(&imx->lock); chip->ops = &pwm_imx27_ops; imx->mmio_base = devm_platform_ioremap_resource(pdev, 0);