Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp183211pxx; Wed, 28 Oct 2020 01:58:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwL7xcPmyDgrgQ00yoAq0E9isC4KotqRde8qSQw8yiOuhNTcB4lL0KOMYZWDZZLEcD65pQC X-Received: by 2002:a17:906:35d7:: with SMTP id p23mr3548639ejb.312.1603875519778; Wed, 28 Oct 2020 01:58:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603875519; cv=none; d=google.com; s=arc-20160816; b=MWbLTOJg2/HvALEn9/biAe4LC6lLa5FrF8RU3RCKDgOs6iGQaAIjZ9cGeekl/j78mQ k+G46qTzoiWpCQ+luwdkfGk9hUoa9/OlYaA2hFY8PO70xr6/NXvkcA7P0XY3UC4KQZPc 0x8wFS1QyHCdVFVC9snMkpTmDdopHXvCebCgHhql6Twevht2ruJ/4+DCjkAMwrWcbLrs C/9j3FXG4g53rAJZSDu2DXBSk+TknBd99vNlZN5YbFYthAEm4JClydT1mIDBHMRC08/8 x/AAnURjwnPUSxGkNT2tslTk8DbIZGdJ/NVV/Ca8KtEyhRCvmxq8wjrE6udkA2vse6vi yZ+w== 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=D7GTnbsb6iTHxYTtqUz8FZrw7nxwfbNZolcDwu0w7nE=; b=OTwaXDalB7wRmqpermuS9vcVy/ic2+6daQjx+J2tk57lIKRlQAyjPemsccUbv/59TH 6SEcsR3UFAuV4ym7B0/xf35kQI1ES2dRWq5Np5xGvricdBCXxRbmwoqpx/jPwa2C9ydV oSFSZs9F4yffU9MSfJGiFHDZKDfEzLdfOj8aXhmWMF7tXS5Ol7DRi9Oya/UlHVOsq0Nw ZzJiYEyVuJtG/7CDk597iHfaQVeiw0snrWeD+unT0RRl88nIpjzrgtWU1hwB2xFsPYY6 3TSctYoKBOjVS3lkpOQ0nlzLpAVdP9fggHGPakGRwPtjRL/9IQ+JEXbiWegTr2DhV6an i4Vw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=OWuR+0IF; 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 i19si2816688ejh.153.2020.10.28.01.58.17; Wed, 28 Oct 2020 01:58:39 -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=OWuR+0IF; 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 S1756651AbgJ0OOp (ORCPT + 99 others); Tue, 27 Oct 2020 10:14:45 -0400 Received: from mail.kernel.org ([198.145.29.99]:59440 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755609AbgJ0OKf (ORCPT ); Tue, 27 Oct 2020 10:10:35 -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 8D82E22202; Tue, 27 Oct 2020 14:10:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603807835; bh=/CLhGNoOMb9IoWHwQk3snVjYwauyyh99vyyoj8zjzaM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OWuR+0IFKySEuWTMy4olrQy/F/tvUg7IAIQu/JamaEf8G0p+531aBNSkag71VXrAj YiKWfmg0Q9ecPJy/Rofiz0dHxDBOl6vyESRRM0+2ay4u9aZtkydEoYUj7d3z94/dLY NGbTliTVwCXxcVYb1/gPbMpdnh4hxLcNhIBpqPiw= 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 4.14 058/191] pwm: lpss: Fix off by one error in base_unit math in pwm_lpss_prepare() Date: Tue, 27 Oct 2020 14:48:33 +0100 Message-Id: <20201027134912.528207566@linuxfoundation.org> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20201027134909.701581493@linuxfoundation.org> References: <20201027134909.701581493@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 7a4a6406cf69a..da63c029aa286 100644 --- a/drivers/pwm/pwm-lpss.c +++ b/drivers/pwm/pwm-lpss.c @@ -105,7 +105,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); @@ -116,8 +116,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