From patchwork Wed Jun 19 09:26:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= X-Patchwork-Id: 13703573 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 93C0EC2BA1A for ; Wed, 19 Jun 2024 09:27:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc: To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=DR7DaLA90oi7Sx2UQfP1lchaT92bhvbec7yBR4cuGW8=; b=ljttUYABYObeOksOe4Q5KbF4A8 Zz1MSpvZ+x41Qqu0zLPGVUz83AkXjmvr8APup2rzRkIqBxSTzcxHI7632O2pK/s3e6w/13xJr9Kru wFo6P1OMxOxrEwt0OHXHEd7ZnkpPyGtBdsMfoDATJFgvlo+FnB62NeAgxHwVbBDD78y9yfphXU2LI eNLW0IINo5oxa0ETQryGmXK+cncAfuahfgy/G2wUwP8UrUWm2KfYu3r0t54mYnI2FVwqutyGGGqBy 0GcFBFgKjke/5HPRtinyTVzhcjC6ss1TdSUit5ATsC0xBdfSVHSqdknerzX+xC3sC2EL9wVS7mw8G fQY/lc3Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sJrad-00000000aTG-46or; Wed, 19 Jun 2024 09:26:52 +0000 Received: from mail-wm1-x32d.google.com ([2a00:1450:4864:20::32d]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sJraZ-00000000aRR-2CJg for linux-arm-kernel@lists.infradead.org; Wed, 19 Jun 2024 09:26:49 +0000 Received: by mail-wm1-x32d.google.com with SMTP id 5b1f17b1804b1-424720e73e1so12382465e9.1 for ; Wed, 19 Jun 2024 02:26:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1718789205; x=1719394005; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=DR7DaLA90oi7Sx2UQfP1lchaT92bhvbec7yBR4cuGW8=; b=j5qyrX9j3KN/B3ncZrw0IS57+WQ14jO2x+Sr5vGEOJRjCOrN7sGgVr+oDuMrH36R6G VaJ7Q7VG3yp05MFVfrTBKutdsEYri0tqvlAIiIcWqTRnmgocHRojkfcXmkmYCbmzuUbk XJePKlVK2npvx1wbZRkfJo3X4s4FgDOjLp5mE7FJHiIkrJ7TrmfWWxBumYrM/TAanrIg XfI5xA9FDr1b56TP6cO0VlJx0iH0879b4tU45EK8XiquusxH8LTB35LpYIW0C1dJ/mH3 UZ1cNzg064w/X8pahXxtHvzT5DcH2+AzAxpyrc/78vmeLjDz0T3t+zJX0lPvvZ5iNgoT tGLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718789205; x=1719394005; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DR7DaLA90oi7Sx2UQfP1lchaT92bhvbec7yBR4cuGW8=; b=LpTKiuFWpkKSX5u9B38Tanj3IAB18gqHPJj+e6DrTOAuRjQWeR2Vgcl0xSwdbMBnnY 7aQIw+ixguVKDraul2lmvfAuZz7YJKBA2G9YVmIOkaxw0fXwrfFBvQ/ANMm9omFaHHEW VwVdi4kZtQ5Yl/urTCV9+N2CfTcbWeDAwakli4uh1NfwEjIF7mNFaXVX27r0+IiXvq2p dYytSBV/leo8iUcjRqMAeaQZXhbc1aRHTE45M3ANZdtgXfmHnfT3yR/AdkT6C3IazCvk /Fvr0kG+XWKtNjGWFppA4xLANdtQ8iTp0uuhDQrJO9XIrYJAJzhPf1mJZhu4yjY9GPs6 AQkg== X-Forwarded-Encrypted: i=1; AJvYcCVIEZgzq1Ico1XJF6KoahTjOfhCYLB/dvMb4cKKgQud5s+cOgH9h42xSzz/LTY6SPkBkvhbNMLJhayZ3NjNT/wISHKd7qO8h4QJXgdm66BWm3qWzbY= X-Gm-Message-State: AOJu0Yzgd1IidsPUui1kRlYPO7IXCAOL7jsXY0XJmgKfWmijobr12e0e dUbmpGiMa2u/XpZHSL79rMM3gPnbGj8fQqx5030NtsPcM7xpi5iqZMvUyTpH6D4= X-Google-Smtp-Source: AGHT+IECQtiC0VKWzrySd3phzgKGLPkfzADfJEpWsC3603JzVYfretGLD12PgHyZjjXmDS4xZ154rg== X-Received: by 2002:a7b:cd07:0:b0:422:370a:ca57 with SMTP id 5b1f17b1804b1-4247529bd22mr12959225e9.36.1718789204826; Wed, 19 Jun 2024 02:26:44 -0700 (PDT) Received: from localhost (p509153eb.dip0.t-ipconnect.de. [80.145.83.235]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-422870e9590sm257894845e9.23.2024.06.19.02.26.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Jun 2024 02:26:44 -0700 (PDT) From: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= To: Fabrice Gasnier , Maxime Coquelin , Alexandre Torgue , Lee Jones , Thierry Reding Cc: linux-pwm@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org, stable@vger.kernel.org Subject: [PATCH 1/2] pwm: stm32: Refuse too small period requests Date: Wed, 19 Jun 2024 11:26:24 +0200 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=916; i=u.kleine-koenig@baylibre.com; h=from:subject:message-id; bh=t/egHWd+wmcQlgsPv+ZLw8ZljE6PdVD/61DykbjcgzU=; b=owEBbQGS/pANAwAKAY+A+1h9Ev5OAcsmYgBmcqREO8R0IRhIv2cdHZmB13NIe9ta4LYlDZM/y NzbWbt7RF2JATMEAAEKAB0WIQQ/gaxpOnoeWYmt/tOPgPtYfRL+TgUCZnKkRAAKCRCPgPtYfRL+ TiHoB/0RVZ9pnx+IyNBO+qORuAclGa8F4EDvd1uHdrU9quZ5Vo2GZqZGHKe+jsPAwrnq5cDTuVL uRzTEYQN15LjnjI3DVpjzCXMdmxlSX8AR3oENWmvoURE8AvQjDS/aEEmkIC2M5qjg5JqtRSO475 b90u24VQc9RElRsZ+/44CaINWsndASrwah18oJ9ymattO4Zreaz2tyKDe08sImU50zAekOAbtAj /l7p8ln/vSiXb4xAuR9aSaknu6FuWBlgumoeguo47sJFpL7zbrOg1yTPdBW2JELuJ6JMbMSI9uS SuECLulflPslzng2EkpawVELbT7Kaeg/FdHL4xsR7rKXmJL7 X-Developer-Key: i=u.kleine-koenig@baylibre.com; a=openpgp; fpr=0D2511F322BFAB1C1580266BE2DCDD9132669BD6 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240619_022647_584977_4262921B X-CRM114-Status: GOOD ( 11.71 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org If period_ns is small, prd might well become 0. Catch that case because otherwise with regmap_write(priv->regmap, TIM_ARR, prd - 1); a few lines down quite a big period is configured. Fixes: 7edf7369205b ("pwm: Add driver for STM32 plaftorm") Cc: stable@vger.kernel.org Signed-off-by: Uwe Kleine-König --- drivers/pwm/pwm-stm32.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/pwm/pwm-stm32.c b/drivers/pwm/pwm-stm32.c index a2f231d13a9f..3e7b2a8e34e7 100644 --- a/drivers/pwm/pwm-stm32.c +++ b/drivers/pwm/pwm-stm32.c @@ -337,6 +337,8 @@ static int stm32_pwm_config(struct stm32_pwm *priv, unsigned int ch, prd = mul_u64_u64_div_u64(period_ns, clk_get_rate(priv->clk), (u64)NSEC_PER_SEC * (prescaler + 1)); + if (!prd) + return -EINVAL; /* * All channels share the same prescaler and counter so when two From patchwork Wed Jun 19 09:26:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= X-Patchwork-Id: 13703574 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 54176C2BA15 for ; Wed, 19 Jun 2024 09:27:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc: To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=7+qqb0yobL2/RGgvLBdYrDcwIAeg34/SRC7j1a+9BQM=; b=STNLzZBk+jzxmAYhZibU9LpKDH P9/Ua89U7LTgErvrIimUkxCwc20iC8sJFKy5Y2n+kdfdZXw7gVK9cKb3ik7r7qlsXbHEt11IWuWID hrBmAfqEASMKfDOtAOJUDCBxv0IIdqQh0vob4xZl8xYylYcdRjTEpxWuP2IAIQDKIQokZ3D7dwr6J c8Fl6LAX6EfbGvBQAiKPGZ5eTG2UqWxoyGPbens+U8+GJHBTHdFYrlP4gYKba4SFQoVW0qFs2MgsS 4xBHK6t+nbK7qa1/NjyskY68M4epqRLwuehBDbswvlTL92Wm53/K+7AcWp7/ogJUvGNkvJ10+0YHE fPgv4K1w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sJrar-00000000aXC-2Owd; Wed, 19 Jun 2024 09:27:06 +0000 Received: from mail-wm1-x330.google.com ([2a00:1450:4864:20::330]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sJraa-00000000aRc-3Fg3 for linux-arm-kernel@lists.infradead.org; Wed, 19 Jun 2024 09:26:50 +0000 Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-42122ac2f38so4573215e9.1 for ; Wed, 19 Jun 2024 02:26:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1718789206; x=1719394006; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=7+qqb0yobL2/RGgvLBdYrDcwIAeg34/SRC7j1a+9BQM=; b=Z+oF2unTA+wxBybZWfkhJqEP/Qde3qS7YbMTfoosX4jJh5QCEtE4n3PnFV7ctZ8zQD 8oHrx3WMD9IEbKb9sm7HvdMrOV1neaPO7bkeR5YWcmn6gS6n8tgqMPYCfjmeKPoPznxI vrqBQ769WIaPhkwFhKM9qTLawg13Fk0XPeNhOoLnIwkMvwn0Oba4hlu+GNI0ZQJlESs1 ZL6yUjajbAet60/T6OlH7gXlkrfJ+sBKPKhXFzfjMqyd434OMdGpeaHuaHkZa1on3HMC RFBjj5WumDs1+I8OwO+hOdTxD7MKQKHfoX8LcCXDFnx7AOCiRov0JwABev3/H1joMDj6 bE3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718789206; x=1719394006; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7+qqb0yobL2/RGgvLBdYrDcwIAeg34/SRC7j1a+9BQM=; b=W+PdOjGxXs2kPjHtinwMSOddejBykrxgSHM5tGgKvF7UFV4DrQth/6GQ+HJ6Ci3L9p osld2qskQO/6CwEs+CWYl5rnvuVmQCJ/Q0z702oy7fLAlVoa5PptNCWtqjot5vFQnlYU 4L/YoonmKs3VTBx8VZUXQgxCPUs3j56IHeqRD6bCYcCxY3pNlDt3cRol6+li1AZttiJv KZd8AIZVUSAidJ09+NYTYEe/NgZiV/89mLIAtlf5f2CojsXy6nHZ/B3nq0C0Dff7bsAM 6zaRU45FxjyyYSOFkwU8wZw64w0Ucbfiqz+rVw4FLBXqR4lpQp7l88nP0tkgIBUvmBoT ULBQ== X-Forwarded-Encrypted: i=1; AJvYcCX/8bsoEBaoK4e6wmWG/cOY/0y2yzvmOj/qySlqUsnl/55QtjKVj3LPOZI12HJlONKbctvv3E2F+vUGyPPTYngitdGMermh++KavTqEMnamcMzk3/A= X-Gm-Message-State: AOJu0YwByPzkVcZVqNXYF1s6cks0JlQRBtBhFKBt+2pq3Ts9ixYV6RcW tLpAJvXb62etGzC2iZmeaadMPFbeBvQ51ZwpAgjep6JNHHAXLIBlEKyZgj02grE= X-Google-Smtp-Source: AGHT+IGfWLTr9ssnbsRhjkvHvT/KYb0ndiiJ29jhEvmScaQZp69vPy0UokUJtxlEqZze+8ABe7aCoQ== X-Received: by 2002:a05:600c:319c:b0:424:71e6:258b with SMTP id 5b1f17b1804b1-42471e6273bmr40938175e9.6.1718789206193; Wed, 19 Jun 2024 02:26:46 -0700 (PDT) Received: from localhost (p509153eb.dip0.t-ipconnect.de. [80.145.83.235]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36075114dcfsm16587999f8f.114.2024.06.19.02.26.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Jun 2024 02:26:45 -0700 (PDT) From: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= To: Fabrice Gasnier , Maxime Coquelin , Alexandre Torgue , Lee Jones , Thierry Reding Cc: linux-pwm@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org, stable@vger.kernel.org Subject: [PATCH 2/2] pwm: stm32: Fix calculation of prescaler Date: Wed, 19 Jun 2024 11:26:25 +0200 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2596; i=u.kleine-koenig@baylibre.com; h=from:subject:message-id; bh=uprloTlZWW2ITNEvdDXN3a5XnP9pblOhaEltw1mAAZI=; b=owEBbQGS/pANAwAKAY+A+1h9Ev5OAcsmYgBmcqRH6SJGfFE/V8DVVjzbmOURvUhFQT3Nwp0Hw XiLMsfjVsuJATMEAAEKAB0WIQQ/gaxpOnoeWYmt/tOPgPtYfRL+TgUCZnKkRwAKCRCPgPtYfRL+ ThXDCACwzZkJ8F2VHE/jJurLEZHTn+Is/zOVkDK5pPfy6uPLFPgQ3wTiaffvjAojLi8o7SCenwc PTNzMByX0+QENQYaYOgU+aFImVfTMNOFytVtNlozMHJFc8iAttlSnpVgYcAJLwnfvL89sC6XdvA 4fNIeL29SRmOv2DbKR+JQFdDXUFKNbCecR3xbxhEmIwB6M8xAq3gfbLzT56M/N4JrdkMdpYfL0F tehelyoRLei/rPaRuqnx6q4oYk/YgB/nAkDayV+xexI9ls75YJTbTncg1CEldC3CNJJRSN5j/rD aIiTU5NiM9HiVv0hVmREkOTdZqZw5h1nmlOlUlCrdy/eW1mB X-Developer-Key: i=u.kleine-koenig@baylibre.com; a=openpgp; fpr=0D2511F322BFAB1C1580266BE2DCDD9132669BD6 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240619_022648_837256_3616C6E7 X-CRM114-Status: GOOD ( 17.11 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org A small prescaler is beneficial, as this improves the resolution of the duty_cycle configuration. However if the prescaler is too small, the maximal possible period becomes considerably smaller than the requested value. One situation where this goes wrong is the following: With a parent clock rate of 208877930 Hz and max_arr = 0xffff = 65535, a request for period = 941243 ns currently results in PSC = 1. The value for ARR is then calculated to PSC = 941243 * 208877930 / (1000000000 * 2) - 1 = 98301 This value is bigger than 65535 however and so doesn't fit into the respective register. In this particular case the PWM was configured for a period of 313733.4806027616 ns (with ARR = 98301 & 0xffff). Even if ARR was configured to its maximal value, only period = 627495.6861167669 ns would be achievable. Fix the calculation accordingly and adapt the comment to match the new algorithm. With the calculation fixed the above case results in PSC = 2 and so an actual period of 941229.1667195285 ns. Fixes: 8002fbeef1e4 ("pwm: stm32: Calculate prescaler with a division instead of a loop") Cc: stable@vger.kernel.org Signed-off-by: Uwe Kleine-König --- drivers/pwm/pwm-stm32.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/drivers/pwm/pwm-stm32.c b/drivers/pwm/pwm-stm32.c index 3e7b2a8e34e7..2de7195e43a9 100644 --- a/drivers/pwm/pwm-stm32.c +++ b/drivers/pwm/pwm-stm32.c @@ -321,17 +321,24 @@ static int stm32_pwm_config(struct stm32_pwm *priv, unsigned int ch, * First we need to find the minimal value for prescaler such that * * period_ns * clkrate - * ------------------------------ + * ------------------------------ ≤ max_arr * NSEC_PER_SEC * (prescaler + 1) * - * isn't bigger than max_arr. + * This equation is equivalent to + * + * period_ns * clkrate + * ---------------------- ≤ prescaler + 1 + * NSEC_PER_SEC * max_arr + * + * As the left hand side might not be integer but the right hand side + * is, the division must be rounded up when doing integer math. There + * is no variant of mul_u64_u64_div_u64() that rounds up, so we're + * trading that against the +1 which results in a non-optimal prescaler + * only if the division's result is integer. */ prescaler = mul_u64_u64_div_u64(period_ns, clk_get_rate(priv->clk), (u64)NSEC_PER_SEC * priv->max_arr); - if (prescaler > 0) - prescaler -= 1; - if (prescaler > MAX_TIM_PSC) return -EINVAL;