Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp641115ybi; Fri, 21 Jun 2019 05:50:35 -0700 (PDT) X-Google-Smtp-Source: APXvYqzM6TIH0dPXpsCclMjIDMV7IpP8RpPVdEOx3+bH/JjtU+8KAELn8YlZmlG91fgwpj8/dd9m X-Received: by 2002:a17:90a:d814:: with SMTP id a20mr6555981pjv.48.1561121435904; Fri, 21 Jun 2019 05:50:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561121435; cv=none; d=google.com; s=arc-20160816; b=o9k4xMoQ1nEFGgrJ2NFx4fMbLsWID/3IfbI5V7yfcE+vdV44gTMdc4HOXRE3LS4VBD ki8uBFgLkjjmF4E9TDRED4De1Q/UGMYnR3O7zQuFFDUJl6NiSTkJpZD1SYsMQr7P5sFO k1UIkuawUNOI3ie+BYORqZVf7vL/9hH81u8zOczU4HDNvqShmQ8f18KPXCuNKIGFjyHv EuOHrLQw4nGlxDq0oESOq3aizAIctNUt8tJpG5mhjjirquwNJtnL1lmLce6IjgDbMcw6 prAK5VnYNkIcvdcnd4PMxyRZD+vaX5M9N+i3XEnEFix7DasnWwFR8X/+7wEdK0qOQhCw zpbw== 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:dkim-signature; bh=1W2WjOrHyAQxbAms+0lUtVAz8eLCNhjimYeSWnn2kxY=; b=LVfNbOEd4pD4JAdWYhFTO53HWw49RBxwFwX/a3GqiZNF7OFi9kRtIKcaqG/fLaI0JR vRtg4XH+tUWMS+tWWDhbemM2gSdY1J4eO2n66Ilv5iYz3lMslSqEi6/gczwF7Led+DEh SjbMepfp7HqgSmJLR3pV9kHGt6PRh6lqNTmqXC9MA1tqR9My42HmK7owqigcCjn6paQu ZgfFZ8FWaEfOq0UXPhNluSETeaDyMvPWAQkO12NtFWQxGKkDJ2kP7u0dx54fZLfklidT pIrNM4qkZfh5zeeRNk6UblB8loP9ttB6+9CiCKQgqfgCQuiZlFGkEZ/Pwj8krHdJesM+ a4Ng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=g6dZCD+0; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n9si2775718pjo.65.2019.06.21.05.50.20; Fri, 21 Jun 2019 05:50:35 -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; dkim=pass header.i=@linaro.org header.s=google header.b=g6dZCD+0; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726321AbfFUMth (ORCPT + 99 others); Fri, 21 Jun 2019 08:49:37 -0400 Received: from mail-wm1-f65.google.com ([209.85.128.65]:38297 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726311AbfFUMtg (ORCPT ); Fri, 21 Jun 2019 08:49:36 -0400 Received: by mail-wm1-f65.google.com with SMTP id s15so6478335wmj.3 for ; Fri, 21 Jun 2019 05:49:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=1W2WjOrHyAQxbAms+0lUtVAz8eLCNhjimYeSWnn2kxY=; b=g6dZCD+0BKGgFTxVXquMah27DBoxfovfxxX56Mb73a05HlB2qIrrf8g+SEftMtzlhD t2ZmeKQkKEqdJoh0k840zL94hKMkX2Cr3jFPzhyXuuPzIuzZI8n2v6V0CKpcvkP3onEG ANAjEFLKx38eZ1JlUKG0zzImhRZGY/9GTpMJoEH/sA2yxdJIOMeW8oxUvpI5N4uQG9z6 QyZAwFS7+whdd+jEHONQAhNRAf2v7RIJGmv718aiN3zc9iGCiJgcdOe4+qpfp5BgUMEI lI9onn9PFGeLpa7SJ99NelRtcQQrUG1r2rMHjx38oPYsVjwX/6soInLmMx6HSHceYYpI 7Jjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=1W2WjOrHyAQxbAms+0lUtVAz8eLCNhjimYeSWnn2kxY=; b=CdDKq2n99bzp1vZcvTvX0JI79vyVhJkrtA2mDDsNunEoH1NCIH2jO1GPHLJpuAVwx8 cIOgmSAwbMV22E+24qNkOV9Fhia1l9EUZWp/uLwBhHpUHFrKzJsZ34RY1oVeIRI6VZE7 3f2tqsGB60xYTNMGDkCXyeFYMxW/U4Z574CE/JlGr24KRNDyR7ZGtjz2GJXV4fVziFwA A/IWnD0UcsFCm3o9/rG2qlBgBRt8yXg/u5dAoHbAoz2BlQJ2JLj48SfdsgWDfHodrVZH NdZq9gksLYvU0q46BoNyM71inwcwOGsLaA+EH4eR8R6zut2OjHHTeBD3MATvn3/ZSqLc vYAA== X-Gm-Message-State: APjAAAVoNfyRax+eiiNnit2jFasWkxk3aVsE2aKDxtkETKhZRbIRMO1n orhmnHyzuUBHIhMJawWtFw0F0Q== X-Received: by 2002:a05:600c:2388:: with SMTP id m8mr3862455wma.23.1561121374405; Fri, 21 Jun 2019 05:49:34 -0700 (PDT) Received: from holly.lan (cpc141214-aztw34-2-0-cust773.18-1.cable.virginm.net. [86.9.19.6]) by smtp.googlemail.com with ESMTPSA id j32sm5690957wrj.43.2019.06.21.05.49.33 (version=TLS1_3 cipher=AEAD-AES128-GCM-SHA256 bits=128/128); Fri, 21 Jun 2019 05:49:33 -0700 (PDT) Subject: Re: [PATCH] backlight: pwm_bl: Fix heuristic to determine number of brightness levels To: Matthias Kaehlcke , Thierry Reding , Lee Jones , 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, Enric Balletbo i Serra , Douglas Anderson , Brian Norris , Pavel Machek , Jacek Anaszewski References: <20190612180003.161966-1-mka@chromium.org> From: Daniel Thompson Message-ID: Date: Fri, 21 Jun 2019 13:49:32 +0100 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; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 12/06/2019 19: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 As I understand it we can't determine the PWM quantization without actually programming it so the table could still be oversized after this patch (e.g. multiple entries end up with same physical brightness) but since it should always be monotonic and the table size will cap out at a sane value then: Acked-by: Daniel Thompson > --- > 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) >