Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp527969pxx; Wed, 28 Oct 2020 10:20:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyM4y3bLKCcvVhsD0gPDQcz3qxMC8OddUs6Xtn3htb6fVYjGpoKLudeTK2JYt/kumap9znC X-Received: by 2002:a17:906:110b:: with SMTP id h11mr135760eja.156.1603905635251; Wed, 28 Oct 2020 10:20:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603905635; cv=none; d=google.com; s=arc-20160816; b=cVwaKn5oqc07HaGXyUR6c8nwPAiTPua4oraeHUh4o8Xusxd7GJtAXfOuYYbCrcnARc uRl4b1dBg/BZ+IFx1kVJmoafiUZQo2c4N4asnNtmIWPZ5ZS+BU25wQgB+pOQZ1pfEYDZ f1pyUTtL3i/LJNIYh4dxM5aPqdgLbQqFLMRG0ZpZ9h9Z54zGw9YKeu1aIHtJ3bZwNCo2 pXcqAQcAMZnUZmWVdSJQolDp8ZMxl/F9RA6hlswwJf9zRdrYcWZsmrZc1prclgL45i3F ZVw2y8baWgPor90OGzPw0TtCym9nXeWVebbk7TE5BpfNahSAwvKHbp9F0441QOSPMlXi myEw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=joarxsFG/I9unzaWlsInsoL3jBjWmOcOEftD1pyoQ9w=; b=WpFX5aBMd1bPZFnlA3dinL7u0DSHZ7hCDwHrYWGMXufjJqAiUWvmK4Hl3CzZfJfnf+ s/2sGjacLBn1g/OnqXuLN3jmnPF/BuRrXf7R8PHXZcvieH2I07txGz0xOzERdFh1e12p WyQ327Ah6q6m0qFr3kqd1rR4Kkkp9Dq9qYUjDVZfboR7zZk/5TjD0GJclpk+8dACkQzA NbajWn446K5hF4k7hfucqr3OwNg6+x9E2pZ2nbzn/dMrN9PIay67gCu078iCSt7WvOom NrJZ2ACebW+iLdpc6Dq27EI0Dl0O/f926cgXKWIIatB4InNEMzFXwyV0z0O0DLVZrflV /Q+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="T8zp26a/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ay1si32292edb.3.2020.10.28.10.20.12; Wed, 28 Oct 2020 10:20:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="T8zp26a/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1820839AbgJ0RjJ (ORCPT + 99 others); Tue, 27 Oct 2020 13:39:09 -0400 Received: from mail.kernel.org ([198.145.29.99]:33018 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1760252AbgJ0OeR (ORCPT ); Tue, 27 Oct 2020 10:34:17 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id CE57720709; Tue, 27 Oct 2020 14:34:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603809256; bh=/RCB6FeAOdZhnvFRYGIpS6f9yJ4NZb61VNU+5+d6VPM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=T8zp26a/ejOeqKQ3yGNzAQx8s8OMiO6rzu7Qc0o+QefjzS+QkYRaTydUNJuOv1OSB DREBIp6fs7GxrsOiOBZiDARTy1QdcQXF6ym/k1IjxZwmR/VEcCHvuXijNZpEf1lTXN YcRKYH8gN4cVRWUWj5l2mfftY15yxrQAQt1ryoW8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andy Shevchenko , =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , Thierry Reding , Hans de Goede , Sasha Levin Subject: [PATCH 5.4 132/408] pwm: lpss: Fix off by one error in base_unit math in pwm_lpss_prepare() Date: Tue, 27 Oct 2020 14:51:10 +0100 Message-Id: <20201027135501.215653156@linuxfoundation.org> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20201027135455.027547757@linuxfoundation.org> References: <20201027135455.027547757@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Hans de Goede [ Upstream commit 181f4d2f44463fe09fe4df02e03095cb87151c29 ] According to the data-sheet the way the PWM controller works is that each input clock-cycle the base_unit gets added to a N bit counter and that counter overflowing determines the PWM output frequency. So assuming e.g. a 16 bit counter this means that if base_unit is set to 1, after 65535 input clock-cycles the counter has been increased from 0 to 65535 and it will overflow on the next cycle, so it will overflow after every 65536 clock cycles and thus the calculations done in pwm_lpss_prepare() should use 65536 and not 65535. This commit fixes this. Note this also aligns the calculations in pwm_lpss_prepare() with those in pwm_lpss_get_state(). Note this effectively reverts commit 684309e5043e ("pwm: lpss: Avoid potential overflow of base_unit"). The next patch in this series really fixes the potential overflow of the base_unit value. Fixes: 684309e5043e ("pwm: lpss: Avoid potential overflow of base_unit") Reviewed-by: Andy Shevchenko Acked-by: Uwe Kleine-König Acked-by: Thierry Reding Signed-off-by: Hans de Goede Link: https://patchwork.freedesktop.org/patch/msgid/20200903112337.4113-4-hdegoede@redhat.com Signed-off-by: Sasha Levin --- drivers/pwm/pwm-lpss.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/pwm/pwm-lpss.c b/drivers/pwm/pwm-lpss.c index 75bbfe5f3bc29..16f32576b320c 100644 --- a/drivers/pwm/pwm-lpss.c +++ b/drivers/pwm/pwm-lpss.c @@ -93,7 +93,7 @@ static void pwm_lpss_prepare(struct pwm_lpss_chip *lpwm, struct pwm_device *pwm, * The equation is: * base_unit = round(base_unit_range * freq / c) */ - base_unit_range = BIT(lpwm->info->base_unit_bits) - 1; + base_unit_range = BIT(lpwm->info->base_unit_bits); freq *= base_unit_range; base_unit = DIV_ROUND_CLOSEST_ULL(freq, c); @@ -104,8 +104,8 @@ static void pwm_lpss_prepare(struct pwm_lpss_chip *lpwm, struct pwm_device *pwm, orig_ctrl = ctrl = pwm_lpss_read(pwm); ctrl &= ~PWM_ON_TIME_DIV_MASK; - ctrl &= ~(base_unit_range << PWM_BASE_UNIT_SHIFT); - base_unit &= base_unit_range; + ctrl &= ~((base_unit_range - 1) << PWM_BASE_UNIT_SHIFT); + base_unit &= (base_unit_range - 1); ctrl |= (u32) base_unit << PWM_BASE_UNIT_SHIFT; ctrl |= on_time_div; -- 2.25.1