Received: by 10.213.65.68 with SMTP id h4csp2340779imn; Mon, 9 Apr 2018 01:40:24 -0700 (PDT) X-Google-Smtp-Source: AIpwx49iMyBtB8W/3G2H/Cl+Wk6zM0VmxpOji6ZDGK1l/HrcDsGInbavnRN/GdtUzPlaOYUbc7iQ X-Received: by 10.98.225.20 with SMTP id q20mr28424577pfh.142.1523263224404; Mon, 09 Apr 2018 01:40:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523263224; cv=none; d=google.com; s=arc-20160816; b=S2hv96pHw6N3xaLfrMSPR4KmAkS6eOF/l6Gl89eZkZqkyrxLROWFDLBNbYmYWA7EE7 dxALnYCMCs9ekBLRlf8fPb6WUV1qgt/rsmjXOWQzl0Y+M7uJgx5MWJvDz/4ti/K5D69O 2drybHKla5HpYxr7iRJHCNKy1vEJPD/SrBksLMZjQ0B07ZRdY7zZ0G1JS2VmYZkXcnDC DrnzB4aLdOuZoaQDCfLjjtnqsyMzxgs0qzNvawdJO8KRcYtoRytpeSEsxNjrW1cMNR+m 48IqVz8BMF7Dh0pWf26YqyMuWl+lj7ZM1zBs0LQALxVhAE8OduLTva9Qur/2FxKUEkqG NBBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=SpuioNASokdSTMmhenIT+P3W05Us0CWBkXuexoxjWFo=; b=KrbR/m0vXmxL5ebCuRzGiXvkFQMB2jEBgWhDafC9/AYIJy9az9BkeTorGhVXJ4kIZp EzhwccmgdjwfOJsW1wjOt14pAuvmUgksKQrO0c+p4zmbTm7iYVUlqwbbwCKHT3xmFSK4 rlT3qQw/+IqzV7/Fu+lnpdf5KfZGclPa4V42fq1+3M6ofAzPbnoivC08mor8jynOhU2w nwldY+FyQbf4vcG6eT8R2QFSKuynFa5oEJnpnGNEUn4t6TbXOpRq2E+JDbMq8z3m9zRw HlKfmrN1doXl9/uMwBH9bZg8rjoSoHSi0ILD+q/YZ7GVcF+FzqjoHLOBx+TSvgg9wyt1 JiOw== 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 w2-v6si14192504plk.702.2018.04.09.01.39.47; Mon, 09 Apr 2018 01:40:24 -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 S1752364AbeDIIdq (ORCPT + 99 others); Mon, 9 Apr 2018 04:33:46 -0400 Received: from bhuna.collabora.co.uk ([46.235.227.227]:57822 "EHLO bhuna.collabora.co.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752295AbeDIIdn (ORCPT ); Mon, 9 Apr 2018 04:33:43 -0400 Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: eballetbo) with ESMTPSA id 6520A27339E From: Enric Balletbo i Serra To: Daniel Thompson , Doug Anderson , Pavel Machek , Rob Herring , Lee Jones Cc: Jingoo Han , Richard Purdie , Jacek Anaszewski , Brian Norris , Guenter Roeck , Alexandru Stan , linux-leds@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, kernel@collabora.com Subject: [RESEND PATCH v3 1/4] backlight: pwm_bl: linear interpolation between brightness-levels Date: Mon, 9 Apr 2018 10:33:30 +0200 Message-Id: <20180409083333.1249-2-enric.balletbo@collabora.com> X-Mailer: git-send-email 2.16.3 In-Reply-To: <20180409083333.1249-1-enric.balletbo@collabora.com> References: <20180409083333.1249-1-enric.balletbo@collabora.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Setting num-interpolated-steps in the dts will allow you to have linear interpolation between values of brightness-levels. This way a high resolution pwm duty cycle can be used without having to list out every possible value in the dts. This system also allows for gamma corrected values. The most simple example is interpolate between two brightness values a number of steps, this can be done setting the following in the dts: brightness-levels = <0 65535>; num-interpolated-steps = <1024>; default-brightness-level = <512>; This will create a brightness-level table with the following values: <0 63 126 189 252 315 378 441 ... 64260 64323 64386 64449 65535> Another use case can be describe a gamma corrected curve, as we have better sensitivity at low luminance than high luminance we probably want have smaller steps for low brightness levels values and bigger steps for high brightness levels values. This can be achieved with the following in the dts: brightness-levels = <0 4096 65535>; num-interpolated-steps = <1024>; default-brightness-level = <512>; This will create a brightness-levels table with the following values: <0 4 8 12 16 20 ... 4096 4156 4216 4276 ... 65535> Signed-off-by: Enric Balletbo i Serra Acked-by: Daniel Thompson --- drivers/video/backlight/pwm_bl.c | 83 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/drivers/video/backlight/pwm_bl.c b/drivers/video/backlight/pwm_bl.c index 8e3f1245f5c5..f0a108ab570a 100644 --- a/drivers/video/backlight/pwm_bl.c +++ b/drivers/video/backlight/pwm_bl.c @@ -147,7 +147,11 @@ static int pwm_backlight_parse_dt(struct device *dev, struct platform_pwm_backlight_data *data) { struct device_node *node = dev->of_node; + unsigned int num_levels = 0; + unsigned int levels_count; + unsigned int num_steps; struct property *prop; + unsigned int *table; int length; u32 value; int ret; @@ -167,6 +171,7 @@ static int pwm_backlight_parse_dt(struct device *dev, /* read brightness levels from DT property */ if (data->max_brightness > 0) { size_t size = sizeof(*data->levels) * data->max_brightness; + unsigned int i, j, n = 0; data->levels = devm_kzalloc(dev, size, GFP_KERNEL); if (!data->levels) @@ -184,6 +189,84 @@ static int pwm_backlight_parse_dt(struct device *dev, return ret; data->dft_brightness = value; + + /* + * This property is optional, if is set enables linear + * interpolation between each of the values of brightness levels + * and creates a new pre-computed table. + */ + of_property_read_u32(node, "num-interpolated-steps", + &num_steps); + + /* + * Make sure that there is at least two entries in the + * brightness-levels table, otherwise we can't interpolate + * between two points. + */ + if (num_steps) { + if (data->max_brightness < 2) { + dev_err(dev, "can't interpolate\n"); + return -EINVAL; + } + + /* + * Recalculate the number of brightness levels, now + * taking in consideration the number of interpolated + * steps between two levels. + */ + for (i = 0; i < data->max_brightness - 1; i++) { + if ((data->levels[i + 1] - data->levels[i]) / + num_steps) + num_levels += num_steps; + else + num_levels++; + } + num_levels++; + dev_dbg(dev, "new number of brightness levels: %d\n", + num_levels); + + /* + * Create a new table of brightness levels with all the + * interpolated steps. + */ + size = sizeof(*table) * num_levels; + table = devm_kzalloc(dev, size, GFP_KERNEL); + if (!table) + return -ENOMEM; + + /* Fill the interpolated table. */ + levels_count = 0; + for (i = 0; i < data->max_brightness - 1; i++) { + value = data->levels[i]; + n = (data->levels[i + 1] - value) / num_steps; + if (n > 0) { + for (j = 0; j < num_steps; j++) { + table[levels_count] = value; + value += n; + levels_count++; + } + } else { + table[levels_count] = data->levels[i]; + levels_count++; + } + } + table[levels_count] = data->levels[i]; + + /* + * As we use interpolation lets remove current + * brightness levels table and replace for the + * new interpolated table. + */ + devm_kfree(dev, data->levels); + data->levels = table; + + /* + * Reassign max_brightness value to the new total number + * of brightness levels. + */ + data->max_brightness = num_levels; + } + data->max_brightness--; } -- 2.16.3