Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp7055716ybi; Thu, 13 Jun 2019 08:50:00 -0700 (PDT) X-Google-Smtp-Source: APXvYqw9eT2NlJ8QdNQll3nj/BsOQ09bCgjRaWGKc2hq3zviPfyhuaYxx1mSNFhIuW7aciJ1Wuyh X-Received: by 2002:a17:902:28c9:: with SMTP id f67mr89420117plb.19.1560441000742; Thu, 13 Jun 2019 08:50:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560441000; cv=none; d=google.com; s=arc-20160816; b=jKe2KRYJ4ug86ER9NrfdzT+cQx8COY8ph4QfDV1896Ky8ILeH3SZofBk7P3RQuY2Xq X9gtfTzvrhTGJebp40YVGH42XR+HryDIkXKKpOWv658k1eW6bylEsRzWp9dU/UMRJ9Yz Blk1VzebekoCZIlfoBuio+VNcD2qFrNh1d8LKDNuM0+UHa5Zo6DFfi07YViTSzgO5M7g WTFflS95+tiVxiwvLE6j0wHSp8SOQwuV7dG5xU/BC0T503ksBv2IE19SQwUIRFS/pEYs S1xWN4w/XMapVKPjdnTRUu6aaRj7TTDZqzahYjcVU+nAPAgJaRRRMBAdkVWgU8UZcdK5 DDkg== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=BbQ3UZU2V9GrTICPAPPBalP/n5LJPEV25JO+nn6N0HU=; b=FEOkSj2uGtXsQT+9wdS2rF39Bq+zOn7LixvgBjcCExvJ8qYj6bZ/L2Alfsw5/PrHaN wU+L80kBrCsCUfRmSZvEN0K/QI4r42Sq6oVqpIzVeB3b+70duMDUBYBWJYurZUtLj6CF y5/B35ljYe8KaxX0Aa5rTExLFoEDYUC6pp3WBDq0BSgNNcqoLgTtjcBrdYwzMnwkWsQJ OG8SQ8bbXRjw15qqjgQqvm7BdxlaDSEydIyBMa+TyVP+aDFgNuycgremlqK/WzlEsTqr TpKuH0svEC2YW0KXdulEL6AvZ25JA6H8HatEqJy8cDKqyzvEgnvxVaXZdUIeSDUqa8/A NNnw== ARC-Authentication-Results: i=1; mx.google.com; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=collabora.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e4si20750pjj.34.2019.06.13.08.49.45; Thu, 13 Jun 2019 08:50:00 -0700 (PDT) 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; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389604AbfFMPsz (ORCPT + 99 others); Thu, 13 Jun 2019 11:48:55 -0400 Received: from bhuna.collabora.co.uk ([46.235.227.227]:52428 "EHLO bhuna.collabora.co.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731710AbfFMJPD (ORCPT ); Thu, 13 Jun 2019 05:15:03 -0400 Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: eballetbo) with ESMTPSA id 73D26260CB2 Subject: Re: [PATCH] backlight: pwm_bl: Fix heuristic to determine number of brightness levels To: Matthias Kaehlcke , Thierry Reding , Lee Jones , Daniel Thompson , Jingoo Han , Bartlomiej Zolnierkiewicz Cc: linux-pwm@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-fbdev@vger.kernel.org, linux-kernel@vger.kernel.org, Douglas Anderson , Brian Norris , Pavel Machek , Jacek Anaszewski References: <20190612180003.161966-1-mka@chromium.org> From: Enric Balletbo i Serra Message-ID: Date: Thu, 13 Jun 2019 11:14:55 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.0 MIME-Version: 1.0 In-Reply-To: <20190612180003.161966-1-mka@chromium.org> Content-Type: text/plain; charset=utf-8 Content-Language: en-GB Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Matthias, On 12/6/19 20:00, Matthias Kaehlcke wrote: > 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 Tested on Samsung Chromebook Plus (16-bit pwm) Tested-by: Enric Balletbo i Serra > --- > 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 fb45f866b923..0b7152fa24f7 100644 > --- a/drivers/video/backlight/pwm_bl.c > +++ b/drivers/video/backlight/pwm_bl.c > @@ -194,29 +194,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) >