Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp5948094ybi; Wed, 12 Jun 2019 11:11:34 -0700 (PDT) X-Google-Smtp-Source: APXvYqxJSfFb+b/JYHeXrZmEMz278//Px6kyF4DRdY/0jFOzp9tWxenzDM+5YBBnz9CEbLzt8vef X-Received: by 2002:a65:6089:: with SMTP id t9mr25906890pgu.170.1560363093881; Wed, 12 Jun 2019 11:11:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560363093; cv=none; d=google.com; s=arc-20160816; b=zcv/HcqAOueamNn6BCsO2Nc33l/nr3b9tvl7R+4uyzQggFPxCAbnFHeBn8qVr5nAnn 4kcARzG/iiLbbgpvRdVxu9rZF9lBJvQbemR2vD5LRERLosfCB3E0O5154WlKumObAJJm uwiOCeI2zPx6uzxfGVsmLUSfg1bEyKdJ4D/jF1An80yteQ0qYEZKSrcHcHozGkC5bCXP ZwDtqYIV3pjd7c7nz95aSFBK/4CFxKeEosjAAIBhskJiyFD4R2HgmcKilTy6v2/HbgQc Z3Feuzd2B0qUruiLCm7FyQgBoGFv8BMULgtN6oJtYiOxrvIfQh9Ze0Kq0WRf1WovYDC5 DY2A== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=m17jcxzIUWgBG3pTTkNAHaWLFrUkZn/F2jLeWBAwk/I=; b=yOmB+Ki03crhfpSdNyTq6AgA+maS9iqCZLfExrUcAFZTR713Alza2EqE/657Hamm5D 4R78KdGfmA5y7uhPrfe7LxAZBxynlu0hGXWcbDN/ZQkZil4rGafN/6u2gjDQMlONu9e9 kSZ8eENuGM2cJ0AerILqEs4XHKSKemQInIZr8WxO1F44AR1SLaRUUcIABTPK3q+X4JSS 2jXUJzTkplgQ7WjeE7ACP3o07pvmU5Voe63C6/CH6XU3vufeRkpz2QkVGT2ysLZoiOkh 9HLMVN7AD6siJBDpJ2HD6OeYmDHx4NbHzNDc1tZsOollN4ANs6U+HVsKCZYtxvDcdu3y 2j+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=K9ECcdPp; 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=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g21si299522plo.306.2019.06.12.11.11.19; Wed, 12 Jun 2019 11:11:33 -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=@chromium.org header.s=google header.b=K9ECcdPp; 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=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728016AbfFLSAM (ORCPT + 99 others); Wed, 12 Jun 2019 14:00:12 -0400 Received: from mail-pl1-f196.google.com ([209.85.214.196]:42859 "EHLO mail-pl1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728084AbfFLSAM (ORCPT ); Wed, 12 Jun 2019 14:00:12 -0400 Received: by mail-pl1-f196.google.com with SMTP id go2so6929603plb.9 for ; Wed, 12 Jun 2019 11:00:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=m17jcxzIUWgBG3pTTkNAHaWLFrUkZn/F2jLeWBAwk/I=; b=K9ECcdPpoHNdUO2vD2Dylfi36k+JOk+4YD8Cfgm4Bn3VTPQdmcyN78WXD7L3d/HO0s hBdUSzi4DPjwEJ/logkQnM3DpJ+UFfFYykyjVso6WnixS6vlIlfMi6BeZJkZPIjx/bHO Dd9kFBgl1ab6JdR3Fm+FtrF4Nn2Y8//52ybEM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=m17jcxzIUWgBG3pTTkNAHaWLFrUkZn/F2jLeWBAwk/I=; b=Kl9TyYtsmdFR8NUBCFh3iznEeiD0hTQ3UC9c/DQVvol2DjlmZM5ilJzw2vtXhvLgby 070P/3i/YFw8UscBhsetIZvEc7Yfzp3f0NJhsdMvv06R2ggmRbImteRpCttIR+FcGIZt 1GI77RkBoWr4nf8cuBW7zYwNYM4E+cUUZI6HMswkXBSc5oUFDzDpfpqeJbnelXuYA/jb mUM/VkL5i7jkgTPy/cUR/MRXh39fIdkdX/PqbE5IqJMCaQZYqN3xcFoEVV43HoQ84p5M fkiLSRBhJAeDmCyvFme5EeRvhDbnsSK7MulLhOnRr7xjIhT56qfXjmnHVUpg4/RWriY8 SRTw== X-Gm-Message-State: APjAAAX5Azvh4Db2bjTFfDGD1DejC3JOZg0bofz0UuSezpixE5TD8TsP VvhI3glcsXhI3rdvM09sRDN2qg== X-Received: by 2002:a17:902:2006:: with SMTP id n6mr25655685pla.232.1560362411522; Wed, 12 Jun 2019 11:00:11 -0700 (PDT) Received: from localhost ([2620:15c:202:1:75a:3f6e:21d:9374]) by smtp.gmail.com with ESMTPSA id m6sm180932pgr.18.2019.06.12.11.00.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jun 2019 11:00:10 -0700 (PDT) From: Matthias Kaehlcke To: 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, Enric Balletbo i Serra , Douglas Anderson , Brian Norris , Pavel Machek , Jacek Anaszewski , Matthias Kaehlcke Subject: [PATCH] backlight: pwm_bl: Fix heuristic to determine number of brightness levels Date: Wed, 12 Jun 2019 11:00:03 -0700 Message-Id: <20190612180003.161966-1-mka@chromium.org> X-Mailer: git-send-email 2.22.0.rc2.383.gf4fbbf30c2-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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) -- 2.22.0.rc2.383.gf4fbbf30c2-goog