Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp592476pxu; Thu, 15 Oct 2020 11:22:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyp9npsA1UZgaL341tiiJD586DNai78Tqv27XQozzTqi7yE5gnA/5B1BnPbbtl4/IuqQsg6 X-Received: by 2002:a17:906:2c45:: with SMTP id f5mr5784992ejh.112.1602786137036; Thu, 15 Oct 2020 11:22:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1602786137; cv=none; d=google.com; s=arc-20160816; b=oJtH0BC9NqUDylTHoTJYwjL7mNuAiCb/A37cuKpQmXg6UwaYzgOXjwOPpRGP6wuCzZ HmK0CdzofkxNgcz+iqWeibjX+ZDyQ756v2H/A+WN1+JQLmvolk02xMsuSCl4PwGR7ujh OpsV/79qdxteExmfYOzUyJhF0mB0mQ1XsHUjtrtQiDqS1038E+fdRSIe8K7PpQ1Vv/3i 5EqFUHhrNa+oo8+Zczh2gakgaE+mlM41OBcoCXYItQh7AMI1Jh2R0th3NsTr9gH7SECn 34/A1Wo3HxSsOQuk8DupXurre61dyKeL1oqjL/rYBFNDCaPGJ7BH4z0CbbOJqGQRqDSB bIOA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version; bh=H7FwAhhWPHZZtKLoXu0OtijXG8l8PtSjQz10RZ331mg=; b=TxbKyVSIAzVwSVH1JfVyt4LV4ObZRGpZw20wKEq4+dANNE2JiRYyeS0VTHK1n4HPd7 TpjjcyhgiDBtjMj2EMg1NGFivyM8TM1keWHHuCEupbCtX8L3nrfNuNhumc2ogGk4UCnY nAuhVdt+UIIMYKlPiBPBfIwQKXafn3/ART9pT7SlVmF/m5LKMhVeYp/sfWI7IGjPswAL pa+Dh/pncX50fZLIIbG8W6WFKRYehKlOyAyoKjrSEk4zW27ROm0f3CgpY81k6N+95pS/ jlsweQnyAy5Fd9QMV6b8F/o2mAOLd+vv1vJ+3jWLzs3pgcH/r06lZohMjrPKmTUPUBYv sZog== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i19si2691559ejh.153.2020.10.15.11.21.38; Thu, 15 Oct 2020 11:22:17 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729413AbgJOGzK (ORCPT + 99 others); Thu, 15 Oct 2020 02:55:10 -0400 Received: from mail-oo1-f65.google.com ([209.85.161.65]:34450 "EHLO mail-oo1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725208AbgJOGzK (ORCPT ); Thu, 15 Oct 2020 02:55:10 -0400 Received: by mail-oo1-f65.google.com with SMTP id o20so480046ook.1; Wed, 14 Oct 2020 23:55:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=H7FwAhhWPHZZtKLoXu0OtijXG8l8PtSjQz10RZ331mg=; b=V2MNpL482J+aSwyZJ4F8JV+OwattGUZNkOtmvdfTFZ312D8h4m/be9YBPiVOYTRY71 /EKooAcFa2vHfhKG+e8KtalbnEkqev3QFWBcdaJH9cps7T9CV+IdqYYn2OUE4erjVHyA VfXIIe2BdUYO/tqIyBNbX/2MbKG1g3Z3uY6DpPfLwtdor6Sx2mxLYxruRNxN6MyckOUr OSqGO6y0LjadwFX3TowzXNsQuyhG3EdIC3DQbrb1GxID8Sz56djhydCvfK4F3Ol5R4X6 7Y4eCP9Y0vZHIVYOg82QwkD5U8cO9d9BkS4S4OiItKMAlzh7F/W4jYjoDzKBkDI+6Aiv dqYg== X-Gm-Message-State: AOAM533RXOETy/IHXq3bVOsinQVqDfj70Ah8+aYkZRD3FPdVuY9vazyw eyuCmY1jL7bm2uE9X0uMZUBSYRe/k1wb0YhJ2rzkLQ3u+PY= X-Received: by 2002:a4a:dbd3:: with SMTP id t19mr1350129oou.40.1602744909171; Wed, 14 Oct 2020 23:55:09 -0700 (PDT) MIME-Version: 1.0 References: <20201013080103.410133-1-amstan@chromium.org> <20201013010056.v2.1.I4dcea1c90e9da3902d466033aa73351e19e49c49@changeid> In-Reply-To: <20201013010056.v2.1.I4dcea1c90e9da3902d466033aa73351e19e49c49@changeid> From: Geert Uytterhoeven Date: Thu, 15 Oct 2020 08:54:57 +0200 Message-ID: Subject: Re: [PATCH v2 1/3] backlight: pwm_bl: Fix interpolation To: Alexandru Stan Cc: Thierry Reding , =?UTF-8?Q?Uwe_Kleine=2DK=C3=B6nig?= , Lee Jones , Daniel Thompson , Jingoo Han , Bartlomiej Zolnierkiewicz , Heiko Stuebner , Rob Herring , Andy Gross , Bjorn Andersson , Douglas Anderson , Enric Balletbo i Serra , Matthias Kaehlcke , DRI Development , Linux Fbdev development list , Linux Kernel Mailing List , Linux PWM List Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Alexandru, On Tue, Oct 13, 2020 at 1:57 PM 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. > > Signed-off-by: Alexandru Stan Thanks for your patch! > --- a/drivers/video/backlight/pwm_bl.c > +++ b/drivers/video/backlight/pwm_bl.c > @@ -327,24 +324,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 = (s64)y2 - y1; > + > + for (x = x1; x < x2; x++) { > + table[x] = y1 + > + div_s64(dy * ((s64)x - x1), dx); Yummy, 64-by-32 divisions. Shouldn't this use a rounded division? Nevertheless, I think it would be worthwhile to implement this using a (modified) Bresenham algorithm, avoiding multiplications and divisions, and possibly increasing accuracy as well. https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm > } > } > - 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 Gr{oetje,eeting}s, Geert -- Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds