Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp1235431pxk; Fri, 4 Sep 2020 04:31:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw6zHUT/ZlSC8IjCyP1hhpzYHetVZNuKevqiDxV0kCnTSFiKHVCCkSxcmfjTjIeqs/TLsdu X-Received: by 2002:a17:906:88d:: with SMTP id n13mr7026253eje.75.1599219081060; Fri, 04 Sep 2020 04:31:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1599219081; cv=none; d=google.com; s=arc-20160816; b=cYUoYpehg1/fYaex4QSycz3RGLGl50wP6jNekpRgaBr1wrwgl3Vmy4tbbumnJig/RC qXTRN6n44MmZEQ1B0u0N5tE+Wh3nxthcGz5hYuTGaim9evq0VvFOkJ+KndCzX7eXf95v wRWd9dd93ytAX5lsgwbhJmFpm9nA/+6MgvajpApqmQtl0dXL4qRRPwPbLwLUSWM/gVZ+ SlE8x5pTTk+2JbLbKgy7YOJqy3Opq7pSZZ5sX8HIBCaiKWibgABVDYyvYy0zzAgwmA5y 2xHcVshvgfNN+xWv9J0IKSOJx8tL+wCPwt5HApR2iV+zwAe4CrOJVFu7qPsaVvPlgLQc hnqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=erzVCkG6u4B8KH8TDdWQurkzlfyvvTZQMwD1DF4GTNA=; b=rG4Aalc0qVQVbuX3w4e8G8e7sJMNycrqpI46g5uKRC84JFuynHSB3pYSGE2YfMvs+d pzC+PWAFrwXkXExXiX11+/I5CnxryEx+3CWgGQIDbDAIVb3WcF7QgXbrTKLly+Y7PV2P esaiPqVdbvzQILMnLWcW8k6Spest4ppAaQNzjgagnLwquNyfkHc8oAZ/g8mwhTFxpNg3 2p0uanpbLpRVQdwBNXaXvceBZPOxCO7Jah0lsg2DRubfliBAHgJQj7G/X91fKutVSHlh Q50xaJU99IE2kvv260yvMM8gCX4PwNUBNUWDyckpL9bSyX3nOfMDTkM39IkqYYTfIyJ9 lMug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=EMjTlfHu; 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=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ga23si3686943ejb.691.2020.09.04.04.30.57; Fri, 04 Sep 2020 04:31:21 -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=@linaro.org header.s=google header.b=EMjTlfHu; 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=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730218AbgIDL1r (ORCPT + 99 others); Fri, 4 Sep 2020 07:27:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60380 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730224AbgIDL1k (ORCPT ); Fri, 4 Sep 2020 07:27:40 -0400 Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D60DBC061247 for ; Fri, 4 Sep 2020 04:27:39 -0700 (PDT) Received: by mail-wr1-x441.google.com with SMTP id x14so6341700wrl.12 for ; Fri, 04 Sep 2020 04:27:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=erzVCkG6u4B8KH8TDdWQurkzlfyvvTZQMwD1DF4GTNA=; b=EMjTlfHuyvDcRxyjY9iQJUH7v3QH6uHOF5whif6JAm7KZKiX2+MN2Xdd8loS/OERcq sfxPHVS//O1wfzaQwir2aQY3LnpWzGmxXw9elj2ATmbC8eZzM3rYj/vOaVainYJtDOoZ n16B1RA9G6kM5vqHot0cK5z1wlMcL6zqw0hynHPOnGsHcfP1gNssyZSU15gHHyIxpfwT MWI3Suzlv7yndH/GhN8WFoZKZL+zaJAT2KDInBYJQCj1j6fwTZ53Lb0+AziTQqcGzJZN p8+vyeWjI4nOhRhqZZ/cq+IvZ3WAKqIdfLQos3a9DpmKcfIr/r+v5mrvqFqC/PcR6GWf pPLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=erzVCkG6u4B8KH8TDdWQurkzlfyvvTZQMwD1DF4GTNA=; b=aIjstihBNI9wPqxKoM16hTT4omoKyAGYRbrda7KUyG7+HJzmybw0vkzYZsqqeqNjY8 uRuBgIUPpyf4lEO9rOr0a9mwTANb6pXVQWxXiZeEvMA8encVZ0cCNat1Y91z2xlNqq4X wDfJz2WWA3GVayPm0I7ODmtZBAvMWqnfD2J4OGYOfQ5RIed4Li0dSx3MU7w9hfEJbhbN oUWFB7qTTotN9udOTCESAWowBwmSLsnCHYa3vaRs17kPgRCp4eljfa2RCc0bEW1EnkSm HfilxrQubB+ppByabn2SuhyXtMw5IQTq6nGQ4jy5JPOTwwYI8oeqWjDmUxXoccPudSo0 H/xQ== X-Gm-Message-State: AOAM531poQ7xcR0KQVAdIhyrAhL3mEhPPRncQbHg5t2vPAclR6wbngEp 6avQlJVIwjkrSb8epUSCX8nKoA== X-Received: by 2002:a5d:6852:: with SMTP id o18mr7062275wrw.113.1599218855784; Fri, 04 Sep 2020 04:27:35 -0700 (PDT) Received: from holly.lan (cpc141214-aztw34-2-0-cust773.18-1.cable.virginm.net. [86.9.19.6]) by smtp.gmail.com with ESMTPSA id 124sm10860162wmd.31.2020.09.04.04.27.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Sep 2020 04:27:34 -0700 (PDT) Date: Fri, 4 Sep 2020 12:27:32 +0100 From: Daniel Thompson To: Alexandru Stan Cc: Thierry Reding , Uwe =?utf-8?Q?Kleine-K=C3=B6nig?= , Lee Jones , Jingoo Han , Bartlomiej Zolnierkiewicz , Heiko Stuebner , Rob Herring , Matthias Kaehlcke , Douglas Anderson , Enric Balletbo i Serra , dri-devel@lists.freedesktop.org, linux-fbdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pwm@vger.kernel.org Subject: Re: [PATCH 1/3] backlight: pwm_bl: Fix interpolation Message-ID: <20200904112732.zdfzmjgpcds3jpzo@holly.lan> References: <20200721042522.2403410-1-amstan@chromium.org> <20200720212502.1.I4dcea1c90e9da3902d466033aa73351e19e49c49@changeid> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200720212502.1.I4dcea1c90e9da3902d466033aa73351e19e49c49@changeid> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jul 20, 2020 at 09:25:20PM -0700, Alexandru Stan wrote: > 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 Apologies for the delay. Patch 2/3 meant I had some thinking to do... and then the holiday's took their toll. Overall this looks good, just some quibbles about broken 64-bit maths. > --- > > 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; dx should be 32-bit. It will be truncated to 32-bit when passed to div_s64() so this type is actively misleading about how the maths works. > + > + 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; This is an u32 expression being assigned to a s64. I could be rusty on my fixed point maths but won't this promote too late for the 64-bitness of dy to be useful? Daniel. > + > + 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