Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp374479pxx; Wed, 28 Oct 2020 07:00:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz3rxExp02NZeA7iQUgSbe9pAUaNuYPYUmKDAWJoraUwFqL8zXNrnVjYpI8A1kmguEUqNYn X-Received: by 2002:a17:906:711:: with SMTP id y17mr7852624ejb.271.1603893644952; Wed, 28 Oct 2020 07:00:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603893644; cv=none; d=google.com; s=arc-20160816; b=MGWBodBiBvtDC8zpDBygRSDy0KWGuVV8AhyVpjQeg1mJdCl8qmDE1/x5UIHc87y0Em WjgdjfqvyIH12AuOgAQfbal7xlx83ha428T39Mgv7Wla1F+9KlHN4pgu05mx/MYzF/H7 5jJQ9Ttaxn7IvB14Lmak5oTUmiF3Z8mr4TQdn70zeriKcbzZ/1tdaBxgzDi/wh1McPnM vx3lm/66I9pqBcaVY3AL89ARNwZN01e5yUKBt8XhIlopmwFCML3Q/oNheKUjF+Ld/It0 p8aqwaQJMN1GfTaOBaIhGdNHrKh2B8rQFT8RZANTH+k10CXj/QXY9BqBshxlC4qI2JJf mZ+g== 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=GBY3EpSVu3ehh/SpOzezkiqnJDmw+SaXy+W/drXBa/8=; b=PT3IY3V0Mxc8KLER/941GcyWmyYJYEbIHr5K2z2HiK5OCyVWWoMYsPj3+btQ27HGIm 0LQyDQdL/ExItorUy8v9Js7zsDxOk9eNWq3SIsOY90yaCHTxcfuH+yuBQga2WDtJYdaJ nwXN7XY69NxVzfzEqkBegg7bXn1a58cB93FgScGUy265uhbjs2DDS31Kt9ZrcGNtgPWV D8qCwQEMQT6tA4uZrO9JTNAZQsRHSYjpIjHrDVw4RVyQcXKj6xgo3YDJswDNiH3aAJU8 6au1Pczsy6pYz6mKX5kjpktEqmLAa3nyrFUTBA+kcAMQQWQCZ8J1NO/C1Aky8BCIHeiS 7I+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=n4DkImpY; 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 lg14si2653130ejb.82.2020.10.28.07.00.20; Wed, 28 Oct 2020 07:00:44 -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=n4DkImpY; 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 S1803604AbgJ0PxK (ORCPT + 99 others); Tue, 27 Oct 2020 11:53:10 -0400 Received: from mail.kernel.org ([198.145.29.99]:44480 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1798524AbgJ0P2c (ORCPT ); Tue, 27 Oct 2020 11:28:32 -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 728B8206F4; Tue, 27 Oct 2020 15:28:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603812512; bh=iY8+alkSPzYrj285BJl1b4U90y2HjaN/I9aouGCCXbI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=n4DkImpY+pSSm6NtL8Mz5qwwr3gcfkFcLF+wz4Ut9F8WRp7Iumv4XjBkb+jgM1XKR oQtgonO2Fu44kDxyYyyCkXxFep95B0x3DJG5EwT+EEol3oHY+d/EUUeHOp12kHGzDR Io6/MgBnnNeK6nNFJxHfpMTupPgowzjMh3jscR5M= 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.9 235/757] pwm: lpss: Add range limit check for the base_unit register value Date: Tue, 27 Oct 2020 14:48:05 +0100 Message-Id: <20201027135501.617162439@linuxfoundation.org> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20201027135450.497324313@linuxfoundation.org> References: <20201027135450.497324313@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 43b1fc634af1a..da9bc3d10104a 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