Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp848024ybl; Fri, 24 Jan 2020 10:37:01 -0800 (PST) X-Google-Smtp-Source: APXvYqxliMXGIE8NdwkS3SiUM031DDlA9/AmwDkE7yAS1q3Ci+oREVmUuYS9psxHh5G6D83L00jM X-Received: by 2002:a05:6830:1515:: with SMTP id k21mr3585415otp.177.1579891021182; Fri, 24 Jan 2020 10:37:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579891021; cv=none; d=google.com; s=arc-20160816; b=tlcyMz8A+i9xEremYFPiZ25NJ1xNow5/mqORovj8XpO1+AIEQWecj2BzbUXHeCBHWb 7x2tHEAOSGbe6rnb66a7iU/5hJ9wi9o0EZIjNG3YPOG/M1D7ai11dRr3iAmkCQ7Xwi1l GWD+M7ipRZBkmvdlLfUC/NDJfhJr7108KsK0obzTG+24xA8kIfbqN/SJTXoMtdBrucL6 aBYhk9jXc8oqnCd0yY2+KV8+VK7MVGjC6uX36sjOupbdb7W6duFjDdLF8xGSuYRfnOWD fvMmDbGrI/6+8JHZhAv17nPVilZbeWpnRDv6xYXhzOE89fQqM/mr2uLeuvgRS0fEivbB fhpw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=4j9swZ/e9J4KRJxZA/O+UO2Y5M2kicgMCdg5OlvbfpE=; b=rKW0y+Ntdhba19tNEn9hKXkA0K16qb4QknnikZt0z7gfbf+d8BRtYcaTe6843405GJ JdzTb4+xsDkgdxgCHQvh1XSDEdp62m7v14Cm9Lsm77V2t9/TxKH30+HApfw4HvXbh9mC +xcMt5tgvgBJcVAoOubzpRb0CQCUbRAgUrN/l0ibvoKyr2BIGYcy2tHyAv+KupzO3Oqm L4hja2T7IG6IDUDm/fl1H+DWzdWIo/BN6zJLWy2x/1FpDPlK0Pp5/NEFHbXRxLviggo9 JdQrfVRStmkIjot4i618MzsFuhQIRZggOjq0u+lx114YGarM4fmcwQVaBBfFptYZPJUZ Opww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=wFLyV+bV; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h139si143443oib.85.2020.01.24.10.36.48; Fri, 24 Jan 2020 10:37:01 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=wFLyV+bV; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2391420AbgAXL0A (ORCPT + 99 others); Fri, 24 Jan 2020 06:26:00 -0500 Received: from mail.kernel.org ([198.145.29.99]:40300 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2403893AbgAXLZ6 (ORCPT ); Fri, 24 Jan 2020 06:25:58 -0500 Received: from localhost (ip-213-127-102-57.ip.prioritytelecom.net [213.127.102.57]) (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 BCCBF2075D; Fri, 24 Jan 2020 11:25:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1579865157; bh=MxQxF8QIHW4yZ+xMdIkYYejitL9UIWZRgNUdAFMgmt8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=wFLyV+bVkB2sj5LbIvjx9xnN+ggur3ka3seOcjF1GhBGDpfEjw5q7G7+bZfGpnb0F pwvFzyRz+NOesj274YtgFF1nnwB4JWjxjIMygRZdJVH8GyHpdsqXT2is+XIRpStobD 1wSKFyMD4ZQ+Pp2HqDR+oTwFuuMS0tEnbl/+m178= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Matthias Kaehlcke , Daniel Thompson , Enric Balletbo i Serra , Lee Jones , Sasha Levin Subject: [PATCH 4.19 453/639] backlight: pwm_bl: Fix heuristic to determine number of brightness levels Date: Fri, 24 Jan 2020 10:30:23 +0100 Message-Id: <20200124093143.782393427@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200124093047.008739095@linuxfoundation.org> References: <20200124093047.008739095@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Matthias Kaehlcke [ Upstream commit 73fbfc499448455f1e1c77717040e09e25f1d976 ] With commit 88ba95bedb79 ("backlight: pwm_bl: Compute brightness of LED linearly to human eye") the number of set bits (aka hweight()) in the PWM period is used in the heuristic to determine the number of brightness levels, when the brightness table isn't specified in the DT. The number of set bits doesn't provide a reliable clue about the length of the period, instead change the heuristic to: nlevels = period / fls(period) Also limit the maximum number of brightness levels to 4096 to avoid excessively large tables. With this the number of levels increases monotonically with the PWM period, until the maximum of 4096 levels is reached: period (ns) # levels 100 16 500 62 1000 111 5000 416 10000 769 50000 3333 100000 4096 Fixes: 88ba95bedb79 ("backlight: pwm_bl: Compute brightness of LED linearly to human eye") Signed-off-by: Matthias Kaehlcke Acked-by: Daniel Thompson Tested-by: Enric Balletbo i Serra Signed-off-by: Lee Jones Signed-off-by: Sasha Levin --- drivers/video/backlight/pwm_bl.c | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/drivers/video/backlight/pwm_bl.c b/drivers/video/backlight/pwm_bl.c index 7ddc0930e98c6..3a3098d4873be 100644 --- a/drivers/video/backlight/pwm_bl.c +++ b/drivers/video/backlight/pwm_bl.c @@ -199,29 +199,17 @@ int pwm_backlight_brightness_default(struct device *dev, struct platform_pwm_backlight_data *data, unsigned int period) { - unsigned int counter = 0; - unsigned int i, n; + unsigned int i; u64 retval; /* - * Count the number of bits needed to represent the period number. The - * number of bits is used to calculate the number of levels used for the - * brightness-levels table, the purpose of this calculation is have a - * pre-computed table with enough levels to get linear brightness - * perception. The period is divided by the number of bits so for a - * 8-bit PWM we have 255 / 8 = 32 brightness levels or for a 16-bit PWM - * we have 65535 / 16 = 4096 brightness levels. - * - * Note that this method is based on empirical testing on different - * devices with PWM of 8 and 16 bits of resolution. + * Once we have 4096 levels there's little point going much higher... + * neither interactive sliders nor animation benefits from having + * more values in the table. */ - n = period; - while (n) { - counter += n % 2; - n >>= 1; - } + data->max_brightness = + min((int)DIV_ROUND_UP(period, fls(period)), 4096); - data->max_brightness = DIV_ROUND_UP(period, counter); data->levels = devm_kcalloc(dev, data->max_brightness, sizeof(*data->levels), GFP_KERNEL); if (!data->levels) -- 2.20.1