Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp422397ybh; Mon, 20 Jul 2020 21:27:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyms38eoV5qf1FSUx8tK37CKTX1HleSQNp0FsDp+roN3fas3xKzhLNgKfrPDkloWlYPwSHt X-Received: by 2002:a17:906:6959:: with SMTP id c25mr23058383ejs.375.1595305673546; Mon, 20 Jul 2020 21:27:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595305673; cv=none; d=google.com; s=arc-20160816; b=shu1YZA7ypzUDeW8f18X53ZGSQroushqKXFGYleRg5nKoidx8Ft4s1Ky1LxgnYxyGL U7Gg9lDl4SIrtEEf9Q2Y7TVzU+KU1LhcrCZIp98BYNHV1y4vLQftHudtZUcuXCECxigu TFJF0r0jZkQHYbQ8CB1N8zhcx6p7XY8aDv8JqFWOgtzeu8aLg/e2wYpOMwQcJmlvfwrR e4V+5p6xghh4/+xWLTVuWwf39XTxETgcmENcW4BwSVWHLy51MABYi5/rHaK2EXTKIe/h EQ7JNXv1lWiw8QgUzweArxks5vcD0KC/feTeq6PyLBJtnRHAvFkMrjyN9doS1ftVxX5u qmWA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=+azHcaGZBExcNgCV7GvONNLJM0PsErlOdvqFw7KBEwo=; b=NDwtZjTrdxDgEotLNcziLXjOQWrvx00ANwOsi6y+aUGk/arqolY6WpPe14fjUrKO9X nfJuHweIqheQMYOZhyKhpdHp9IFvr90C0g2khhcGzo+5XGi8HhCUUGWkPY3OKnNKqezh c1XKf31NXba+Ty9HbC6dddY9q1WPf/r8CpzvBG0M54fIw7yvL13R83UIyJf8yWTC7IVL ayobqLSFX79jwB3xgmD5dCLxPRdsfVWv/zBtBzr5U36dW96cAvOLRwEuChe+ztFd65Ng ZO1U1D5U1SntDwkEJVnJwb4B9+dklnjwyTarSOWkrLL9ZsPNpPDKRmpMd2Eg5LPM/jO/ PMSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=atLCSv1E; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id d92si11586002edd.183.2020.07.20.21.27.27; Mon, 20 Jul 2020 21:27:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=atLCSv1E; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1726417AbgGUE1P (ORCPT + 99 others); Tue, 21 Jul 2020 00:27:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726010AbgGUE1O (ORCPT ); Tue, 21 Jul 2020 00:27:14 -0400 Received: from mail-pj1-x1044.google.com (mail-pj1-x1044.google.com [IPv6:2607:f8b0:4864:20::1044]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 37B1AC0619D5 for ; Mon, 20 Jul 2020 21:27:14 -0700 (PDT) Received: by mail-pj1-x1044.google.com with SMTP id gc9so941095pjb.2 for ; Mon, 20 Jul 2020 21:27:14 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=+azHcaGZBExcNgCV7GvONNLJM0PsErlOdvqFw7KBEwo=; b=atLCSv1EBxJkYoBnJzRXFqbtgESLRJzG7/KIw7V61wkbSesm8DrfFClztE3hl0YKn2 zhvMJB53o8AjyD15maS9ei03a8VdZ8V6ejQ3gOHpH8R4Ts2xqki/oXbuzosB2E9xVowQ REFefSCvyJiYypXQbs8xUXcMN6u34h1Y5A+PE= 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=+azHcaGZBExcNgCV7GvONNLJM0PsErlOdvqFw7KBEwo=; b=Nu+f1FvwNqTigvY0T7K8FuIHarai0+4dZ4L1bwJ8SF/ZYmcTWFm824vYpNNaGvggz/ vcz8+HpEfFAgXAnKOOWZIbrDbKBlUtAj/BwQq/KpnwaUJFNWNEx83Ib8sk7dixE+ffnK rDL7aYFNv4PRhkCvWnc5OXNM+sXGLPRQz/rJwD6EaJ2AbiqSw9rX3xnAszdgTaIzL139 g9V79pyy3X2wdm7TAdannKiQ9emxuRSrMrhOG8v8gfKF5zGCAFFHTTssygGkmJL3JPN1 DpJv4LS8yKGjHj3glu9YlJ5c50QNs+9nSNrpaMEh1/B4C7GMRdLIsWDsDp7uItCRpH4J uOwA== X-Gm-Message-State: AOAM5339v4NeBx29Tk1pnnjktvLENwMwFwhBiyTh2QOYcZ43PB11B1DO nTN0dNrkxuCLZi2efgiJ6pyveg== X-Received: by 2002:a17:90a:240a:: with SMTP id h10mr2845650pje.225.1595305633715; Mon, 20 Jul 2020 21:27:13 -0700 (PDT) Received: from alex-desktop.lan (c-73-63-253-164.hsd1.ca.comcast.net. [73.63.253.164]) by smtp.gmail.com with ESMTPSA id o8sm1207075pjf.37.2020.07.20.21.27.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jul 2020 21:27:13 -0700 (PDT) From: Alexandru Stan To: Thierry Reding , =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , Lee Jones , Daniel Thompson , Jingoo Han , Bartlomiej Zolnierkiewicz , Heiko Stuebner , Rob Herring Cc: Matthias Kaehlcke , Douglas Anderson , Enric Balletbo i Serra , Alexandru Stan , dri-devel@lists.freedesktop.org, linux-fbdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pwm@vger.kernel.org Subject: [PATCH 1/3] backlight: pwm_bl: Fix interpolation Date: Mon, 20 Jul 2020 21:25:20 -0700 Message-Id: <20200720212502.1.I4dcea1c90e9da3902d466033aa73351e19e49c49@changeid> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200721042522.2403410-1-amstan@chromium.org> References: <20200721042522.2403410-1-amstan@chromium.org> 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 Whenever num-interpolated-steps was larger than the distance between 2 consecutive brightness levels the table would get really discontinuous. The slope of the interpolation would stick with integers only and if it was 0 the whole line segment would get skipped. Example settings: brightness-levels = <0 1 2 4 8 16 32 64 128 256>; num-interpolated-steps = <16>; The distances between 1 2 4 and 8 would be 1, and only starting with 16 it would start to interpolate properly. Let's change it so there's always interpolation happening, even if there's no enough points available (read: values in the table would appear more than once). This should match the expected behavior much more closely. Reviewed-by: Douglas Anderson Reviewed-by: Matthias Kaehlcke Signed-off-by: Alexandru Stan --- drivers/video/backlight/pwm_bl.c | 70 ++++++++++++++------------------ 1 file changed, 31 insertions(+), 39 deletions(-) diff --git a/drivers/video/backlight/pwm_bl.c b/drivers/video/backlight/pwm_bl.c index 82b8d7594701..5193a72305a2 100644 --- a/drivers/video/backlight/pwm_bl.c +++ b/drivers/video/backlight/pwm_bl.c @@ -235,8 +235,7 @@ 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_levels; unsigned int num_steps = 0; struct property *prop; unsigned int *table; @@ -265,12 +264,11 @@ static int pwm_backlight_parse_dt(struct device *dev, if (!prop) return 0; - data->max_brightness = length / sizeof(u32); + num_levels = length / sizeof(u32); /* 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; + if (num_levels > 0) { + size_t size = sizeof(*data->levels) * num_levels; data->levels = devm_kzalloc(dev, size, GFP_KERNEL); if (!data->levels) @@ -278,7 +276,7 @@ static int pwm_backlight_parse_dt(struct device *dev, ret = of_property_read_u32_array(node, "brightness-levels", data->levels, - data->max_brightness); + num_levels); if (ret < 0) return ret; @@ -303,7 +301,13 @@ static int pwm_backlight_parse_dt(struct device *dev, * between two points. */ if (num_steps) { - if (data->max_brightness < 2) { + unsigned int num_input_levels = num_levels; + unsigned int i; + u32 x1, x2, x; + u32 y1, y2; + s64 dx, dy; + + if (num_input_levels < 2) { dev_err(dev, "can't interpolate\n"); return -EINVAL; } @@ -313,14 +317,7 @@ static int pwm_backlight_parse_dt(struct device *dev, * 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++; + num_levels = (num_input_levels - 1) * num_steps + 1; dev_dbg(dev, "new number of brightness levels: %d\n", num_levels); @@ -332,24 +329,25 @@ static int pwm_backlight_parse_dt(struct device *dev, 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++; + /* + * Fill the interpolated table[x] = y + * by draw lines between each (x1, y1) to (x2, y2). + */ + dx = num_steps; + for (i = 0; i < num_input_levels - 1; i++) { + x1 = i * dx; + x2 = x1 + dx; + y1 = data->levels[i]; + y2 = data->levels[i + 1]; + dy = y2 - y1; + + for (x = x1; x < x2; x++) { + table[x] = y1 + + div_s64(dy * (x - x1), dx); } } - table[levels_count] = data->levels[i]; + /* Fill in the last point, since no line starts here. */ + table[x2] = y2; /* * As we use interpolation lets remove current @@ -358,15 +356,9 @@ static int pwm_backlight_parse_dt(struct device *dev, */ 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--; + data->max_brightness = num_levels - 1; } return 0; -- 2.27.0