Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp527631pxx; Wed, 28 Oct 2020 10:20:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxnRgDmX7FpA74ybGwv5nD3t0VSnrADObFZL4KWm9JX1gSXWI1sTzbW2qPPBVTtOmYPebwK X-Received: by 2002:a17:906:383:: with SMTP id b3mr63298eja.351.1603905609508; Wed, 28 Oct 2020 10:20:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603905609; cv=none; d=google.com; s=arc-20160816; b=ys5tl5ZGJ05AiyZZvhhnHrUlMa5h6jINuPCzqnkmhVb8DWNrVrIEqf2JT5kLuorC07 lmvs1hnrjrocuoOKH9ZYwGysOubLjJVjRYhDq06VqLfCojWiNAgdGyGMst+gYuyRY4dK TUYhm7f7hMnfwemfMuxkYiJfIMmO5jCEriWd+jcvedCNT8nMw9XGehwrDgmU47ZLbUET 9ebPStc2RRNoAiyyWdIidlaHQtSjG5/XS3cY+9qRoB62DQ+JaHkC5/GAKmP3RKUyS1Ix fWpk8fAmlklplvtkES6jbI7IlWPxrzgnb6psYxn/oVStMPQXQ4/SM/6MqrQfGm8Xxo9u KH/Q== 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=a2u1MjrBUX6fNG85TcjdRKWtFdGYsuY3RxFrV8N9YMs=; b=hVw/JpA4mkbr+r43criVigjYgdFNcpjiSWQGV00sOGTHtwjYUFr7LgKfwQRZXuL+lV BQrllKyjpAl3xpiaWbz7SrnpF6r4FOgcdF5HoX7Jj4XFvl5xWhEEDNVpDne+2pzKKmQd gc38Jr+IkvvYRIrn6uObOkNFXINYC6XOg8bmbuD90JXDaLHE6FWUBnA1hSB+ZIOvBQcz YhXPIygIqRPkksnDnbDiqTC4HtWHLbl1hOhXh29QGJMpQq1syBSvxfADpZh93T6B27kf pVJ0nX5E9K1UxzHyw694eE4k9GTHfAmoDu5vKhEoAF/h+vWgpKcjkSvaMMrGFQxZP7Fa jsMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="P9znOwx/"; 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 h20si3592583eja.402.2020.10.28.10.19.47; Wed, 28 Oct 2020 10:20:09 -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="P9znOwx/"; 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 S1820831AbgJ0RjB (ORCPT + 99 others); Tue, 27 Oct 2020 13:39:01 -0400 Received: from mail.kernel.org ([198.145.29.99]:33134 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1760266AbgJ0OeX (ORCPT ); Tue, 27 Oct 2020 10:34:23 -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 5CD1420773; Tue, 27 Oct 2020 14:34:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603809261; bh=loRu9X5YDZQepfBgowK8l1UTSryY7TjMd+cnq7+KGXg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=P9znOwx/MvNnV26X/TkBosJJmD41zPFOg500BkWrxlEzbt+vPBBFxa663AyVsMqHF wv3xk1e2rhIwjD6pgt8tFpY7Sg07XtIMBfCwAUcm64439t5glIJX4MUg3l5heOtpKO MjAxazxsfWe36s8rX3EnRfNC9bfuUch995t6RTu4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andy Shevchenko , Thierry Reding , Hans de Goede , Sasha Levin Subject: [PATCH 5.4 133/408] pwm: lpss: Add range limit check for the base_unit register value Date: Tue, 27 Oct 2020 14:51:11 +0100 Message-Id: <20201027135501.266309423@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 ef9f60daab309558c8bb3e086a9a11ee40bd6061 ] When the user requests a high enough period ns value, then the calculations in pwm_lpss_prepare() might result in a base_unit value of 0. But 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. Adding 0 to the counter is a no-op. The data-sheet even explicitly states that writing 0 to the base_unit bits will result in the PWM outputting a continuous 0 signal. When the user requestes a low enough period ns value, then the calculations in pwm_lpss_prepare() might result in a base_unit value which is bigger then base_unit_range - 1. Currently the codes for this deals with this by applying a mask: base_unit &= (base_unit_range - 1); But this means that we let the value overflow the range, we throw away the higher bits and store whatever value is left in the lower bits into the register leading to a random output frequency, rather then clamping the output frequency to the highest frequency which the hardware can do. This commit fixes both issues by clamping the base_unit value to be between 1 and (base_unit_range - 1). Fixes: 684309e5043e ("pwm: lpss: Avoid potential overflow of base_unit") Reviewed-by: Andy Shevchenko Acked-by: Thierry Reding Signed-off-by: Hans de Goede Link: https://patchwork.freedesktop.org/patch/msgid/20200903112337.4113-5-hdegoede@redhat.com Signed-off-by: Sasha Levin --- drivers/pwm/pwm-lpss.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/pwm/pwm-lpss.c b/drivers/pwm/pwm-lpss.c index 16f32576b320c..d77cec2769b76 100644 --- a/drivers/pwm/pwm-lpss.c +++ b/drivers/pwm/pwm-lpss.c @@ -97,6 +97,8 @@ static void pwm_lpss_prepare(struct pwm_lpss_chip *lpwm, struct pwm_device *pwm, freq *= base_unit_range; base_unit = DIV_ROUND_CLOSEST_ULL(freq, c); + /* base_unit must not be 0 and we also want to avoid overflowing it */ + base_unit = clamp_val(base_unit, 1, base_unit_range - 1); on_time_div = 255ULL * duty_ns; do_div(on_time_div, period_ns); @@ -105,7 +107,6 @@ 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 - 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